# Contour hierarchy issue with cv2.RETR\_CCOMP: Circle inside square not recognized as child

**URL:** <https://forum.opencv.org/t/contour-hierarchy-issue-with-cv2-retr-ccomp-circle-inside-square-not-recognized-as-child/20632>\
**Category:** Python\
**Created:** [March 25, 2025, 10:22pm UTC](https://forum.opencv.org/t/contour-hierarchy-issue-with-cv2-retr-ccomp-circle-inside-square-not-recognized-as-child/20632 "2025-03-25T22:22:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Lukowicz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pawel_lukowicz/32/12242_2.png) [@Pawel\_Lukowicz](https://forum.opencv.org/u/Pawel_Lukowicz)\
**Post date:** [March 25, 2025, 10:22pm UTC](https://forum.opencv.org/t/contour-hierarchy-issue-with-cv2-retr-ccomp-circle-inside-square-not-recognized-as-child/20632/1 "2025-03-25T22:22:59Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d49853bfd09d529b4b0506afc3e71a092160ce10.png)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 25, 2025, 10:32pm UTC](https://forum.opencv.org/t/contour-hierarchy-issue-with-cv2-retr-ccomp-circle-inside-square-not-recognized-as-child/20632/2 "2025-03-25T22:32:44Z")

</div>

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.

> **[OpenCV: Structural Analysis and Shape Descriptors](https://docs.opencv.org/4.x/d3/dc0/group __imgproc__ shape.html#ga819779b9857cc2f8601e6526a3a5bc71)**
