title: How to count pixels in color segment in OpenCV
body: <p>I have a OpenCV C++ application.
    I have segmented an image with <em>pyrMeanShiftFiltering</em> function.
    Now I need to count the pixel in a segment and the number of pixel having the most frequent value in the same segment in order to compute a ratio between them. How could I do that?</p>
  
  <p>I am using Tsukuba image and the code is.<br>
  <a href="https://i.stack.imgur.com/10IKu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/10IKu.jpg" alt="Tsukuba"></a>  </p>
  
  <pre><code>Mat image, segmented;
  image = imread("TsukubaL.jpg", 1 );
  pyrMeanShiftFiltering(image, segmented, 16, 32);
  </code></pre>
  
  <p>The segmented image is:<br>
  <a href="https://i.stack.imgur.com/jLxuy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jLxuy.png" alt="Segmented tsukuba"></a><br>
  If I consider a pixel in a single segment, the part where I count the pixel in that segment is:  </p>
  
  <pre><code>int cont=0;
  Vec3b x = segmented.at&lt;Vec3b&gt;(160, 136);
  for(int i = 160; i &lt; segmented.rows; ++i) { //check right-down
      for(int j = 136; j &lt; segmented.cols; ++j) {
          if(segmented.at&lt;Vec3b&gt;(i, j) == x)
              cont++;
          else
              continue;
      }
  }
  for(int i = 160; i &gt; 0; --i) {  //check right-up
      for(int j = 136; j &lt; segmented.cols; ++j) {
          if(segmented.at&lt;Vec3b&gt;(i, j) == x)
              cont++;
          else
              continue;
      }
  }
  for(int i = 160; i &lt; segmented.rows; ++i) { //check down-left
      for(int j = 136; j &gt; 0; --j) {
          if(segmented.at&lt;Vec3b&gt;(i, j) == x)
              cont++;
          else
              continue;
      }
  }
  for(int i = 160; i &gt; 0; --i) {  //check up-left
      for(int j = 136; j &gt; 0; --j) {
          if(segmented.at&lt;Vec3b&gt;(i, j) == x)
              cont++;
          else
              continue;
      }
  }
  cout&lt;&lt;"Pixel "&lt;&lt;x&lt;&lt;"cont = "&lt;&lt;cont&lt;&lt;endl;  
  </code></pre>
  
  <p>In this example, I consider a white pixel in position (160, 136) and count the same pixel to the central one in the four direction starting from it, and the output is: </p>
  
  <blockquote>
    <p>Pixel [206, 222, 240]cont = 127  </p>
  </blockquote>
  
  <p>Could it be a possible good way to do it?</p>