title: How does FindContours cycle through the image? OpenCV 2.3
body: <p>We are writing a program that takes input from a webcam, substracts all color except the green values, finds the seperated BLOBs and numerates them. Eventually, this will be used as an input for a video game, but that is irrelevent right now.</p>
  
  <p>The code in question is not the code that actually does all of this, but instead, a rewritten segment of the code to test how FindContours actually works. 
  Usually, with imageprocessing, we have been tought that the image is read from top left to bottom right, but after some testing, it seems that it does the exact opposite, starting at the lower right corner and moving to the upper left!</p>
  
  <p>So the question here is: In which order does FindContours find it's Contours? Am I right in my assumption or is it my own code confusing me? </p>
  
  <p>Input: <a href="http://i32.photobucket.com/albums/d37/Ethalox/Blobtest06.png" rel="nofollow">Blobtest06</a></p>
  
  <p><a href="http://i32.photobucket.com/albums/d37/Ethalox/Components.jpg" rel="nofollow">"Components" window</a></p>
  
  <p><a href="http://i32.photobucket.com/albums/d37/Ethalox/Console.jpg" rel="nofollow">Console</a></p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;opencv2/core/mat.hpp&gt;
  #include &lt;Windows.h&gt; //for sleep function
  
  using namespace cv;
  using namespace std;
  
  void IsolateGreen(Mat mIn, Mat&amp; mOut)
  {
  Mat inImg (mIn.rows, mIn.cols, CV_8UC3, Scalar(1,2,3));
  inImg.data = mIn.data;
  Mat channelRed (inImg.rows, inImg.cols, CV_8UC1);
  Mat channelGreen (inImg.rows, inImg.cols, CV_8UC1);
  Mat channelBlue (inImg.rows, inImg.cols, CV_8UC1);
  Mat outImg[] = {channelRed, channelGreen, channelBlue};
  
  int fromTo[] = { 0,2, 1,1, 2,0};
  mixChannels( &amp;inImg, 1, outImg, 3, fromTo, 3);
  
  mOut = (channelGreen) - (channelRed + channelBlue);
  
  threshold(mOut, mOut, 5, 255, THRESH_BINARY);
  
  erode(mOut, mOut, Mat(), Point (-1,-1), 1);
  dilate(mOut, mOut, Mat(), Point(-1,-1), 2);
  
  }
  
  void FindContours(Mat&amp; mDst, Mat mGreenScale, vector&lt;vector&lt;Point&gt;&gt;&amp; vecContours, vector&lt;Vec4i&gt;&amp; vecHierarchy, Mat img)
  {
  //This is empty at all times. We need it to avoid crashes.
  vector&lt;Vec4i&gt; vecHierarchy2; 
  
  // mGreenScale = mGreenScale &gt; 1; //// MIGHT be entirely unneeded 
  
  mDst = img &gt; 1;
  findContours( mGreenScale, vecContours, vecHierarchy,
          CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  
  /* Colors, in order:
  1st. = Red
  2nd. = Dark red
  3rd. = Purple
  4th. = Blue
  5th. = Baby blue
  6th. = Green
  7th. = Olive green
  8th. = Dark green
  */
  
  int aRed[] = {255, 100, 128, 0, 191, 0, 202, 0};
  int aGreen[] = {0, 0, 0, 0, 239, 255, 255, 100};
  int aBlue[] = {0, 0, 128, 255, 255, 0, 112, 0};
  string sColor[] = {"Red", "Dark red", "Purple", "Blue", "Baby blue", "Green", "Light green", "Dark green"};
  
  //its important that we check if there is anything in vecHierarchy (else) {crash} :P
  //function drawContours cannot handle an empty vedHierarchy
  if (vecHierarchy != vecHierarchy2)
  {
      // iterate through all the top-level contours,
      for(int idx = 0; idx &gt;= 0; idx = vecHierarchy[idx][0] )
      { 
          // draw each connected component with its own FIXED color
          Scalar color( aBlue[idx], aGreen[idx], aRed[idx] );
          drawContours( mDst, vecContours, idx, color, /*1*/ CV_FILLED, 8, vecHierarchy );
          cout &lt;&lt; vecContours[idx][0] &lt;&lt; " - - " &lt;&lt; sColor[idx] &lt;&lt; " - - Index: " &lt;&lt; idx &lt;&lt; endl;
      }
  }
  cout &lt;&lt; "Objects: ";
  cout &lt;&lt; vecContours.size();
  cout &lt;&lt; endl;
  
  }
  
  
  
  
  int main()
      {
      Mat img = imread("Blobtest06.png");
      Mat mGreenScale;
  
      //These next 5 instances ties to contourfinding                 
      cvtColor(img, mGreenScale, CV_8UC3); //sets the right rows and cols
      vector&lt;vector&lt;Point&gt;&gt; vecContours; //points to each pixel in a contour
      vector&lt;Vec4i&gt; vecHierarchy; //A hierarchy for the functions
      Mat mDst = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3); //mDst image
  
  
      IsolateGreen(img, mGreenScale);
      FindContours(mDst, mGreenScale, vecContours, vecHierarchy, img);
  
      namedWindow( "Components", 1 );
      imshow( "Components", mDst );
      namedWindow( "Source", 1 );
      imshow( "Source", mGreenScale );
  
      waitKey();  
      return 0;
      } 
  </code></pre>
  
  <p>PS: Sorry for horrible syntax. The site is being difficult and it's just about lunchtime.</p>