opencv python 错误:断言失败(size.width>0&&size.height>0)

opencv python 错误:断言失败(size.width>0&&size.height>0)

问题描述:

我在运行以下代码时遇到错误.

I am getting an error while running the following code.

import cv2
import numpy as np
img = cv2.imread('messi.jpg',0)
img = cv2.line(img,(0,0),(50,50),(255,0,0),5)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

错误说:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown function, file ......\src\opencv\modules\highgui\src\window.cpp,第 261 行

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown function, file ......\src\opencv\modules\highgui\src\window.cpp, line 261

回溯(最近一次调用最后一次):

Traceback (most recent call last):

文件F:\Computer Programming\scripts\OpenCv\1.py",第 6 行,在cv2.imshow("图片",img)cv2.error: ......\src\opencv\modules\highgui\src\window.cpp:261: 错误: (-215) size.width>0 &&尺寸.高度>0

File "F:\Computer programming\scripts\OpenCv\1.py", line 6, in cv2.imshow("image",img) cv2.error: ......\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0

如果我删除该行:

img = cv2.line(img,(0,0),(50,50),(255,0,0),5)

脚本有效.

这是因为 cv2.line 返回 None 而您将其分配给您的 img代码> 变量.所以当你到下一行并尝试显示图像时,没有图像可以显示.

Its because cv2.line returns None and you are assigning that to your img variable. So when you get to the next line and try to show the image, there is no image to be shown.

cv2.line(img) 替换 img = cv2.line(img,(0,0),(50,50),(255,0,0),5),(0,0),(50,50),(255,0,0),5)

Replace img = cv2.line(img,(0,0),(50,50),(255,0,0),5) with cv2.line(img,(0,0),(50,50),(255,0,0),5)

在此处阅读有关 cv2.circle 的更多信息.