我正在尝试将徽标图片与另一张图片重叠,但是得到这个:
error on line 10 : (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
我要运行的代码段如下:
img2 = cv2.imread('Capture.jpg' , cv2.IMREAD_COLOR)
logo = cv2.imread('pyt.jpg')
rows , cols , channel = img2.shape
roi = img2[0:rows , 0:cols]
gray_img = cv2.cvtColor(logo , cv2.COLOR_BGR2GRAY)
ret , mask = cv2.threshold(gray_img , 220 , 255 , cv2.THRESH_BINARY)
#cv2.imshow('mask' , mask)
mask_inv = cv2.bitwise_not(mask)
#cv2.imshow('img' , mask_inv)
img_bg = cv2.bitwise_and(roi , roi , mask = mask_inv) #error on this line
#img_fg = cv2.bitwise_and(logo, logo , mask = mask)
#dst = cv2.add(img_bg , img_fg)
#logo[0:row , 0:col] = dst
#cv2.imshow('img' , img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:2)
诀窍在于,掩码应与目标大小相同。您的代码中的错误来自这一部分:
rows , cols , channel = img2.shape
roi = img2[0:rows , 0:cols]
roi
应该具有徽标的大小,而不是整个图像的大小!
除了使用按位运算外,使用蒙版复制徽标要简单得多,当然要注意ROI大小:
rows , cols = logo.shape[:2]
roi = img2[0:rows , 0:cols]
cv.copyTo(logo, mask, roi)