counting the number of child contours inside a contour in opencv
<p>I have this source image</p>

<p><a href="https://i.stack.imgur.com/fHNe8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fHNe8.png" alt="enter image description here"></a></p>

<p>and I have applied binary thresholding to get this</p>

<p><a href="https://i.stack.imgur.com/C2TEX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C2TEX.png" alt="enter image description here"></a></p>

<p>I used contours to differentiate between the ones having child contours and ones that don't.The resultant pic is</p>

<p><a href="https://i.stack.imgur.com/YZ3UE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YZ3UE.png" alt="enter image description here"></a></p>

<p>But how do I count the number of child contours that each green contour contains?. This is code I have used:-</p>

<pre><code>Mat binMask = lung;// the thresholded image
Mat lung_src = imread("source.tiff");// the source image
//imshow("bin mask", binMask);
vector&lt;std::vector&lt;cv::Point&gt;&gt; contours;
vector&lt;cv::Vec4i&gt; hierarchy;
int count = 0, j;

double largest_area = 0;
int largest_contour_index = 0;

findContours(binMask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

for (int i = 0; i &lt; contours.size(); i++)
{
    double a = contourArea(contours[i], false);  //  Find the area of contour
    if (a&gt;largest_area)
    {
        largest_area = a;
        largest_contour_index = i;
    }
    for (j = 0; j &lt;= i; j++)
    {
        if (hierarchy[j][2] != -1) // means it has child contour
        {

                drawContours(lung_src, contours, j, Scalar(0, 255, 0), 1, 8, hierarchy, 0, Point());

        }           
        else  // means it doesn't have any child contour
        {
            drawContours(lung_src, contours, j, Scalar(0, 0, 255), 1, 8, hierarchy, 0, Point());
        }
    }
}
drawContours(lung_src, contours, largest_contour_index, Scalar(255, 0, 0), 1, 8, hierarchy, 0, Point());
imshow("lung-mapped", lung_src);
</code></pre>

<p>EDIT-1- I added the code from Humam at the end to check it out:</p>

<pre><code>std::vector&lt;int&gt; number_of_inner_contours(contours.size(), -1);
int number_of_childs = 0;
for (size_t i = 0; i &lt; contours.size(); i++)
{

    int first_child_index = hierarchy[i][2];
    if (first_child_index &gt;= 0)
    {
        int next_child_index = hierarchy[first_child_index][0];
        if (number_of_inner_contours[next_child_index]&lt;0)
        {
            number_of_childs = number_of_inner_contours[next_child_index];
        }
        else
        {
            while (next_child_index &gt;= 0)
            {
                next_child_index = hierarchy[next_child_index][0];
                ++number_of_childs;
            }
            number_of_inner_contours[i] = number_of_childs;
        }
    }
    else
    {
        number_of_inner_contours[i] = 0;
    }
    cout &lt;&lt; "\nThe contour[" &lt;&lt; i &lt;&lt; "] has " &lt;&lt; number_of_inner_contours[i] &lt;&lt; "child contours";
}
</code></pre>

<p>But the output I got was like : </p>

<pre><code>      The contour[456 ] has 0 child contours
      The contour[457 ] has 0 child contours
      The contour[458 ] has 0 child contours
      The contour[459 ] has -1 child contours
</code></pre>