cv::kmeans code snippet

It was impossible for me to find in the Internet a sample of usage of kmeans with the new C++ interface. 

So, as I finally made it, I thought that it would be a good idea to post it here hoping to save someone's time. 

cv::Mat samples = (cv::Mat_<float>(8, 1) << 31 , 2 , 10 , 11 , 25 , 27, 2, 1); 
cv::Mat labels, centers; 

// double kmeans(const Mat&  samples, int clusterCount, Mat&  labels, TermCriteria termcrit, int attempts, int  flags, Mat*  centers) 
double compactness = cv::kmeans(samples, 3, labels, cv::TermCriteria(), 2, cv::KMEANS_PP_CENTERS, &centers); 

cout<<"labels:"<<endl; 
for(int i = 0; i < labels.rows; ++i) 

        cout<<labels.at<int>(i, 0)<<endl; 


cout<<"/ncenters:"<<endl; 
for(int i = 0; i < centers.rows; ++i) 

        cout<<centers.at<float>(i, 0)<<endl; 


cout<<"/ncompactness: "<<compactness<<endl; 

你可能感兴趣的:(c,float,idea,internet)