Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to centers generated by kmeans in svm training ?

I define initial centers and use some data, than perform kmeans. I also print new centers , they are different from my initial centers. Here, I added code of that. I want to use these centers in svm training. Give me idea or add example code for that. For data set, I have text files, in those data are 512512. I create data from text files with this dimension (262144 (row) 2 (cols) ).

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


    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20); 

  Mat labelsMat(N,1,CV_32SC1);
      Mat labelsMatKMeans;

    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,cl,CV_32FC1);
    readCenters(centers,"centers.txt");

    Point center;
    center.x = 0;//rng_center.uniform(0, height);
    center.y = 0;//rng_center.uniform(0, width);

    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = train_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y), Scalar(width*0.05, height*0.05));

        Mat repeat_center; 
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        ////cout<<repeat_center;
        //pointChunk=pointChunk+repeat_center;

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

        labelChunk=k;
    }

    for(int i=0;i<N;++i)
    {
        train_data.at<float>(i,0)= cvCeil(train_data.at<float>(i,0));
        train_data.at<float>(i,1)= cvCeil(train_data.at<float>(i,1));
    //  train_data.at<float>(i,2)= cvCeil(train_data.at<float>(i,2));

        labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
    }
    printDim(labelsMat,"labelMat");
     train_data=abs(train_data); 
    //absdiff(testDataMat,Scalar::all(0),testDataMat);

       vector<Vec3b> colors_region;
     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(train_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

         // cout << "M = "<< endl << " "  << labelsMatKMeans << endl << endl;
          cout<< "Final center :"<<endl;
          print(centers, 3);
          cout<< "  " << endl;

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

        img_kmeans = Scalar::all(0);

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

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)train_data.at<float>(i,0);
            ipt.y=(int)train_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);

how to centers generated by kmeans in svm training ?

I define initial centers and use some data, than perform kmeans. I also print new centers , they are different from my initial centers. Here, I added code of that. I want to use these centers in svm training. Give me idea or add example code for that. For data set, I have text files, in those data are 512512 x 512. I create data from text files with this dimension (262144 (row) (row) x 2 (cols) ).

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


    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20); 

  Mat labelsMat(N,1,CV_32SC1);
      Mat labelsMatKMeans;

    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,cl,CV_32FC1);
    readCenters(centers,"centers.txt");

    Point center;
    center.x = 0;//rng_center.uniform(0, height);
    center.y = 0;//rng_center.uniform(0, width);

    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = train_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y), Scalar(width*0.05, height*0.05));

        Mat repeat_center; 
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        ////cout<<repeat_center;
        //pointChunk=pointChunk+repeat_center;

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

        labelChunk=k;
    }

    for(int i=0;i<N;++i)
    {
        train_data.at<float>(i,0)= cvCeil(train_data.at<float>(i,0));
        train_data.at<float>(i,1)= cvCeil(train_data.at<float>(i,1));
    //  train_data.at<float>(i,2)= cvCeil(train_data.at<float>(i,2));

        labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
    }
    printDim(labelsMat,"labelMat");
     train_data=abs(train_data); 
    //absdiff(testDataMat,Scalar::all(0),testDataMat);

       vector<Vec3b> colors_region;
     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(train_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

         // cout << "M = "<< endl << " "  << labelsMatKMeans << endl << endl;
          cout<< "Final center :"<<endl;
          print(centers, 3);
          cout<< "  " << endl;

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

        img_kmeans = Scalar::all(0);

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

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)train_data.at<float>(i,0);
            ipt.y=(int)train_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);

how to use centers generated by kmeans in svm training ?

I define initial centers and use some data, than perform kmeans. I also print new centers , they are different from my initial centers. Here, I added code of that. I want to use these centers in svm training. Give me idea or add example code for that. For data set, I have text files, in those data are 512 x 512. I create data from text files with this dimension (262144 (row) x 2 (cols) ).

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


    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20); 

  Mat labelsMat(N,1,CV_32SC1);
      Mat labelsMatKMeans;

    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,cl,CV_32FC1);
    readCenters(centers,"centers.txt");

    Point center;
    center.x = 0;//rng_center.uniform(0, height);
    center.y = 0;//rng_center.uniform(0, width);

    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = train_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y), Scalar(width*0.05, height*0.05));

        Mat repeat_center; 
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        ////cout<<repeat_center;
        //pointChunk=pointChunk+repeat_center;

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

        labelChunk=k;
    }

    for(int i=0;i<N;++i)
    {
        train_data.at<float>(i,0)= cvCeil(train_data.at<float>(i,0));
        train_data.at<float>(i,1)= cvCeil(train_data.at<float>(i,1));
    //  train_data.at<float>(i,2)= cvCeil(train_data.at<float>(i,2));

        labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
    }
    printDim(labelsMat,"labelMat");
     train_data=abs(train_data); 
    //absdiff(testDataMat,Scalar::all(0),testDataMat);

       vector<Vec3b> colors_region;
     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(train_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

         // cout << "M = "<< endl << " "  << labelsMatKMeans << endl << endl;
          cout<< "Final center :"<<endl;
          print(centers, 3);
          cout<< "  " << endl;

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

        img_kmeans = Scalar::all(0);

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

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)train_data.at<float>(i,0);
            ipt.y=(int)train_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);

how to use centers generated by kmeans in svm training in opencv ?

I define initial centers and use some data, than perform kmeans. I also print new centers , they are different from my initial centers. Here, I added code of that. I want to use these centers in svm training. Give me idea or add example code for that. For data set, I have text files, in those data are 512 x 512. I create data from text files with this dimension (262144 (row) x 2 (cols) ).

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


    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20); 

  Mat labelsMat(N,1,CV_32SC1);
      Mat labelsMatKMeans;

    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,cl,CV_32FC1);
    readCenters(centers,"centers.txt");

    Point center;
    center.x = 0;//rng_center.uniform(0, height);
    center.y = 0;//rng_center.uniform(0, width);

    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = train_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y), Scalar(width*0.05, height*0.05));

        Mat repeat_center; 
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        ////cout<<repeat_center;
        //pointChunk=pointChunk+repeat_center;

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

        labelChunk=k;
    }

    for(int i=0;i<N;++i)
    {
        train_data.at<float>(i,0)= cvCeil(train_data.at<float>(i,0));
        train_data.at<float>(i,1)= cvCeil(train_data.at<float>(i,1));
    //  train_data.at<float>(i,2)= cvCeil(train_data.at<float>(i,2));

        labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
    }
    printDim(labelsMat,"labelMat");
     train_data=abs(train_data); 
    //absdiff(testDataMat,Scalar::all(0),testDataMat);

       vector<Vec3b> colors_region;
     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(train_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

         // cout << "M = "<< endl << " "  << labelsMatKMeans << endl << endl;
          cout<< "Final center :"<<endl;
          print(centers, 3);
          cout<< "  " << endl;

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

        img_kmeans = Scalar::all(0);

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

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)train_data.at<float>(i,0);
            ipt.y=(int)train_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);