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