ali*_*olo 7 opencv python-3.x python-3.6
这是我的图像融合代码,但是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()
Run Code Online (Sandbox Code Playgroud)
错误是:
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
Run Code Online (Sandbox Code Playgroud)
问题是什么?我搜索了该函数,并确定该函数正确。我不明白这个错误!
When you run this:
dst= cv2.addWeighted(img1,0.5,img2,0.5,0)
Run Code Online (Sandbox Code Playgroud)
Error info:
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
Run Code Online (Sandbox Code Playgroud)
Possible reasons:
not np.ndarray, such as None. Maybe you havn't read it.img1.shape does not equal to img2.shape. They have different size.You should check img1.shape and img2.shape before you directly do cv2.addWeighted if you are not sure whether they are the same size.
Or, if you want to add small image on the big one, you should use ROI/mask/slice op.
小智 5
正如上面答案中问题和原因 2 的评论之一所指出的,您还可以尝试调整其中一张图像的大小以匹配另一张图像,然后尝试addWeighted。
您的代码将如下所示:
import cv2
import numpy as np
img1 = cv2.imread('1.png')
img2 = cv2.imread('messi.jpg')
# Read about the resize method parameters here: https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize
img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
dst = cv2.addWeighted(img1, 0.7, img2_resized, 0.3, 0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8850 次 |
| 最近记录: |