Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to find which pixel belongs to which clusters or centers?

I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

How to find which pixel belongs to which clusters or centers?

image description I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

How to find which pixel belongs to which clusters or centers?

image description I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

How to find which pixel belongs to which clusters or centers?

image description I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

code:

(Include header files) using namespace cv; using namespace cv::ml; using namespace std; void readCenters(cv::Mat&, const char *);

int CLUSTER_COUNT=5;//number of clusters Mat train_data; Mat test_data;

void printDim(cv::Mat mat,const char *name)

void splitData(cv::Mat &data,cv::Mat &train_data,cv::Mat &test_data)

void readData(cv::Mat& data) { std::vector<const char="" *=""> filenames; filenames.push_back("Data/ik147_1.txt"); filenames.push_back("Data/ik147_2.txt"); string line; vector<mat> raw_data(2);//= new std::vector<cv::mat>(4); int row; double min,max; for(int i =0;i<2;i++) { ifstream file( filenames[i] ); while( file>>row ) { raw_data[i].push_back(row); } minMaxLoc(raw_data[i],&min,&max); cout<<filenames[i]&lt;&lt;" min="" :"&lt;<min&lt;&lt;",="" max="" :"&lt;<max&lt;<std::endl;<="" p="">

}
int N=raw_data[0].rows;

// cv::Mat data(N,3,CV_32FC1); cout<<"N value : "<< N << endl; int columns_to_read=2; data.create(N,columns_to_read,CV_32FC1);

for(int i=0;i<columns_to_read;i++)
{
    raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
}

}

void computeLabelledData(Mat& data,Mat &data_with_labels) { Mat labels,centers;
kmeans(data, CLUSTER_COUNT, labels, TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);

data_with_labels.create(data.rows,data.cols+1,CV_32FC1);
data.copyTo(data_with_labels(Rect(0,0,data.cols,data.rows)));
labels.copyTo(data_with_labels(Rect(data.cols,0,labels.cols,labels.rows)));

}

void print(Mat mat, int prec) {
for(int i=0; i<mat.size().height; i++)="" {="" cout="" &lt;&lt;="" "[";="" for(int="" j="0;" j<mat.size().width;="" j++)="" {="" cout="" &lt;&lt;="" setprecision(prec)="" &lt;&lt;="" mat.at<float="">(i,j); if(j != mat.size().width-1) cout << ", "; else cout << "]" << endl; } } } int main() { // Data for visual representation int width = 512, height = 512; Mat image = Mat::zeros(height, width, CV_8UC3);

    Mat data;
readData(data);
printDim(data,"data");
Mat data_with_labels,train_data_labels,test_data_labels;
 computeLabelledData(data,data_with_labels);
  splitData(data_with_labels,train_data_labels,test_data_labels);
    //splitData(data,train_data,test_data);

Mat train_data,train_labels;
train_data_labels(cv::Rect(0,0,train_data_labels.cols-1,train_data_labels.rows)).copyTo(train_data);
train_data_labels(cv::Rect(train_data_labels.cols-1,0,1,train_data_labels.rows)).copyTo(train_labels);

Mat test_data,test_labels,predicted_labels,confusion_matrix;
test_data_labels(cv::Rect(0,0,test_data_labels.cols-1,test_data_labels.rows)).copyTo(test_data);
test_data_labels(cv::Rect(test_data_labels.cols-1,0,1,test_data_labels.rows)).copyTo(test_labels);

printDim(train_data,"train_data"); 
printDim(test_data,"test_data"); 

cout <<"train data : "  << endl;
//print(train_data, 3);
cout <<"test data : "  << endl;
//print(test_data, 3);


int N=test_data.rows; //number of data points
int K=5;  //number of labels

RNG rng((unsigned)time(NULL)); 
RNG rng_center(20);
int clusterCount=K;
int sampleCount = N;
Mat centers(5,2,CV_32FC1);
readCenters(centers,"centers.txt");
Mat labelsMat(N,1,CV_32SC1);
Mat labelsMatKMeans;
Mat img_kmeans(512, 512, CV_8UC3);

Point center;
center.x=0;//centers.at<float>(k,0);
center.y=0;//:centers.at<float>(k,1);

// Set up training data
/* generate random labeld sample from multigaussian distribution */ 
for( int k = 0; k < clusterCount; ++k )
{
    Mat pointChunk = test_data.rowRange(k*sampleCount/clusterCount,
                                        k == clusterCount - 1 ? sampleCount :
                                        (k+1)*sampleCount/clusterCount);

    rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y,0,0), Scalar(width*0.10, height*0.10));
    Mat repeat_center;
    repeat(centers.row(k),pointChunk.rows,1,repeat_center);
    pointChunk=pointChunk+repeat_center; 

    Mat labelChunk = labelsMat.rowRange(k*sampleCount/clusterCount,
                                        k == clusterCount - 1 ? sampleCount :
                                        (k+1)*sampleCount/clusterCount);

    labelChunk=k;


}
test_data=abs(test_data);
for(int i=0;i<N;++i)
{

    test_data.at<float>(i,0)= cvCeil(test_data.at<float>(i,0));
    test_data.at<float>(i,1)= cvCeil(test_data.at<float>(i,1));

}
 std::vector<Vec3b> colors_region;
