kMeans随机数据分类:
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main1()
{Mat img(500, 500, CV_8UC3);RNG rng(12345);Scalar colorTab[] = {Scalar(0,0,255),Scalar(0,255,0),Scalar(255,0,0),Scalar(0,255,255),Scalar(255,0,255)};int numCluster = rng.uniform(2, 5); //分类个数cout << "分类个数:" << numCluster << endl;int sampleCount = rng.uniform(2, 1000); //需要分类的点数Mat points(sampleCount, 1, CV_32FC2); //每一列两个数Mat labels; //存储每一个数据点的聚类编号Mat centers; //存储每一个聚类的中心位置//生成随机数for (int k = 0; k < numCluster; k++){Point center;center.x = rng.uniform(0, img.cols);center.y = rng.uniform(0, img.rows);//随机数据块Mat pointChunk = points.rowRange(k*sampleCount / numCluster, k == numCluster - 1 ? sampleCount: (k + 1)*sampleCount / numCluster);rng.fill(pointChunk, RNG::NORMAL, Scalar(center.x, center.y), Scalar(img.cols*0.05, img.rows*0.05));}randShuffle(points, 1, &rng); //将随机数据块打乱//使用kmeanskmeans(points, numCluster, labels, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1), 3, KMEANS_PP_CENTERS, centers);//用不同颜色显示分类img = Scalar::all(255);for (int i = 0; i < sampleCount; i++){int index = labels.at<int>(i);Point p = points.at<Point2f>(i);circle(img, p, 2, colorTab[index], -1, 8); //-1表示填充}//每个聚类的中心来绘制圆for (int i = 0; i < centers.rows; i++){int x = centers.at<float>(i, 0);int y = centers.at<float>(i, 1);cout << "x:" << x << "y:" << y << endl;circle(img, Point(x, y), 40, colorTab[i], 1, LINE_AA);}imshow("KMean-Demo", img);waitKey(0);return 0; //返回值为0表示成功执行此函数
}
运行结果:
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::ml;int main2(int argc, char **argv)
{Mat src = imread("E:\\vs2015\\opencvstudy\\2kmeans.jpg", 1);if (src.empty()){cout << "could not load the image!" << endl;return -1; //返回-1代表函数执行失败}imshow("input", src);int width = src.cols;int height = src.rows;int dims = src.channels();初始化定义int sampleCount = width*height;int clusterCount = 4;Mat points(sampleCount, dims, CV_32F, Scalar(10));Mat labels;Mat centers(clusterCount,1,points.type());RGB数据转换到样本数据int index = 0;for (int row = 0; row < height; row++){for (int col = 0; col < width; col++){index = row*width + col;Vec3b bgr = src.at<Vec3b>(row, col);points.at<float>(index, 0) = static_cast<int>(bgr[0]);points.at<float>(index, 1) = static_cast<int>(bgr[1]);points.at<float>(index, 2) = static_cast<int>(bgr[2]);}}运行kMeansTermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);kmeans(points, sampleCount, labels, criteria, 3, KMEANS_PP_CENTERS, centers);显示图像分割结果Mat result = Mat::zeros(src.size(), src.type());Scalar colorTab[] = {Scalar(0,0,255),Scalar(0,255,0),Scalar(255,0,0),Scalar(0,255,255),Scalar(255,0,255)};for (int row = 0; row < height; row++){for (int col = 0; col < width; col++){index = row*width + col;int label = labels.at<int>(index,0);result.at<Vec3b>(row, col)[0] = colorTab[label][0];result.at<Vec3b>(row, col)[1] = colorTab[label][1];result.at<Vec3b>(row, col)[2] = colorTab[label][2];}}for (int i = 0; i < centers.rows; i++){int x = centers.at<float>(i, 0);int y = centers.at<float>(i, 1);cout << "第" << i << "个:" << "c.x" << x << "c.y" << y << endl;}imshow("KMeans_Result", result);waitKey(0);return 0;
}
https://www.cnblogs.com/mikewolf2002/p/3372846.html