error: (-215:Assertion failed) n

2019-05-21  本文已影响0人  林木_lker

最近在用opencv做图片差异比较,使用的是网上的示例,其中有一段获取不同点的轮廓时报错了。
代码:

thresh = cv2.threshold(diff,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

报错信息:
Traceback (most recent call last):
File "diff-image.py", line 27, in <module>
(x,y,w,h) = cv2.boundingRect(c)
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:743: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'

最后在stackoverflow上找到了解决办法,原来是opencv版本问题造成的。
解决办法:

thresh = cv2.threshold(diff,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[1] if imutils.is_cv3() else cnts[0]

详细问题解决描述
https://stackoverflow.com/questions/54734538/opencv-assertion-failed-215assertion-failed-npoints-0-depth-cv-32

上一篇下一篇

猜你喜欢

热点阅读