std::vector<Vec3b> colors_data;
//generate random colors for each label
for(int i=0;i<K;++i)
{
    int icolor = (unsigned) rng;
    colors_region.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));
    icolor=(unsigned)rng;
    colors_data.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));

}

//draw k-means output kmeans(test_data, clusterCount, labelsMatKMeans, TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);

      print(centers, 3);
    img_kmeans = Scalar::all(0);

    for( int i = 0; i < sampleCount; i++ )
    {
        int clusterIdx = labelsMatKMeans.at<int>(i);

        Point ipt ;
        ipt.x= (int)test_data.at<float>(i,0);
        ipt.y=(int)test_data.at<float>(i,1);
        circle( img_kmeans, ipt, 5, Scalar(colors_data[clusterIdx]), -1, 8 );
    }

    imshow("K-Means clustering", img_kmeans);
    imwrite("k_means.png",img_kmeans);

// Ptr<svm> svm = Algorithm::load<svm>("svm_params.xml");

cout<<"data loaded"<< endl;

waitKey(0);

}

void readCenters(cv::Mat &centers, const char * filename){ const int ROWS=5; const int COLS=2; std::cout<<"reading centers "<<filename&lt;<std::endl; float="" array[rows][cols];="" std::ifstream="" file(="" filename="" );="" std::string="" line;="" int="" row,col;="" int="" i="0;&lt;/p">

while( file>>row>>col )
{
    centers.at<float>(i,0)=row;
    centers.at<float>(i,1)=col;
    i++;
    cout<<"center "<< i <<": ("<< row <<","<< col << ")" <<endl;
}

}

How to find which pixel belongs to which clusters or centers?

image description I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

code:

(Include header files)
using namespace cv;
using namespace cv::ml;
using namespace std;
void readCenters(cv::Mat&, const char *);

*); int CLUSTER_COUNT=5;//number of clusters Mat train_data; Mat test_data;

test_data; void printDim(cv::Mat mat,const char *name)

*name) void splitData(cv::Mat &data,cv::Mat &train_data,cv::Mat &test_data)

&test_data) void readData(cv::Mat& data) { std::vector<const char="" *=""> char *> filenames; filenames.push_back("Data/ik147_1.txt"); filenames.push_back("Data/ik147_2.txt"); string line; vector<mat> vector<Mat> raw_data(2);//= new std::vector<cv::mat>(4); std::vector<cv::Mat>(4); int row; double min,max; for(int i =0;i<2;i++) { ifstream file( filenames[i] ); while( file>>row ) { raw_data[i].push_back(row); } minMaxLoc(raw_data[i],&min,&max); cout<<filenames[i]&lt;&lt;" min="" :"&lt;<min&lt;&lt;",="" max="" :"&lt;<max&lt;<std::endl;<="" p="">

cout<<filenames[i]<<" min :"<<min<<", max :"<<max<<std::endl;

    }
 int N=raw_data[0].rows;

// cv::Mat data(N,3,CV_32FC1); cout<<"N value : "<< N << endl; int columns_to_read=2; data.create(N,columns_to_read,CV_32FC1);

