我使用此代码。它应该打印集群标签然后是质心。但是带有质心的“中心”矩阵似乎是空的,充满了零。我的朋友们怎么了?
#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* centers2 = 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,centers2,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 = %f \n", i, j, cvGetReal2D(centers2,i, j));
}
}
cvReleaseMat(&points);
cvReleaseMat(¢ers2);
cvReleaseMat(&clusters);
}