您的位置:首页 > 大数据 > 人工智能

opencv 报错 OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow

2018-01-11 21:59 1276 查看
感觉国内这种资料真的很少,最后在stack overflow找到的答案,先贴个原贴,感谢国外的大牛们。
https://stackoverflow.com/questions/33052570/opencv-error-error-while-displaying-rectangle
场景可以归结为:

如果使用的是opencv 2.x的Python版

恰好我们有这样的场景:

img2 = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('img2',img2)


报错:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp, line 269
Traceback (most recent call last):
File "programs/test14.py", line 36, in <module>
cv2.imshow('img2',img2)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow

原因是:

这个版本的rectangle会返回一个None类型,接着调用imshow就会有问题

如果是 opencv 3.0可能就没有问题了(我没试过,应该是这样)

但是我们不想换版本,那么可以这样改代码

cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('img2',frame)

这样就ok了。

另:Python的这种语言特性,简单是简单了,不强调注意前后的数据类型,就会比较容易出现这种情况,明明出问题了,但是不知道是什么问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv Python
相关文章推荐