OpenCV利用K-means实现根据颜色进行图像分割
K-means算法分割
K-means是一种经典的无监督聚类算法---不需要人工干预。
算法原理:
(1)随机选择两个中心点;
(2)计算每个点到这两个中心点的距离,最近的分成一类(连接起来);
(3)重新计算中心点(平均值计算),计算新的中心点到旧的中心点的差值如果小于输入的值,就说明中心的位置发生了变化,,那么到(2)步重新计算中心点到每个点的距离,开始下一次循环;
(4)执行多个迭代之后,满足收敛时,得到最终的分类
应用:分类
根据颜色分类
#include<OpenCV2/opencv.hpp>
#include<iOStream>
int main(int arGC, char** argv) {
cv::Mat img(500, 500, CV_8UC3);
cv::RNG rng(12345);
cv::Scalar colorTab[] = {
cv::Scalar(0, 0, 255),
cv::Scalar(0, 255, 0),
cv::Scalar(255, 0, 0),
cv::Scalar(0, 255, 255),
cv::Scalar(255, 0, 255)
};
int numCluster = rng.unifORM(2, 5);
printf("种类数量 : %d\n", numCluster);
//4
int sampleCount = rng.uniform(2, 1000);//随机样本
printf("样本数量 : %d\n", sampleCount);
//591
cv::Mat points(sampleCount, 1, CV_32FC2);
cv::Mat labels;
cv::Mat centers;
for (int k = 0; k < numCluster; k++) {
cv::Point center;
center.x = rng.uniform(0, img.cols);//随机坐标
center.y = rng.uniform(0, img.rows);
cv::Mat pointChunk = points.rowRange(k * sampleCount / numCluster, k == numCluster - 1 ? sampleCount : (k + 1) * sampleCount / numCluster);
//每一类占1/numCluster 行
//rng.fill(pointChunk, cv::RNG::NORMAL, cv::Scalar(center.x, center.y), cv::Scalar(img.cols * 0.05, img.rows * 0.05));//用随机数填充矩阵
rng.fill(pointChunk, cv::RNG::UNIFORM, 0, 255);//用随机数填充矩阵
}
randShuffle(points, 1, &rng);//算法打乱元素排列顺序
kmeans(points, numCluster, labels, cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 10, 0.1), 3, cv::KMEANS_PP_CENTERS, centers);
// 用不同颜色显示分类
//初始化图片颜色。
img = cv::Scalar::all(255);
for (int i = 0; i < sampleCount; i++) {
int index = labels.at<int>(i);
//获取ponint点
cv::Point p = points.at<cv::Point2f>(i);
//填充
circle(img, p, 2, colorTab[index], -1, 8);
}
// 每个聚类的中心来绘制圆
for (int i = 0; i < centers.rows; i++) {
int x = centers.at<float>(i, 0);
int y = centers.at<float>(i, 1);
printf("c.x= %d, c.y=%d", x, y);
circle(img, cv::Point(x, y), 40, colorTab[i], 1, cv::LINE_AA);
}
imshow("KMeans-Data-Demo", img);
cv::waiTKEy(0);
return 0;
}
实例
2.png
#include<opencv2/opencv.hpp>
#include<iostream>
int main(int argc, char** argv) {
cv::Scalar colorTab[] = {
cv::Scalar(0, 0, 255),
cv::Scalar(0, 255, 0),
cv::Scalar(255, 0, 0),
cv::Scalar(0, 255, 255),
};
cv::Mat src = cv::imread("D:/bb/tu1/2.png");
if (!src.data)
{
printf("图像读取失败...\n");
return -1;
}
cv::imshow("src", src);
int width = src.cols;
int height = src.rows;
int dims = src.channels();
int sampleCount = width * height; //总像素
int clusterCount = 4; //分类数量
cv::Mat points(sampleCount, dims,CV_32F,cv::Scalar(10));
//一个像素为一行
cv::Mat labels;
cv::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;
cv::Vec3b bgr = src.at<cv::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]);
}
}
cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS,10,0.1);
//kmeans的终止的条件
kmeans(points, clusterCount, labels, criteria, 3, cv::KMEANS_PP_CENTERS, centers);//运行kmeans
//显示图像分割结果
cv::Mat result = cv::Mat::zeros(src.size(),src.type());
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<cv::Vec3b>(row, col)[0] = colorTab[label][0];
result.at<cv::Vec3b>(row, col)[1] = colorTab[label][1];
result.at<cv::Vec3b>(row, col)[2] = colorTab[label][2];
}
}
cv::imshow("result", result);
cv::waitKey(0);
return 0;
}
以上就是OpenCV利用K-means实现根据颜色进行图像分割的详细内容,更多关于OpenCV K-means图像分割的资料请关注其它相关文章!
相关文章