Ask Your Question

Revision history [back]

You want to take advantage of the information that findContours will put in hierarchy when using CV_RETR_TREE.

If you don't mind adapting information from a Python tutorial, there's an introduction here.

The gist is that each entry in the contours list will have a corresponding entry in the hierarchy list of the form [Next, Previous, First_Child, Parent], where each value is an index into contours.

To find only internal contours, skip entries where the Parent value is -1.

Source: I just did the opposite (keep only Parent=-1) as a way to close large holes in a flood fill without altering the exterior boundaries. (As a proof of concept before switching to RETR_EXTERNAL.)