opencv 错误:(-215:Assert失败)npoints >= 0 in function 'cv::convexityDefects'

hs1ihplo  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(366)
defects = cv2.convexityDefects(cnt, hull)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\convhull.cpp:319: error: (-215:Assertion failed) npoints >= 0 in function 'cv::convexityDefects'

字符串
我尝试通过确保contours和船体变量具有有效值并且它们的长度大于或等于3来解决这个问题,然后将它们传递给cv2.convexityDefects函数。

if cnt is not None and hull is not None and len(cnt) >= 3 and len(hull) >= 3:
    defects = cv2.convexityDefects(cnt, hull)
else:
    print("Invalid contour or not enough points in the contour or hull.")
    defects = None

# finding convexity defects
defects = cv2.convexityDefects(cnt, hull)
count_defects = 0
cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3)  # to draw all contours pass -1

rnmwe5a2

rnmwe5a21#

我已经检查了cv::convexityDefects的源代码,错误可能来自npoints = points.checkVector(2, CV_32S);
必须将变量作为有符号32位整数(np.int32)和正确的形状(N,2)传递。

相关问题