data.create(N,columns_to_read,CV_32FC1);

    for(int i=0;i<columns_to_read;i++)
 {
     raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
 }

}

} void computeLabelledData(Mat& data,Mat &data_with_labels) { Mat labels,centers;
kmeans(data, CLUSTER_COUNT, labels, TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);

centers);


    data_with_labels.create(data.rows,data.cols+1,CV_32FC1);
 data.copyTo(data_with_labels(Rect(0,0,data.cols,data.rows)));
 labels.copyTo(data_with_labels(Rect(data.cols,0,labels.cols,labels.rows)));

}

} void print(Mat mat, int prec) {
for(int i=0; i<mat.size().height; i++)="" {="" cout="" &lt;&lt;="" "[";="" for(int="" j="0;" j<mat.size().width;="" j++)="" {="" cout="" &lt;&lt;="" setprecision(prec)="" &lt;&lt;="" mat.at<float="">(i,j); i++) { cout << "["; for(int j=0; j<mat.size().width; j++) { cout << setprecision(prec) << mat.at<float>(i,j); if(j != mat.size().width-1) cout << ", "; else cout << "]" << endl; } } } int main() { // Data for visual representation int width = 512, height = 512; Mat image = Mat::zeros(height, width, CV_8UC3);

CV_8UC3);

        Mat data;
 readData(data);
 printDim(data,"data");
 Mat data_with_labels,train_data_labels,test_data_labels;
  computeLabelledData(data,data_with_labels);
   splitData(data_with_labels,train_data_labels,test_data_labels);
     //splitData(data,train_data,test_data);

 Mat train_data,train_labels;
 train_data_labels(cv::Rect(0,0,train_data_labels.cols-1,train_data_labels.rows)).copyTo(train_data);
 train_data_labels(cv::Rect(train_data_labels.cols-1,0,1,train_data_labels.rows)).copyTo(train_labels);

 Mat test_data,test_labels,predicted_labels,confusion_matrix;
 test_data_labels(cv::Rect(0,0,test_data_labels.cols-1,test_data_labels.rows)).copyTo(test_data);
 test_data_labels(cv::Rect(test_data_labels.cols-1,0,1,test_data_labels.rows)).copyTo(test_labels);

 printDim(train_data,"train_data"); 
 printDim(test_data,"test_data"); 

 cout <<"train data : "  << endl;
 //print(train_data, 3);
 cout <<"test data : "  << endl;
 //print(test_data, 3);


 int N=test_data.rows; //number of data points
 int K=5;  //number of labels

 RNG rng((unsigned)time(NULL)); 
 RNG rng_center(20);
 int clusterCount=K;
 int sampleCount = N;
 Mat centers(5,2,CV_32FC1);
 readCenters(centers,"centers.txt");
 Mat labelsMat(N,1,CV_32SC1);
 Mat labelsMatKMeans;
 Mat img_kmeans(512, 512, CV_8UC3);

 Point center;
 center.x=0;//centers.at<float>(k,0);
 center.y=0;//:centers.at<float>(k,1);

 // Set up training data
 /* generate random labeld sample from multigaussian distribution */ 
 for( int k = 0; k < clusterCount; ++k )
 {
     Mat pointChunk = test_data.rowRange(k*sampleCount/clusterCount,
                                         k == clusterCount - 1 ? sampleCount :
                                         (k+1)*sampleCount/clusterCount);

     rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y,0,0), Scalar(width*0.10, height*0.10));
     Mat repeat_center;
     repeat(centers.row(k),pointChunk.rows,1,repeat_center);
     pointChunk=pointChunk+repeat_center; 

     Mat labelChunk = labelsMat.rowRange(k*sampleCount/clusterCount,
                                         k == clusterCount - 1 ? sampleCount :
                                         (k+1)*sampleCount/clusterCount);

     labelChunk=k;


 }
 test_data=abs(test_data);
 for(int i=0;i<N;++i)
 {

     test_data.at<float>(i,0)= cvCeil(test_data.at<float>(i,0));
     test_data.at<float>(i,1)= cvCeil(test_data.at<float>(i,1));

 }
  std::vector<Vec3b> colors_region;
 std::vector<Vec3b> colors_data;
 //generate random colors for each label
 for(int i=0;i<K;++i)
 {
     int icolor = (unsigned) rng;
     colors_region.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));
     icolor=(unsigned)rng;
     colors_data.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));

 }

//draw k-means output kmeans(test_data, clusterCount, labelsMatKMeans, TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);

centers);

          print(centers, 3);
     img_kmeans = Scalar::all(0);

     for( int i = 0; i < sampleCount; i++ )
     {
         int clusterIdx = labelsMatKMeans.at<int>(i);

         Point ipt ;
         ipt.x= (int)test_data.at<float>(i,0);
         ipt.y=(int)test_data.at<float>(i,1);
         circle( img_kmeans, ipt, 5, Scalar(colors_data[clusterIdx]), -1, 8 );
     }

     imshow("K-Means clustering", img_kmeans);
     imwrite("k_means.png",img_kmeans);

// Ptr<svm> Ptr<SVM> svm = Algorithm::load<svm>("svm_params.xml");

Algorithm::load<SVM>("svm_params.xml");

    cout<<"data loaded"<< endl;

 waitKey(0);

}

} void readCenters(cv::Mat &centers, const char * filename){ const int ROWS=5; const int COLS=2; std::cout<<"reading centers "<<filename&lt;<std::endl; float="" array[rows][cols];="" std::ifstream="" file(="" filename="" );="" std::string="" line;="" int="" row,col;="" int="" i="0;&lt;/p">

"<<filename<<std::endl;
    float array[ROWS][COLS];
    std::ifstream file( filename );
    std::string line; 
    int row,col;
    int i=0;

    while( file>>row>>col )
 {
     centers.at<float>(i,0)=row;
     centers.at<float>(i,1)=col;
     i++;
     cout<<"center "<< i <<": ("<< row <<","<< col << ")" <<endl;
    }
}

}