result = cv2.addWeighted(image, 0.8, cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR), 0.2, 0) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: 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 'cv::arithm_op' result = cv2.addWeighted(image, 0.8, cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR), 0.2, 0) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: 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 'cv::arithm_op'
时间: 2024-04-09 16:32:08 浏览: 257
这个错误提示告诉我们输入的数组尺寸不匹配。在这种情况下,`addWeighted`函数要求原始图像和边缘图像具有相同的尺寸和通道数。
请确保原始图像和边缘图像的尺寸相同。您可以使用`cv2.resize()`函数调整边缘图的尺寸,使其与原始图像保持一致。以下是修改后的代码示例:
```python
import cv2
import numpy as np
# 读取原图
original_image = cv2.imread('original_image.jpg')
# 将原图转为灰度图
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
# 使用Canny算法进行边缘检测
edges = cv2.Canny(gray_image, 100, 200)
# 调整边缘图尺寸与原图相同
edges = cv2.resize(edges, (original_image.shape[1], original_image.shape[0]))
# 将边缘图转换为三通道
edges_bgr = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
# 将边缘图与原图叠加
result = cv2.addWeighted(original_image, 0.8, edges_bgr, 0.2, 0)
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这样修改后,边缘图将被调整为与原始图像相同的尺寸,并且将其转换为三通道图像。然后再进行叠加操作。希望这能解决您遇到的问题。
阅读全文