昨天在使用OpenCV-Python时报错:OpenCV Assertion failed: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S),经过一番搜索,在stackoverflow上找到了答案,主要是版本问题,错误代码如下:
contours = contours[0] if imutils.is_cv2() else contours[1]
此处只能用于判断CV2的版本是2还是3,而如今的最新版本是4.1.1,OpenCV2和OpenCV4的返回值都为contours, hierarchy,OpenCV3返回值是image, contours, hierarchy,因此如果你的代码是上面的代码,你需要得到imutils.is_cv2是True,但你用的版本是OpenCV4,得到的是False,所以这里会报错
那么我们可以将代码改为contours = contours[1] if imutils.is_cv3() else contours[0],
这用我们只要判断其版本是否是OpenCV3,无论版本是2还是4都能得到正确的返回,可以兼容目前的三个版本。
如果没有看明白,点这里看原文答案。
OpenCV Assertion failed: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S
最新推荐文章于 2022-10-10 18:51:36 发布