Monday, February 21, 2011

kmeans2 opencv

void main( int argc, char** argv )
{
    #define MAX_CLUSTERS 5
   
   
    //read from file
    vector< vector < float > > descriptor;
    vector classes;
    ifstream f;
    f.open("c:\\bayes2.txt");
    while(f)
    {
        string str = "";
        f>>str;
        vector desc;
        size_t found = str.find(",");
        while(found<100 && found>0)
        {
            string t = str.substr(0,found);
            desc.push_back(strtod(t.c_str(), NULL));
            str = str.substr(found+1);
            found = str.find(",");
        }
        if(desc.size()>0)
        {
            descriptor.push_back(desc);
            classes.push_back(strtod(str.c_str(), NULL));
        }
    }
    f.close();
   
   

        int k, cluster_count = MAX_CLUSTERS;
        int i, sample_count = descriptor.size(),num_features = descriptor[0].size();
       

       
        CvMat* points = cvCreateMat( sample_count, num_features, CV_32FC2 );
        CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );


        for(int i=0;i
            {
              for(int j=0;j
                 CV_MAT_ELEM(*points,float,i,j)=descriptor[i][j];
            }
           
        cvKMeans2( points, cluster_count, clusters,
                   cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 200, 1.0 ));


        for (int i=0; i
                cout << "i*: " << i << " " << (clusters->data.i[i]) << " "<< endl;
        }
               

        cvReleaseMat( &points );
        cvReleaseMat( &clusters );

 }

No comments:

Post a Comment