Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can change findcontour() mode parameter to CV_RETR_EXTERNAL which will retrieves only the extreme outer contours, and sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours. That is CV_RETR_EXTERNAL will exclude all child contour(contour inside contour).

And if your code need to extract all contour(including child) you can iterate through only outer contour(first hierarchy level) using below code,

for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through outer contour contour exclude child

Also see this question, might be helpful.

You can change findcontour() mode parameter to CV_RETR_EXTERNAL which will retrieves only the extreme outer contours, and sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours. That is CV_RETR_EXTERNAL will exclude all child contour(contour inside contour).

And if your code need to extract all contour(including child) you can iterate through only outer contour(first hierarchy level) using below code,

for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through outer contour  contour exclude child

Also see this question, might be helpful.