cv2.addWeighted()不起作用

时间:2018-07-17 02:45:02

标签: python-3.x opencv

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

当我尝试上面的代码时,我得到了错误:

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)和 img2.shape:(640,960,3)

我在这里错了什么?

1 个答案:

答案 0 :(得分:2)

融合两个大小不同的图像没有意义。您可以:

  1. 使用两张相同大小的图像
  2. 调整其中一幅图像的大小以匹配另一幅图像的大小

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

    这将调整img2的大小以匹配img1

  3. 的大小
  4. 在较小的图像中添加paddings,使其与较大图像的大小匹配。

相关问题