Use of Kmean C++

asked 2015-01-17 04:30:02 -0600

totocvxdfvdsv gravatar image

updated 2015-01-17 04:51:31 -0600

berak gravatar image

Hello everybody,

I'm confronted to a problem in my attempt to use Kmeans form OpenCV. I'am using C++11 with the IDE qtcreator on ubuntu 14.04.

Concerning my problem, I have a collection of samples caracterized by a constant amount of complex features.

My first Idea was to send a vector of vector of complex to kmeans but as you may no it didn't work. So i decided to use a cv::Mat filled with my samples. So I have a cv::Mat in which:

rows = samples

cols = features (a complex coefficient is seen as two float so the real number of features in my cv::Mat is twice the original number of features).

With such an input, I have no error when running the code BUT, the output of :

cv::kmeans(bdd, nbClusters, labels,cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), attempts, cv::KMEANS_RANDOM_CENTERS, centers );

   cerr << centers.size() << endl;

    cerr << centers<< endl;

    cerr << labels.size() << endl;

is:

[0 x 0]

[]

[1 x 800]

So the kmean function did not work.

Does someone has an idea of how to fix that promblem?

Thanks a lot

edit retag flag offensive close merge delete

Comments

can you show, how you fill the bdd Mat ? how much is nbClusters ?

berak gravatar imageberak ( 2015-01-17 04:52:22 -0600 )edit

The interresting part of the function that builds upp the bdd is

//cv::Mat inputSamples(numSamples, numFeatures, CV_32F)
cv::Mat outPut = cv::Mat(totalSamples, totalFeatures*2, CV_32F);

int offseti = 0;
int offsetj = 0;
for (uint index=0; index<allBdd.size(); index++)
{
    cv::Mat tempMat = allBdd[index];
    for (int i=0; i<tempMat.cols; i++)//0->199
    {
        for(int j=0; j<tempMat.rows; j++)//0->40
        {
            outPut.at<float> (j+offsetj,i+offseti)= tempMat.at<float>(j,i);
        }
    }
    offseti += tempMat.cols;
}
cout<<endl<<"Final Size of the bddMath after reading all BDD= "<<outPut.size()<<endll;
return outPut

Where ouPut is the bdd and allbdd is a vector of cv::Mat

totocvxdfvdsv gravatar imagetotocvxdfvdsv ( 2015-01-17 05:14:38 -0600 )edit

Your problem might be settled in wrong parameters: cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), i.e. only 10 iterations and an eps of 1.0? Try 100 iterations and eps of 0.01 And see what comes out. I'd also always choose KMEANS_PP_CENTERS.

Note to the creation of outPut: In allBdd you have all your features in different mats and you want to create a new one containing all features by stacking them column-wise. With copyTo and colRange this would have been much more elegant:

for (uint index=0; index<allBdd.size(); index++)
{
  cv::Mat tempMat = allBdd[index];
  tmpMat.copyTo(outPut.colRange(offseti, offseti+tempMat.cols));
  offseti += tempMat.cols;
}
Guanta gravatar imageGuanta ( 2015-01-17 07:28:10 -0600 )edit

Thanks for your help,

I tried your solution but I still have a strange output. For example if I look into the labels output I have something like:

1.4013e-45

7.00649e-45

1.4013e-45

1.4013e-45

7.00649e-45

8.40779e-45

1.68156e-44

2.38221e-44

2.38221e-44

0

0

Do you have any idee what that mean? I thought about something like the integer label affected to each sample.

totocvxdfvdsv gravatar imagetotocvxdfvdsv ( 2015-01-19 11:14:26 -0600 )edit

Hmm, this looks indeed strange. Seems, that all the labels are 0 or close to 0. I thought the type of the labels should be integer, not float. Did you get now correct centers?

Guanta gravatar imageGuanta ( 2015-01-19 11:55:39 -0600 )edit

I agree the result is pretty strange! The centroids are also peculiar and they seems aligned but it might be because of dimensionality (11 and plot on 2.....)

this is the print of centers:

Center 0 :-2.52567-1.91473-10.37434.7551520.307513.73612.452832.621866.6833-64.7493-619.13217.1157210.8272.61229e-15-10.356645.405315.741846.87586.49164-4.543460.125105-3.46925

Center 1 :-2.95158-2.13840.800356-1.379833.90617-4.4137111.92352.1866357.1363105.276301.674785.91219.945-7.28762e-16-9.31652-13.695121.5254-42.31570.559087-2.73084-2.33927-5.50557 And so on

totocvxdfvdsv gravatar imagetotocvxdfvdsv ( 2015-01-20 02:24:09 -0600 )edit

the centers seem to be fine

Guanta gravatar imageGuanta ( 2015-01-20 03:50:29 -0600 )edit

yes it seems at the first look but when i plot them on the projection of my samples they do not fit well on the clusters

totocvxdfvdsv gravatar imagetotocvxdfvdsv ( 2015-01-21 02:10:14 -0600 )edit