python报错error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in funct

x, y, w, h = cv2.boundingRect(np.array(cnt))

在执行以上代码时,出现以下错误:
error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-k1ohfcms\opencv\modules\imgproc\src\shapedescr.cpp:874: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function ‘cv::pointSetBoundingRect’
修改为以下语句可以解决问题:

x, y, w, h = cv2.boundingRect(np.array(cnt[Index]))

报错原因与 cv2.findContours函数的返回值有关。
findContours函数返回两个参数,contoursheridency
如果直接把contours参数传入cv2.boundingRect函数,会因为contours维数比需要的维数超出一维。导致以上错误。
解决方法为在contours后指定某个切片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值