Ask Your Question
2

convexityDefects errors

asked 2020-04-08 03:47:43 -0600

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

edit retag flag offensive close merge delete

Comments

+1 for te nice example !

berak gravatar imageberak ( 2020-04-09 03:30:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-04-08 06:29:23 -0600

berak gravatar image

updated 2020-04-08 06:33:09 -0600

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)

edit flag offensive delete link more

Comments

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

TheCakeLover gravatar imageTheCakeLover ( 2020-04-10 01:54:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-08 03:47:43 -0600

Seen: 1,435 times

Last updated: Apr 08 '20