OpenCV断言失败:(-215:断言失败)npoints> = 0&& (深度== CV_32F ||深度== CV_32S) [英] OpenCV Assertion failed: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S)
问题描述
我在
当我尝试运行代码时,出现以下错误: When I try to run the code I get the following error: 我尝试在StackOverflow上寻找解决方案,但没有发现任何远程相似的地方. I've tried searching for a solution on StackOverflow, but I didn't find anything remotely similar. 编辑(请参阅评论): 这是做错了事:
我找到了原因.您关注的教程可能是在OpenCV 4发布之前发布的. OpenCV 3已更改
I found out the reason. Probably, the tutorial you are following was published before OpenCV 4 was released. OpenCV 3 changed 这篇关于OpenCV断言失败:(-215:断言失败)npoints> = 0&& (深度== CV_32F ||深度== CV_32S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋![INFO] processing image 1/9955
Traceback (most recent call last):
File "extract_single_letters_from_captchas.py", line 47, in <module>
(x, y, w, h) = cv2.boundingRect(contour)
cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:741: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'
type(contour[0])
= <class 'numpy.ndarray'>
len(contour)
= 4
推荐答案
contours = contours[0] if imutils.is_cv2() else contours[1]
imutils.is_cv2()
返回False
,即使它应该返回True
.如果您不希望删除此依赖项,请更改为:imutils.is_cv2()
is returning False
even though it should return True
. If you don't mind to remove this dependency, change to:contours = contours[0]
image, contours, hierarchy
,而 OpenCV 4的cv2.findContours(...)
返回contours, hierarchy
.因此,在OpenCV 4之前,可以正确地说,如果您使用OpenCV 2,则应该为contours[0]
,否则为contours[1]
.如果您仍然希望拥有这种兼容性",则可以更改为:
cv2.findContours(...)
to return image, contours, hierarchy
, while OpenCV 2's cv2.findContours(...)
and OpenCV 4's cv2.findContours(...)
return contours, hierarchy
. Therefore, before OpenCV 4, it was correct to say that if you use OpenCV 2 it should be contours[0]
else contours[1]
. If you still want to have this "compatibility", you can change to:contours = contours[1] if imutils.is_cv3() else contours[0]