Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

convexityDefects errors

Dear all,

I am constantly running into problems using convexityDefects(). I have created an example image (see below), and OpenCV correctly finds all contours and convex hulls, but still, when running the line of code I commented out it throws the error

error: (-215:Assertion failed) hpoints > 0 in function 'cv::convexityDefects'

Where is the error? Is it related to the image depth?

import cv2
import imutils
import numpy as np

black = np.zeros((500,500), dtype=np.uint8)
cv2.rectangle(black, (60,60), (440, 440), (255,255,255), 4)
poly = np.array([[80,80], [110, 110], [80, 200], [300, 110]])
cv2.drawContours(black, [poly], 0, (255, 255, 255), 3)
cnts = cv2.findContours(black, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)



for c in cnts:
    toShow = cv2.cvtColor(black.copy(), cv2.COLOR_GRAY2BGR)
    cv2.drawContours(toShow, [c], 0, (0,255,0), 2)
    hull = cv2.convexHull(c)
    cv2.drawContours(toShow, [hull], 0, (0,0,255), 1)

  # print("conv defects", cv2.convexityDefects(c, hull))

    cv2.imshow("cont", toShow)
    cv2.waitKey()

test image

test image

convex hull

inner contour with correct convex hull