Jay Suthar
Jay Suthar

Reputation: 199

cv2.addWeighted() does not work

import cv2
import numpy as np
import time

path = "/home/jayu/Desktop/openCv/"

imgpath1 = path + "img1.jpg"
imgpath2 = path + "img2.jpg"


img1 = cv2.imread(imgpath1,1)
img2 = cv2.imread(imgpath2,1)
print (img1.shape)
print (img2.shape)


for i in np.linspace(0,1,10):
    alpha = i
    beta = 1-alpha
    output  = cv2.addWeighted(img1,alpha,img2,beta,0)
    cv2.imshow("hello",output)
    time.sleep(0.10)
    if cv2.waitKey(0)==27:
        break
cv2.destroyAllWindows(  )

when i am trying above code i get error:

output  = cv2.addWeighted(img1,alpha,img2,beta,0)
cv2.error: /build/opencv-2TNgni/opencv-3.1.0+dfsg1/modules/core/src/arithm.cpp:639: error: (-209) 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

img1.shape : (183, 275, 3) and img2.shape : (640, 960, 3)

what did i wrong here ?

Upvotes: 0

Views: 1988

Answers (1)

hkchengrex
hkchengrex

Reputation: 4826

It doesn't make sense to blend two images of different sizes. You can either:

  1. Use two images of the same size
  2. Resize one of the image to match the size of another image

    resize(img2, img2, img1.size())

    This resizes img2 to match the size of img1

  3. Add paddings to the smaller image such that it match the size of the larger image.

Upvotes: 4

Related Questions