种子区域增长分割opencv

时间:2014-07-04 12:00:28

标签: c opencv

我正在尝试在openCV中的种子区域增长分割上编写代码。 我已将此[[1]:https://stackoverflow.com/questions/14416511/seeded-region-growing-with-opencv][1]代码作为参考,并尝试构建基于迭代的方法。

    #include <cv.h>
#include <highgui.h>
#include <stdio.h>

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->height;
xDim = Image1->width;
IplImage* img = cvCreateImage(cvGetSize(Image1), Image1->depth , Image1->nChannels );

Image2 = cvCreateImage(cvGetSize(Image1), Image1->depth , Image1->nChannels );
cvZero( Image2 );

for (y = Yseed - 5; y <= Yseed + 5; y++)
     for (x = Xseed - 5; x <= Xseed + 5; x++)
       if ((x > 0) && (y > 0) && (x < xDim) && (y <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 < yDim; y++)
     for (x = 0; x < 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 < 1)&&(y < 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 < Threshold)
              {
              total[0] += abs(s11.val[0]);
              total[1] += abs(s11.val[1]);
              total[2] += abs(s11.val[2]);
              coont++;
              if ((x > 0) && (y > 0))
              cvSet2D(Image2,y,x,s11);
              }  
    }

}

是否可以基于递归机制重写代码?什么似乎是手动选择阈值,Xseed,Yseed 的标准?

0 个答案:

没有答案