Contour hierarchy issue with cv2.RETR_CCOMP: Circle inside square not recognized as child

I’m using cv2.findContours with cv2.RETR_CCOMP to get the contour hierarchy. Both the circle and the square are detected as contours. Since these are just outlines, each shape has both an inner and an outer contour, meaning the circle has both a parent and a child, and the square does as well.

However, even though the circle is inside the square, it is not recognized as its child in the hierarchy. Has anyone encountered a similar issue and found a solution?

CCOMP itself doesn’t result in real hierarchy, only a 2-level one (outer, inner). TREE does give the full hierarchy.

RETR_CCOMP
Python: cv.RETR_CCOMP
retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.

1 Like