代码之家  ›  专栏  ›  技术社区  ›  purva

错误:(-215:断言失败)函数“cv::convoxityDefects”中的npoints>=0

  •  0
  • purva  · 技术社区  · 3 年前
     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'
    

    我试图通过确保轮廓和船体变量具有有效值并且它们的长度大于或等于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
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Odeaxcsh    3 年前

    我已经检查了的源代码 cv::convexityDefects 错误可能来自 npoints = points.checkVector(2, CV_32S);

    您必须将变量作为有符号的32位整数(np.int32)和正确的形状(N,2)传递。