title: seeded region growing segmentation opencv
body: <p>I am trying to write a code on seeded region growing segmentation in openCV.
  I have taken this [[1]: <a href="https://stackoverflow.com/questions/14416511/seeded-region-growing-with-opencv%5d%5b1%5d">https://stackoverflow.com/questions/14416511/seeded-region-growing-with-opencv][1]</a> code as a reference and tried to built a iterative based approach. </p>
  
  <pre><code>    #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;stdio.h&gt;
  
  int xDim,yDim; 
  unsigned long total[3]; 
  int coont;
  int Diff, mean[3], temp[3];
  static int count = 0;
  int Threshold = 45;
  IplImage *Image1;
  IplImage *Image2;
  CvScalar s = {0,0,0,0};
  CvScalar s11 = {0,0,0,0};
  
  void GrowColor(int,int);
  
  
  int main(int argc, char *argv[])
  {
  int  x, y,Xseed = 40,Yseed = 243;
  total[0] = total[1] = total[2] = coont = 0;
  
  
  Image1 = cvLoadImage("lena.jpg",CV_LOAD_IMAGE_UNCHANGED );
  yDim = Image1-&gt;height;
  xDim = Image1-&gt;width;
  IplImage* img = cvCreateImage(cvGetSize(Image1), Image1-&gt;depth , Image1-&gt;nChannels );
  
  Image2 = cvCreateImage(cvGetSize(Image1), Image1-&gt;depth , Image1-&gt;nChannels );
  cvZero( Image2 );
  
  for (y = Yseed - 5; y &lt;= Yseed + 5; y++)
       for (x = Xseed - 5; x &lt;= Xseed + 5; x++)
         if ((x &gt; 0) &amp;&amp; (y &gt; 0) &amp;&amp; (x &lt; xDim) &amp;&amp; (y &lt;yDim))
           {
            coont++;
            s = cvGet2D(Image1,y,x);
            total[0] += (s.val[0]);  
            total[1] += (s.val[1]);
            total[2] += (s.val[2]);
            }
  
  for (y = 0; y &lt; yDim; y++)
       for (x = 0; x &lt; xDim; x++)
           GrowColor(y,x);
  
  cvNamedWindow( "original", 1 );
  cvShowImage( "original", Image1 );
  cvNamedWindow( "wndname", 1 );
  cvShowImage( "wndname", Image2 );
  
  cvWaitKey(0);
  return 0;
  }
  
  
  void GrowColor(int y, int x)
  {
  
  s.val[0]=0; s.val[1]=0; s.val[2]=0; s.val[3]=0;
  if((x &lt; 1)&amp;&amp;(y &lt; 1))
  s = cvGet2D(Image1,y,x);
  
  if(s.val[0]==0)
    {        
         s11 = cvGet2D(Image1,y,x);
         mean[0] = total[0] / coont;
         mean[1] = total[1] / coont;
         mean[2] = total[2] / coont;
  
         temp[0] = abs(s11.val[0] - mean[0]);
         temp[1] = abs(s11.val[1] - mean[1]);
         temp[2] = abs(s11.val[2] - mean[2]);
  
         Diff = (int)(sqrt((temp[0]*temp[0]+temp[1]*temp[1]+temp[2]*temp[2])/3));
  
                if (Diff &lt; Threshold)
                {
                total[0] += abs(s11.val[0]);
                total[1] += abs(s11.val[1]);
                total[2] += abs(s11.val[2]);
                coont++;
                if ((x &gt; 0) &amp;&amp; (y &gt; 0))
                cvSet2D(Image2,y,x,s11);
                }  
      }
  
  }
  </code></pre>
  
  <p>Is it possible to rewrite the code based on recursive mechanism? What seems to be the criteria for selecting the <strong>threshold value, Xseed, Yseed</strong> manually?</p>