种子区域种植OpenCV

时间:2017-01-07 06:28:07

标签: c++ opencv

我正在使用Win 7,64,MS2012(C ++)和OpenCV 2.4.11

我编写种子区域增长算法,但我不知道为什么结果不合适。

目标是用SRG算法提取图像的胸肌(左角)。

输入图片:

Input image

这是我的代码(主要功能)

img = imread("C:/Users/Zimabi/Downloads/region_growing/jhgray.jpg",0);

Mat J1  = Mat::zeros(img.rows,img.cols,CV_8UC1);
int x=15,y=15;

int reg_mean = img.at<uchar>(x,y);
int reg_size = 1;
int neg_pos = 0;

Mat neg_list = Mat::zeros(img.cols*img.rows,3,CV_8UC1);
double pixdist = 0, reg_maxdist = 0.1;
int xn = 0,yn = 0;


Mat neigb = Mat::zeros(4,2,CV_8UC1);

int nei1 [4] = {-1,1,0,0};
int nei2 [4] = {0,0,-1,1};


while ((pixdist < reg_maxdist) && (reg_size < img.cols*img.rows))
{
    int inx,min1=1000;

    for (int r = 0; r < 4; r++)
    {
        xn = x + nei1[r] ;
        yn = y + nei2[r]; 


        bool ins=(xn>=1)&&(yn>=1)&&(xn<=img.rows)&&(yn<= img.cols);

        if (ins && (J1.at<uchar>(xn,yn) == 0))
        {

            neg_pos = neg_pos+1;
            neg_list.at<uchar>(neg_pos,0) = xn;
            neg_list.at<uchar>(neg_pos,1) = yn;
            neg_list.at<uchar>(neg_pos,2) = img.at<uchar>(xn,yn); 
            J1.at<uchar>(xn,yn)=255;
        }
    }

    Mat x1 = Mat::zeros(neg_pos,1,CV_8UC1);

        for (int i3 = 0; i3 <neg_pos ; i3++)
        {


            x1.at<uchar>(i3,0) = abs(neg_list.at<uchar>(i3,2) -  reg_mean);
            if (x1.at<uchar>(i3,0)<min1)
          {
              min1 = x1.at<uchar>(i3,0);
              inx = i3;
          }
        }

        pixdist = min1;

        J1.at<uchar>(x,y)=255; 
        reg_size=reg_size+1;

        reg_mean= (reg_mean*reg_size + neg_list.at<uchar>  (inx,2))/(reg_size+1);

        x = neg_list.at<uchar>(inx,0);
        y = neg_list.at<uchar>(inx,1);


        neg_list.at<uchar>(inx,0) = neg_list.at<uchar>(neg_pos,0);
        neg_list.at<uchar>(inx,1) = neg_list.at<uchar>(neg_pos,1);
        neg_list.at<uchar>(inx,2) = neg_list.at<uchar>(neg_pos,2);
        neg_pos=neg_pos-1;

}

imshow("J",J1);
waitKey(0);
return 0;

此致

0 个答案:

没有答案