opencv4.1.2 kmeans algrithom
#include"opencv2/highgui.hpp"
#include"opencv2/core.hpp"
#include"opencv2/imgproc.hpp"
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv){
const int MAX_CLUSTERS = 5;// number of clusters of input data
Scalar colorTab [] ={
Scalar(0,0,255),
Scalar(0,255,0),
Scalar(255,100,100),
Scalar(0,255,25)
}; //every cluster have a unique color
Mat img(500,500,CV_8UC3); //create a 500*500 1 channel image by mat
RNG rng(12345); //the random class
for(;;){
int k,clusterCount =rng.uniform(2,MAX_CLUSTERS+1);//create random sequence number by uniform distribution,[2~MAX_CLUSTER+1)
int i,sampleCount = rng.uniform(1,1001);
cout<< "number of cluster count:"<<clusterCount<<"number of sample count:"<<sampleCount<<endl;
Mat points(sampleCount,1,CV_32FC2),labels;
clusterCount =MIN(clusterCount,sampleCount);
vector<Point2f> centors;
for(int k=0;k<clusterCount;k++){
/**random init**/
Point centor;
centor.x = rng.uniform(0,img.cols);
centor.y = rng.uniform(0,img.rows);
Mat pointChunk =points.rowRange(k*sampleCount/clusterCount,k==clusterCount-1?sampleCount:(k+1)*sampleCount/clusterCount);
rng.fill(pointChunk,RNG::NORMAL,Scalar(centor.x,centor.y),Scalar(img.cols*0.05,img.rows*0.05));
}
randShuffle(points,1,&rng);
double compactness = kmeans(points,clusterCount,labels,TermCriteria(TermCriteria::EPS+TermCriteria::COUNT,10,1.0),3,KMEANS_PP_CENTERS,centors);
img =Scalar::all(0);
for(i=0;i<sampleCount;i++){
int clusterIndex = labels.at<int>(i);
Point ipt =points.at<Point2f>(i);
circle(img,ipt,2,colorTab[clusterIndex],FILLED,LINE_AA);
}
for(i=0;i<(int)centors.size();++i){
Point2f c = centors[i];
circle(img,c,40,colorTab[i],1,LINE_AA);
}
cout<< "compactness:"<<compactness<<endl;
imshow("clusters",img);
char key=(char)waitKey();
if(key==27||key=='q'||key=='Q'){
break;
}
return 0;
}
return 1;
}