kmeans2 cannot get the 'centers' .the turn out to be zero all. (Bug #1359)


Added by lowe nick over 13 years ago. Updated almost 13 years ago.


Status:Done Start date:
Priority:Low Due date:
Assignee:Maria Dimashova % Done:

0%

Category:core
Target version:2.4.0
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

i use this code to get the cluster labels,""cluster_
and the centroids "centers"
but the cvmat centers remains full of zeros.
why?
how can i get the centroids?

#include <iostream>
#include <stdio.h>

#include "cxcore.h"
#include "highgui.h"
using namespace cv;

int main( int argc, char** argv ) {
int i,j;

CvMat* points = cvCreateMat( 5, 2, CV_32FC1 );
CvMat* centers = cvCreateMat( 5, 2, CV_32FC1 );
CvMat* clusters = cvCreateMat( 5, 1, CV_32SC1 );

cvSetReal2D( points, 0, 0,1);
cvSetReal2D( points, 0, 1,1);
cvSetReal2D( points, 1, 0,2);
cvSetReal2D( points, 1, 1,2);
cvSetReal2D( points, 2, 0,6);
cvSetReal2D( points, 2, 1,6);
cvSetReal2D( points, 3, 0,5);
cvSetReal2D( points, 3, 1,5);
cvSetReal2D( points, 4, 0,10);
cvSetReal2D( points, 4, 1,10);

cvKMeans2( points, 3, clusters,
cvTermCriteria( CV_TERMCRIT_EPS, 1000,0 ),1000,0,KMEANS_RANDOM_CENTERS,centers,0);

for(i=0;i<5;i++)
printf( " %lf \n", cvGetReal2D(clusters,i, 0));

for(i=0;i<5;i++){
for(j=0;j<2;j++){
printf( "center %i %i = %lf \n", i, j, cvGetReal2D(centers,i, 0));
}
}

cvReleaseMat(&points);
cvReleaseMat(&centers);
cvReleaseMat(&clusters);
}


Associated revisions

Revision 84c99906
Added by Maria Dimashova almost 13 years ago

fixed cvKMeans2 (#1359)

Revision 78a6692e
Added by Roman Donchenko over 11 years ago

Merge pull request #1359 from asmorkalov:winrt_tiff_api_fix

History

Updated by lowe nick over 13 years ago

a correction

printf( "center %i %i = %lf \n", i, j, cvGetReal2D(centers,i, j)); (j instead of zero)

can u help me please make the k means2 work?

Updated by Alexander Shishkov about 13 years ago

  • Description changed from i use this code to get the cluster labels,""cluster_ and the centro... to i use this code to get the cluster labels,""cluster_ and the centro... More

Updated by Alexander Shishkov about 13 years ago

  • Category set to core

Updated by Alexander Shishkov almost 13 years ago

  • Target version deleted ()

Updated by Alexander Shishkov almost 13 years ago

  • Assignee deleted (Vadim Pisarevsky)

Updated by Maria Dimashova almost 13 years ago

  • Assignee set to Maria Dimashova

Updated by Maria Dimashova almost 13 years ago

Thanks for the report. I found two bugs:

1) In your code the matrix for the clusters centers is created of incorrect size. You should replace current creation with CvMat* centers = cvCreateMat( 3/*clusters_count*/, 2/*dim*/, CV_32FC1 ); and don't forget to correct the circle of printing the centers (5->3). After these changes the centers are printed successfully.

2) opencv should throw an exception in the case of incorrect input data size, but it didn't. Instead, it created a temporary matrix of needed size and after leaving the function its values were released. This problem was fixed in r7705.

  • Status changed from Open to Done

Updated by Alexander Shishkov almost 13 years ago

  • Target version set to 2.4.0

Also available in: Atom PDF