title: Understanding Hierarchy, from findContours opencv
body: <p>I am trying to understand Hierarchy and findContours, from opencv c++, but am finding it difficult.</p>
  
  <p>I did go through this question <a href="https://stackoverflow.com/questions/8461612/using-hierarchy-in-findcontours-in-opencv">here</a>, but I still am not able to get it clearly. I tried using it in different examples, using below sample images</p>
  
  <p><img src="https://i.stack.imgur.com/6damE.png" alt="enter image description here"></p>
  
  <p>for the above image, running the following code,</p>
  
  <pre><code>    #include "opencv2/highgui/highgui.hpp"
      #include "opencv2/imgproc/imgproc.hpp"
      #include &lt;iostream&gt;
      #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
  
      using namespace cv;
      using namespace std;
  
      Mat src; Mat src_gray;
      int thresh = 100;
      int max_thresh = 255;
      RNG rng(12345);
  
      void thresh_callback(int, void* );
  
      /** @function main */
      int main( int argc, char** argv )
      {
              src = imread( argv[1], 1 );
              cvtColor( src, src_gray, CV_BGR2GRAY );
              blur( src_gray, src_gray, Size(3,3) );
  
              String source_window = "Source";
              namedWindow( source_window, CV_WINDOW_AUTOSIZE );
              imshow( source_window, src );
  
              createTrackbar( " Canny thresh:", "Source", &amp;thresh, max_thresh, thresh_callback );
              thresh_callback( 0, 0 );
  
              waitKey(0);
              return(0);
      }
  
      /** @function thresh_callback */
      void thresh_callback(int, void* )
      {
              Mat canny_output;
              vector&lt;vector&lt;Point&gt; &gt; contours;
              vector&lt;Vec4i&gt; hierarchy;
              int rows = 0,cols = 0;
  
              Canny( src_gray, canny_output, thresh, thresh*3, 3 );
              findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
  
              for(int i=0;i&lt;hierarchy.size();i++)
              {
                      cout&lt;&lt;hierarchy[i]&lt;&lt;endl;
  
              }
      /*      for(int i =0;i&lt;contours.size();i++)
              {
                      for(int j = 0;j&lt;contours[i].size();j++)
                      {
                              cout&lt;&lt;"printing contours"&lt;&lt;endl;
                              cout&lt;&lt;contours[i][j]&lt;&lt;endl;
                      }
              }*/
  
              Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
              for( int i = 0; i&lt; contours.size(); i++ )
              {
                      Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
                      drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
              }
  
              /// Show in a window
              namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
              imshow( "Contours", drawing );
      }
  </code></pre>
  
  <p>The output I got for the above image for Hierarchy is as follows</p>
  
  <pre><code>    [6, -1, 1, -1]
      [-1, -1, 2, 0]
      [-1, -1, 3, 1]
      [-1, -1, 4, 2]
      [-1, -1, 5, 3]
      [-1, -1, -1, 4]
      [-1, 0, -1, -1]
  </code></pre>
  
  <p>According to the description from <a href="https://stackoverflow.com/questions/8461612/using-hierarchy-in-findcontours-in-opencv">here</a>, there are 7 contours. I do not understand how this is possible, when I see only 2 in the image. An outer contour and another one inside enclosed by the other.</p>
  
  <p>Secondly, What do the numbers, 6, 5, 3 denote? From reading the description, am not able to understand the underlying logic. It would be really helpful if someone can explain this.</p>
  
  <p>To remove doubts, I tried with various image types. Below is another one</p>
  
  <p><img src="https://i.stack.imgur.com/zlrdL.png" alt="enter image description here"></p>
  
  <p>and the answer for this is</p>
  
  <pre><code>      [2, -1, 1, -1]
        [-1, -1, -1, 0]
        [-1, 0, -1, -1]
  </code></pre>
  
  <p>Now we have 3 contours, while actually there's only one, as visible to naked eye.</p>
  
  <p>and finally, tried the worst case,</p>
  
  <p><img src="https://i.stack.imgur.com/on0mn.png" alt="enter image description here"></p>
  
  <p>And the output, </p>
  
  <pre><code>    [-1, -1, 1, -1]
      [-1, -1, -1, 0]
  </code></pre>
  
  <p>I know I am obviously missing some logic. If someone can explain, it would be of great help.</p>
  
  <p>I am trying to split the contours, based on their area, as large, medium and small, based on some threshold. I am stuck here , as I am trying to compute the area of a contour, but not able to understand it completely.</p>
  
  <p>Few things I can deduce is there's possibly two contour regions drawn for every possible single contour. One the boundary and the other the hole or space inside. Is it something like that?</p>
  
  <p>Still, the numbers of the hierarchy still confuse me.</p>
  
  <p>Any help would be really appreciated. Thanks in advance :).</p>