title: OpenCV 2.3: How to put all holes for each contour into subvector?
body: <p>for each contour I want to put its holes into separate vector&lt; of holes>. By default in OpenCV 2.* we have 2 options - use OpenCV 1.* tree structure and iterate through it as <a href="http://jmpelletier.com/a-simple-opencv-tutorial/" rel="nofollow">shown in this tutorial</a>, of get our head around the <code>vector&lt;Vec4i&gt;</code> which is a bit simplified but same OpenCV 1.0 alike tree structure which relies on <code>std::vector&lt; std::vector&lt;Point&gt; &gt;</code> you can get from <code>findContours</code>. Tree is grate when you need to know inheritance - "what is placed inside of what". But often you really need just a set of areas and its holes. (for vectorization and similar tasks).</p>
  
  <p>Currently I have such code:</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  
  using namespace cv;
  int main(  )
  {
      Mat black = Mat::zeros(Size(100, 100), CV_8UC1);
      for(int i = 0; i &lt;= 35; ++i)
      {
          for (int k = 0; k &lt;=35; ++k)
          {
              black.row(i).col(k) = 255;
          }
      }
  
      for(int i = 10; i &lt;= 15; ++i)
      {
          for (int k = 10; k &lt;=15; ++k)
          {
              black.row(i).col(k) = 0;
          }
      }
  
      for(int i = 25; i &lt;= 30; ++i)
      {
          for (int k = 25; k &lt;=30; ++k)
          {
              black.row(i).col(k) = 0;
          }
      }
  
      for(int i = 45; i &lt;= 75; ++i)
      {
          for (int k = 45; k &lt;= 75; ++k)
          {
              black.row(i).col(k) = 255;
          }
      }
  
      for(int i = 55; i &lt;= 65; ++i)
      {
          for (int k = 55; k &lt;=65; ++k)
          {
              black.row(i).col(k) = 0;
          }
      }
  
      for(int i = 57; i &lt;= 62; ++i)
      {
          for (int k = 57; k &lt;=62; ++k)
          {
              black.row(i).col(k) = 255;
          }
      }
  
      namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
      imshow("Display Image", black);
  
      std::vector&lt; std::vector&lt;Point&gt; &gt; contours;
  vector&lt;Vec4i&gt; hierarchy;
  if(sum(black).val[0] &gt; 0.0)
  {
      findContours(black, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
  }
  else
  {
      std::cout &lt;&lt; "It's a black image, so I'm not going to do anything..." &lt;&lt; std::endl;
  }
  
  for(int i = 0; i &lt; contours.size(); ++i)
  {
      std::cout &lt;&lt; "counter " &lt;&lt; i &lt;&lt; ":" &lt;&lt; std::endl;
      std::cout &lt;&lt;  hierarchy[i][0]  &lt;&lt; " "  &lt;&lt; hierarchy[i][1] &lt;&lt; " " &lt;&lt;  hierarchy[i][2]  &lt;&lt; hierarchy[i][3]  &lt;&lt; std::endl;
  
      for (int k = 0; k &lt; contours[i].size(); ++k)
      {
          std::cout &lt;&lt; "\t point " &lt;&lt; k &lt;&lt; " x: " &lt;&lt; contours[i][k].x &lt;&lt; "; y: " &lt;&lt;  contours[i][k].y &lt;&lt; std::endl;
      }
  }
  
      waitKey(0);
      std::cin.get();
      return 0;
  }
  </code></pre>
  
  <p>I wonder is it possible to change   <code>std::vector&lt; std::vector&lt;Point&gt; &gt; contours;</code> to something like <code>std::vector&lt; std::pair&lt;std::vector&lt;Point&gt;, std::vector&lt;std::vector&lt;Point&gt; &gt; &gt; contours;</code> where pair consists of contour : vector of its holes. Is it possible and how to implement such thing with openCV 2.3?</p>
  
  <h1>Update:</h1>
  
  <p>What I meant was something like this (bad, bad C style...) code snippet I created:</p>
  
  <pre><code>struct vector_object
  {
      std::vector&lt;Point&gt; border;
      std::vector&lt; std::vector&lt;Point&gt; &gt; holes;
  };
  
  std::vector&lt;vector_object&gt; container;
  
  void store_vector_hole(std::vector&lt; std::vector&lt;Point&gt; &gt; &amp;holes, std::vector&lt; std::vector&lt;Point&gt; &gt; &amp; contours, std::vector&lt;Vec4i&gt; &amp; hierarchy, const int &amp; cid);
  void store_vectror_countors(std::vector&lt; std::vector&lt;Point&gt; &gt; &amp; contours, std::vector&lt;Vec4i&gt; &amp; hierarchy, const int &amp; cid);
  
  
  void store_vector_hole(std::vector&lt; std::vector&lt;Point&gt; &gt; &amp;holes, std::vector&lt; std::vector&lt;Point&gt; &gt; &amp; contours, std::vector&lt;Vec4i&gt; &amp; hierarchy, const int &amp; cid)
  {
      holes.push_back(contours[cid]);
      if (hierarchy[cid][2] &gt;= 0) // holes in holes == real poligons
      {
          store_vectror_countors( contours, hierarchy, hierarchy[cid][2]);
      }
      if (hierarchy[cid][0] &gt;= 0) // holes on same level of depth == another holes of this poligon
      {
          store_vector_hole(holes, contours, hierarchy, hierarchy[cid][0]);
      }
      return;
  
  }
  void store_vectror_countors(std::vector&lt; std::vector&lt;Point&gt; &gt; &amp; contours, std::vector&lt;Vec4i&gt; &amp; hierarchy, const int &amp; cid)
  {
      if (hierarchy[cid][0]&gt;= 0)
          store_vectror_countors(contours, hierarchy, hierarchy[cid][0]);
  
      vector_object current;
      current.border = contours[cid];
  
      if ((hierarchy[cid][0]&lt;= 0) &amp;&amp; (hierarchy[cid][2] &lt;= 0))
      {
              container.push_back(current);
              return;
      }
  
      if(hierarchy[cid][2] &gt;= 0) // h
      {
          store_vector_hole(current.holes, contours, hierarchy, hierarchy[cid][2]);
      }
  
      container.push_back(current);
      return;
  
  }
  </code></pre>
  
  <p>that you activate via:</p>
  
  <pre><code>//...
  findContours(black, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
  store_vectror_countors(contours, hierarchy, 0);
  </code></pre>
  
  <p>Any Ideas on making it more robust and C++ish?</p>