Anjali Singh
Anjali Singh

Reputation: 47

Problem in overlapping image using OpenCv

I am trying to just overlap a logo pic with some another picture but getting this:

error on line 10 : (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'

The code snippet I am trying to run is as follows:

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()

Upvotes: 1

Views: 209

Answers (1)

Stefan Dragnev
Stefan Dragnev

Reputation: 14473

The trick is that the mask should have the same size as the destination. The error in your code comes from this part:

rows , cols , channel = img2.shape
roi = img2[0:rows , 0:cols]

roi is supposed to have the size of the logo, not the whole image!

Instead of using bitwise operations, it's much simpler to just copy the logo using the mask, of course, being careful with the ROI size:

rows , cols = logo.shape[:2]
roi = img2[0:rows , 0:cols]

cv.copyTo(logo, mask, roi)

Upvotes: 2

Related Questions