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

感觉国内这种资料真的很少,最后在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的这种语言特性,简单是简单了,不强调注意前后的数据类型,就会比较容易出现这种情况,明明出问题了,但是不知道是什么问题

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值