title: Floodfill function in OpenCV
body: <p>What is the meaning of floating range and fixed range given in the <a href="https://docs.opencv.org/4.5.1/d7/d1b/group__imgproc__misc.html#ga366aae45a6c1289b341d140839f18717" rel="nofollow noreferrer">documentation of floodfill function</a>??</p>
  <p>I used the floodfill function to a grayscale image shown below. The image has three regions of varying intensities.</p>
  <p><img src="https://i.stack.imgur.com/cL1pl.png" alt="input image" /></p>
  <ol>
  <li>outer rectangle = 170</li>
  <li>inner ellipse = 175</li>
  <li>inner rectangle = 180</li>
  </ol>
  <p>I want to floodfill the regions of 170 and 175 together as single connected component and region with 180 as separate one.</p>
  <p>I modified the  <a href="http://nghiaho.com/?p=1102" rel="nofollow noreferrer">code from here</a> and function as follows:</p>
  <pre><code>  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
  void FindBlobs(const cv::Mat &amp;binary, std::vector &lt; std::vector&lt;cv::Point2i&gt; &gt; &amp;blobs);
  
  int main(int argc, char **argv)
  {
      cv::Mat img = cv::imread(&quot;blob.png&quot;, 0); // force greyscale
  
      if(!img.data) {
          std::cout &lt;&lt; &quot;File not found&quot; &lt;&lt; std::endl;
          return -1;
      }
  
      cv::namedWindow(&quot;binary&quot;);
      cv::namedWindow(&quot;labelled&quot;);
  
      cv::Mat output = cv::Mat::zeros(img.size(), CV_8UC3);
  
      cv::Mat binary=img.clone();
      std::vector &lt; std::vector&lt;cv::Point2i &gt; &gt; blobs;
          
      FindBlobs(binary, blobs);
  
      // Randomy color the blobs
      for(size_t i=0; i &lt; blobs.size(); i++) {
          unsigned char r = 255 * (rand()/(1.0 + RAND_MAX));
          unsigned char g = 255 * (rand()/(1.0 + RAND_MAX));
          unsigned char b = 255 * (rand()/(1.0 + RAND_MAX));
  
          for(size_t j=0; j &lt; blobs[i].size(); j++) {
              int x = blobs[i][j].x;
              int y = blobs[i][j].y;
  
              output.at&lt;cv::Vec3b&gt;(y,x)[0] = b;
              output.at&lt;cv::Vec3b&gt;(y,x)[1] = g;
              output.at&lt;cv::Vec3b&gt;(y,x)[2] = r;
          }
      }
  
      cv::imshow(&quot;binary&quot;, img);
      cv::imshow(&quot;labelled&quot;, output);
      cv::waitKey(0);
  
      return 0;
  }
  
  void FindBlobs(const cv::Mat &amp;binary, std::vector &lt; std::vector&lt;cv::Point2i&gt; &gt; &amp;blobs)
  {
      blobs.clear();
  
      cv::Mat label_image;
      binary.convertTo(label_image, CV_32FC1); 
  
      int label_count = 2; 
  
      for(int y=0; y &lt; binary.rows; y++) {
      {
       for(int x=0; x &lt; binary.cols; x++) {
           {   if((int)label_image.at&lt;float&gt;(y,x) &lt; 150) {  //start labelling only when pixel &gt; 150
               {
           continue;
               }
  
              cv::Rect rect;
              cv::floodFill(label_image, cv::Point(x,y), cv::Scalar(label_count), &amp;rect, cv::Scalar(0), cv::Scalar(6), 4+CV_FLOODFILL_FIXED_RANGE);
  
              std::vector &lt;cv::Point2i&gt; blob;
  
              for(int i=rect.y; i &lt; (rect.y+rect.height); i++) {
              {   for(int j=rect.x; j &lt; (rect.x+rect.width); j++) {
                  {   if((int)label_image.at&lt;float&gt;(i,j) != label_count) {
                      {    continue;
                      }
  
                      blob.push_back(cv::Point2i(j,i));
                  }
              }
  
              blobs.push_back(blob);
  
              label_count++;
          }
      }
  }
  </code></pre>
  <p>I used fixed range using the flag CV_FLOODFILL_FIXED_RANGE(<strong>is it correct the way I used??</strong>)</p>
  <p>I specify the <strong>loDiff=0 and upDiff=6</strong>.</p>
  <p>I <strong>expected</strong>  that when seed becomes 170, all points in range 170-0 to 170+6 ie <strong>170 to 176</strong> (<strong>outer rectangle &amp; inner ellipse</strong>) are floodfilled with same label and since inner rectangle is 180, it would have different label.</p>
  <p>However I get the output as below:-</p>
  <p><img src="https://i.stack.imgur.com/CdHHZ.png" alt="enter image description here" /></p>
  <p><strong>the outer rectangle and inner ellipse are not having the same label</strong>. What might be the mistake?</p>
  <p>expected o/p : inner ellipse also be of orange color(same as outer rectangle)</p>