0
votes

img1 img2

img1 = cv.imread('face.png',1)
img2 = cv.imread('flower.png',1)
img2= cv.resize(img2,(642,640))
cv.imshow('img1',img1)
cv.imshow('img2',img2)
dst = cv.addWeighted(img1,0.7,img2,0.3,1)
cv.imshow('dst',dst)
cv.waitKey(0)
cv.destroyAllWindows()

Give me this

error: OpenCV(4.2.0) ../modules/core/src/arithm.cpp:666: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'

1
You have posted two JPEG images yet your code opens two PNG images? - Mark Setchell
one was jpeg and other one was jpg I was getting this error so i change them both to png - c9der

1 Answers

0
votes

It should be:

img2= cv.resize(img2,(640,642))

The reason is, when you retrieve the image shape, it returns height, and width respectively.

(h, w) = img.shape[:2]

Then you resize the second image using width and height respectively.

img2 = cv.resize(img2, (w, h))

But it is best if you assert both image with the same shape, before using addWeighted, to make sure both images are the same shape.

assert img1.shape == img2.shape

Result:


enter image description here

Code:


import cv2 as cv

img1 = cv.imread('face.png', 1)
img2 = cv.imread('flower.png', 1)

(h, w) = img1.shape[:2]
img2 = cv.resize(img2, (w, h))

assert img1.shape == img2.shape

cv.imshow('img1', img1)
cv.imshow('img2', img2)

dst = cv.addWeighted(src1=img1, alpha=0.7, src2=img2, beta=0.3, gamma=1.0)

cv.imshow('dst', dst)
cv.waitKey(0)

cv.imwrite("dst.png", dst)

cv.destroyAllWindows()