这是我的图像混合代码,但 cv2.addweighted() 函数有问题:
import cv2
import numpy as np
img1 = cv2.imread('1.png')
img2 = cv2.imread('messi.jpg')
dst= cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
错误是:
Traceback (most recent call last):
dst= cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.error: C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:659: 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 cv::arithm_op
问题是什么?我搜索了这个函数,我确定这个函数是正确的。我没有理解错误!
原文由 alilolo 发布,翻译遵循 CC BY-SA 4.0 许可协议
当你运行这个:
错误信息:
可能的原因:
not np.ndarray
,例如None
。也许你还没有读过它。img1.shape
不等于img2.shape
。它们有不同的尺寸。你应该检查
img1.shape
和img2.shape
在你直接做之前cv2.addWeighted
如果你不确定它们是否相同大小。或者,如果你想在大图像上添加小图像,你应该使用
ROI
/mask
/slice
操作。