首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV与Python断言失败((mask.type() == CV_8UC1 CV_8UC1 mask.type() == CV_8SC1))在binary_op中

OpenCV与Python断言失败((mask.type() == CV_8UC1 CV_8UC1 mask.type() == CV_8SC1))在binary_op中
EN

Stack Overflow用户
提问于 2015-03-19 19:42:48
回答 1查看 9.7K关注 0票数 4

我试图覆盖一个图像在网络摄像头的提要上。这是代码的主要部分-

代码语言:javascript
复制
# Load our overlay image: glasses.png
imgGlasses = cv2.imread('1.png')
     
# Create the mask for the glasses
imgGlassesGray = cv2.cvtColor(imgGlasses, cv2.COLOR_BGR2GRAY)
ret, orig_mask = cv2.threshold(imgGlasses, 10, 255, cv2.THRESH_BINARY)
 
# Create the inverted mask for the glasses
orig_mask_inv = cv2.bitwise_not(orig_mask)
 
# Convert glasses image to BGR
# and save the original image size (used later when re-sizing the image)
imgGlasses = imgGlasses[:,:,0:3]
origGlassesHeight, origGlassesWidth = imgGlasses.shape[:2]
    
video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        #cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]
            
            eyes = eye_cascade.detectMultiScale(roi_gray)
            for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
                
        for (ex, ey, ew, eh) in eyes:
            glassesWidth = 3*ew
            glassesHeight = glassesWidth * origGlassesHeight / origGlassesWidth
            
            # Center the glasses on the bottom of the nose
            x1 = ex - (glassesWidth/4)
            x2 = ex + ew + (glassesWidth/4)
            y1 = ey + eh - (glassesHeight/2)
            y2 = ey + eh + (glassesHeight/2)
                     
            # Re-calculate the width and height of the glasses image
            glassesWidth = x2 - x1
            glassesHeight = y2 - y1
         
            # Re-size the original image and the masks to the glasses sizes
            # calcualted above
            glasses = cv2.resize(imgGlasses, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)
            mask = cv2.resize(orig_mask, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)
            mask_inv = cv2.resize(orig_mask_inv, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)
         
            # take ROI for glasses from background equal to size of glasses image
            roi = roi_color[y1:y2, x1:x2]
         
                # roi_bg contains the original image only where the glasses is not
                # in the region that is the size of the glasses.
            roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
         
                # roi_fg contains the image of the glasses only where the glasses is
            roi_fg = cv2.bitwise_and(glasses,glasses,mask = mask)
         
                # join the roi_bg and roi_fg
            dst = cv2.add(roi_bg,roi_fg)
         
                # place the joined image, saved to dst back over the original image
            roi_color[y1:y2, x1:x2] = dst
         
            break

我在运行程序时遇到了以下错误,我不知道如何解决这个问题-

错误:断言失败(( /home/user/opencv-2.4.10/modules/core/src/arithm.cpp,( mask.type() ) == CV_8UC1 =mask.type() == CV_8SC1)),在binary_op中,文件第1035行 回溯(最近一次调用):文件"main_eye.py",第86行 错误: /home/user/opencv-2.4.10/modules/core/src/arithm.cpp:1035:错误:(-215) (函数binary_op中的mask.type() == CV_8UC1 mask.type() == CV_8SC1)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-19 21:32:48

你的面具是有3个通道的图像,但它应该只有一个通道。您可以使用mask.shape检查它-它将返回元组与您的面具尺寸。问题在于:

代码语言:javascript
复制
imgGlassesGray = cv2.cvtColor(imgGlasses, cv2.COLOR_BGR2GRAY)
ret, orig_mask = cv2.threshold(imgGlasses, 10, 255, cv2.THRESH_BINARY)

您正在使用3通道的图像创建掩码,因此您的掩码也将有3个通道。使用单通道映像创建它(很可能您希望使用imgGlassesGray而不是imgGlasses) --它会解决您的问题。

代码语言:javascript
复制
imgGlassesGray = cv2.cvtColor(imgGlasses, cv2.COLOR_BGR2GRAY)
ret, orig_mask = cv2.threshold(imgGlassesGray, 10, 255, cv2.THRESH_BINARY)
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29153569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档