This is example image:
and I use opencv to detect contours:
>>> fc = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
>>> contours = fc[0]
For detecting closed contour I thought to check start and end points in each contour returned by opencv, while I noticed that regardless the shape of object opencv seem to outline each object, so I get this result:
>>> for contour in contours:
>>> print(contour[0,:,:], contour[-1,:,:])
[[246 38]] [[247 38]]
[[92 33]] [[93 33]]
or each found contour has closed path.
I searched for additional constants to available methods in findContour()
function, but it seems all return closed paths.
So is there some general way of detecting if found contour is closed?
I googled before asking and didn't got results, but I see good candidate in similar questions at the right side: How can i know if a contour is open or closed in opencv? where it is suggested I use cv2.isContourConvex(contour)
, but:
>>> for contour in contours:
>>> print(cv2.isContourConvex(contour))
False
False
yet another update: contourArea looks like it may provide answer (at least for simple contours) but I didn't tested on anything else then above example image:
>>> for contour in contours:
>>> print(cv2.contourArea(contour))
0.0
12437.5
convexHull()
? likecontourhulled=cv2.convexHull(contour); cv2.isContourConvex(contourhulled)
.