title: How to segment objects after OpenCV connectedComponents function
body: <p>I have obtained a labeling with the <code>connectedComponents</code> function of C++ OpenCV, which looks like in the picture :</p>
  
  <p><a href="https://i.stack.imgur.com/1D0EV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1D0EV.png" alt="enter image description here"></a></p>
  
  <p>This is the output of the <code>ccLabels</code> variable, which is a <code>cv::Mat</code> of the same size with the original image.</p>
  
  <p>So what I need to do is : </p>
  
  <ol>
  <li>Count the occurences of each number, and select the ones that
  occur more than <code>N</code> times, which are the "big" ones.</li>
  <li>Segment the
  areas of the "big" components, and then count the number of 4's and
  0's inside that area.</li>
  </ol>
  
  <p>My ultimate aim is to count the number of holes in the image, so I aim to infer number of holes from <code>(number of 0's / number of 4's)</code>. This is probably not the prettiest way but the images are very uniform in terms of size and illumination, so it will meet my needs.</p>
  
  <p>But I'm new to OpenCV and I don't have much idea how to accomplish this task.</p>
  
  <p>Here is what I've done so far:</p>
  
  <pre><code>cv::Mat1b outImg;
  cv::threshold(grayImg, outImg, 150, 255, 0); // Thresholded -binary- image
  cv::Mat ccLabels;
  cv::connectedComponents(outImg, ccLabels); // Each non-zero pixel is labeled with their connectedComponent ID's
  // write the labels to file:
  std::ofstream myfile;
      myfile.open("ccLabels.txt");
      cv::Size s = ccLabels.size();
      myfile &lt;&lt; "Size: " &lt;&lt; s.height &lt;&lt; " , " &lt;&lt; s.width &lt;&lt;"\n";
      for (int r1 = 0; r1 &lt; s.height; r1++) {
          for (int c1 = 0; c1 &lt; s.height; c1++) {
              myfile &lt;&lt; ccLabels.at&lt;int&gt;(r1,c1);
          }
          myfile &lt;&lt; "\n";
      }
      myfile.close();
  </code></pre>
  
  <p>Since I know how to iterate inside the matrix, counting the numbers should be OK, but first I have to separate(eliminate / ignore) the "background" pixels, which are the 0's <strong>outside</strong> the connected components. Then counting should be easy. </p>
  
  <p>How can I segment these "big" components? Maybe obtaining a mask, and only consider pixels where <code>mask(x,y) = 1</code>?</p>
  
  <p>Thanks for any help !</p>
  
  <h2>Edit</h2>
  
  <p>This is the thresholded image: </p>
  
  <p><a href="https://i.stack.imgur.com/xZXYs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xZXYs.png" alt="enter image description here"></a></p>
  
  <p>And this is what I get after Canny edge detection : </p>
  
  <p><a href="https://i.stack.imgur.com/8zDCR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8zDCR.png" alt="enter image description here"></a></p>
  
  <p>This is the actual image (thresholded) : 
  <a href="https://i.stack.imgur.com/AuiYQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AuiYQ.png" alt="enter image description here"></a></p>