opencv边缘检测报错15: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S)

opencv边缘检测报错

这个问题困扰我好几天了

我一直以为是图片路径问题,搞得我一直在重装python,最后发现是OpenCV边缘检测报错,通过这个博客发现修改如下就没报错了,记录下,好气呀

 

 

def preProcess(image):
    ratio = image.shape[0] / 500.0
    image = imutils.resize(image, height=500)
    grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gaussImage = cv2.GaussianBlur(grayImage, (5, 5), 0)
    edgedImage = cv2.Canny(gaussImage, 75, 200)

    cnts = cv2.findContours(edgedImage.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]

    for c in cnts:
        peri = cv2.arcLength(c, True)  # Calculating contour circumference
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        if len(approx) == 4:
            screenCnt = approx
            break

    return screenCnt, ratio


报错:

Traceback (most recent call last):
  File "G:/Pythonproject/test1.py", line 110, in <module>
    screenCnt, ratio = preProcess(image)
  File "G:/Pythonproject/test1.py", line 95, in preProcess
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\opencv\modules\imgproc\src\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'
Process finished with exit code 1
解决方法:
  这里的操作有误:
  cnts = cnts[0] if imutils.is_cv2() else cnts[1]

      如果你仍然想使用它, 你可以将它改为:
  cnts = cnts[1] if imutils.is_cv3() else cnts[0]

转载于:https://www.cnblogs.com/wandaoyi/p/11544079.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值