Kmean聚类算法(c++实现)

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int main(){

    vector> pointSets;
    vector centralPoints;
    centralPoints.push_back(Point2i(50,50)); //生成的待聚类的点的期望中心
    centralPoints.push_back(Point2i(300,250));
    centralPoints.push_back(Point2i(100,300));

    for(int i=0;i<1000;i++)
    {
         vector xy;
         int count=i%3;


         xy.push_back(centralPoints[count].x+double(rand())/RAND_MAX*150);
         xy.push_back(centralPoints[count].y+double(rand())/RAND_MAX*150);
         pointSets.push_back(xy);


    }
    Mat img(500,500,CV_8UC3);
    for(int i=0;i(i,j)[k]=255;
            }

        }
    }

    for(int i=0;i initPoints;
    for(int i=0;i<3;i++)
    {
        initPoints.push_back(Point2i(double(rand())/RAND_MAX*500,double(rand())/RAND_MAX*500));
        cout<< initPoints[i] << endl;
    }


    //Every point to The center ' s distance
     vector tempPoints;
     for(int k=0;k LastPoints;
     for(int k=0;k

Kmean聚类算法(c++实现)_第1张图片

每按一次空格进行一次迭代,可以看到多次迭代过后中心趋于不变。

你可能感兴趣的:(算法,c++,聚类)