First time here? Check out the FAQ!

Ask Your Question
2

convexityDefects errors

asked Apr 8 '0

TheCakeLover gravatar image

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

Preview: (hide)

Comments

+1 for te nice example !

berak gravatar imageberak (Apr 9 '0)edit

1 answer

Sort by » oldest newest most voted
2

answered Apr 8 '0

berak gravatar image

updated Apr 8 '0

from convexitydefects docs:

convexhull Convex hull obtained using convexHull that should contain indices of the contour points that make the hull.

you give it points, not indices

see last param of convexHull() (use returnPoints=False)

Preview: (hide)

Comments

Oh god, you can't imagine how often I read the documentation...thank you very much!

TheCakeLover gravatar imageTheCakeLover (Apr 10 '0)edit

Question Tools

1 follower

Stats

Asked: Apr 8 '0

Seen: 1,558 times

Last updated: Apr 08 '20