基于颜色的图像分割使用k均值,opencv

时间:2013-12-07 16:43:01

标签: k-means

我想

1)读取3通道图像

2)使用K-Means方法,创建k个不同的类

3)我将相应地标记像素以显示它们属于哪个类并将它们存储在矩阵中。

4)之后,我正在考虑使用连接组件标签来确定它们是否在同一段中。

我是opencv的新手,所以我想问你实现,一些代码片段让我开始。 任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:2)

typedef struct {
    int red;
    int green;
    int blue;
    int b64;
    int groupNo;
} IMAGE;

typedef struct _MEANS
{ 
  int r, g, b;
  int groupNo;
} point_t, *point;



Mat img = imread("C:\\253027.jpg",CV_LOAD_IMAGE_COLOR);
int clusterCount=5; 
IMAGE **strct= {0};
strct = allocate_matrix(img.rows, img.cols);
int i,j,k;

 if(! img.data )   
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}


for ( i = 0; i < img.rows; i++) {
    for ( j = 0; j < img.cols; j++) {
            strct[i][j].red = img.at<Vec3b>(i,j)[2];
            strct[i][j].green = img.at<Vec3b>(i,j)[1];
            strct[i][j].blue = img.at<Vec3b>(i,j)[0];

           /* printf("%u",strct[i][j].red);
            printf("%u",strct[i][j].green);
            printf("%u",strct[i][j].blue);
            */
    }
}

double *tempuz= (double *)malloc(sizeof(double) * clusterCount);

point_t* p = (point_t *)malloc(sizeof(point_t) * clusterCount);

for( k = 0; k < clusterCount; k++ )
{
      p[k].r= rand()%255;
      p[k].g=rand()%255;
      p[k].b=rand()%255;
      p[k].groupNo=k;
}


int max=0;  
  for (i = 0; i < img.rows; i++) {
    for ( j = 0; j < img.cols; j++) {
       for(k = 0; k < clusterCount; k++ ){
           tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);  
      //  cout<<"tempuz"<<tempuz[k]<<endl;
  } strct[i][j].groupNo=min_element(tempuz,clusterCount);
    //   cout<<"grup no"<<strct[i][j].groupNo<<endl;

    }
}   

   i=0;
   int r=0,b=0,g=0,counter=0;
   while(i<200){
   r=0,b=0,g=0,counter=0;

   for(k=0;k<clusterCount;k++){ 
     for (i = 0; i < img.rows; i++) {
    for ( j = 0; j < img.cols; j++) {
    if(strct[i][j].groupNo==k){
        r+=strct[i][j].red;
        b+=strct[i][j].blue;
        g+=strct[i][j].green;
        counter++;
    }
  }
}
if(counter!=0){
p[k].r=r/counter;
p[k].b=b/counter;
p[k].g=g/=counter;
}
}
i++;


  max=0;    
  for (i = 0; i < img.rows; i++) {
   for ( j = 0; j < img.cols; j++) {
     for(k = 0; k < clusterCount; k++ ){
        tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);  

   } strct[i][j].groupNo=min_element(tempuz,clusterCount);

}
  } 
}//end of while
 for(int k=0;k<clusterCount;k++){ 
   for (int i = 0; i < img.rows; i++) {
  for (int j = 0; j < img.cols; j++) {
    if(strct[i][j].groupNo==k){
        strct[i][j].red=p[k].r;
        strct[i][j].blue=p[k].b;
        strct[i][j].green=p[k].r;
    }
  }
}
   }

    for (int i = 0; i < img.rows; i++) {
    for (int j = 0; j < img.cols; j++) {
            img.at<Vec3b>(i,j)[2] = strct[i][j].red;
            img.at<Vec3b>(i,j)[1] = strct[i][j].green;
            img.at<Vec3b>(i,j)[0] = strct[i][j].blue;
    }
}