// Kmeans.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
void Ml_Kmeans2();
void test();
int main()
{
cv::Mat img = cv::imread("2.png");
cv::namedWindow("source image");
cv::imshow("source image", img);
//生成一维采样点,包括所有图像像素点,注意采样点格式为32bit浮点数
cv::Mat samples(img.cols * img.rows, 1, CV_32FC3);
//cv::Mat samples(img.cols*img.rows, img.channels(), CV_32F);
//标记矩阵,32位整形
cv::Mat labels(img.cols * img.rows, 1, CV_32SC1);
uchar* p;
int index = 0;
//k记录的是图像的一维采样点的维度
int i, j, k = 0;
for (i = 0; i < img.rows; i++)
{
p = img.ptr<uchar>(i);
for (j = 0; j < img.cols; j++)
{
samples.at<cv::Vec3f>(k, 0)[0] = float(p[j * 3]);
samples.at<cv::Vec3f>(k, 0)[1] = float(p[j * 3 + 1]);
samples.at<cv::Vec3f>(k, 0)[2] = float(p[j * 3 + 2]);
k++;
}
}
//聚类数为4
int clusterCount = 4;
//存储聚类后的中心点
cv::Mat centers(clusterCount, 1, samples.type());
kmeans(samples, clusterCount, labels,
cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
3, //执行次数
cv::KMEANS_PP_CENTERS, //随机初始化的方法
centers //输出最终的均值点
);
//我们已知有3个聚类,用不同的灰度层表示。
//以下部分是用来做结果显示,根据label标签的值来输出结果
cv::Mat img1(img.rows, img.cols, CV_8UC1);
float step = 255 / (clusterCount - 1);
k = 0;
for (i = 0; i < img1.rows; i++)
{
p = img1.ptr<uchar>(i);
for (j = 0; j < img1.cols; j++)
{
int tt = labels.at<int>(k, 0);
k++;
p[j] = 255 - tt * step;
}
}
cv::namedWindow("K-Means分割效果");
imshow("K-Means分割效果", img1);
cv::waitKey();
//Ml_Kmeans2();
//test();
return 0;
}
void Ml_Kmeans2()//39.k_means K均值聚类2
{
Mat src = imread("2.png");
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 rgb = src.at<Vec3b>(row, col);
points.at<float>(index, 0) = static_cast<int>(rgb[0]);
points.at<float>(index, 1) = static_cast<int>(rgb[1]);
points.at<float>(index, 2) = static_cast<int>(rgb[2]);
}
}
// 运行K-Means数据分类
TermCriteria criteria = TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0);
kmeans(points, clusterCount, labels, criteria, 3, KMEANS_PP_CENTERS, centers);
// 显示图像分割结果
Mat result = Mat::zeros(src.size(), CV_8UC3);
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);
if (label == 1)
{
result.at<Vec3b>(row, col)[0] = 255;
result.at<Vec3b>(row, col)[1] = 0;
result.at<Vec3b>(row, col)[2] = 0;
}
else if (label == 2)
{
result.at<Vec3b>(row, col)[0] = 0;
result.at<Vec3b>(row, col)[1] = 255;
result.at<Vec3b>(row, col)[2] = 0;
}
else if (label == 3)
{
result.at<Vec3b>(row, col)[0] = 0;
result.at<Vec3b>(row, col)[1] = 0;
result.at<Vec3b>(row, col)[2] = 255;
}
else if (label == 0)
{
result.at<Vec3b>(row, col)[0] = 0;
result.at<Vec3b>(row, col)[1] = 255;
result.at<Vec3b>(row, col)[2] = 255;
}
}
}
imshow("kmeans-demo", result);
waitKey(0);
}
void test()
{
Mat img(5, 2, CV_32FC3);
img.at<float>(0, 0) = 0;
img.at<float>(0, 1) = 1;
img.at<float>(0, 2) = 2;
img.at<float>(1, 0) = 1;
img.at<float>(2, 0) = 2;
img.at<float>(3, 0) = 3;
Mat img2(4, 6, CV_32F);
img2.at<float>(0, 0) = 0;
img2.at<float>(0, 1) = 1;
img2.at<float>(0, 2) = 2;
img2.at<float>(1, 0) = 1;
img2.at<float>(2, 0) = 2;
img2.at<float>(3, 1) = 3;
std::cout << img << ";" << std::endl << std::endl;
getchar();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Kmeans.zip (28个子文件)
Kmeans
.vs
Kmeans
v15
ipch
c08554ab725192ea.ipch 320KB
2f79593236b77237.ipch 320KB
.suo 19KB
Browse.VC.db 21.15MB
Kmeans
Kmeans.vcxproj.filters 1KB
pch.h 639B
Kmeans.cpp 4KB
pch.cpp 200B
2.png 431KB
Kmeans.vcxproj.user 165B
x64
Debug
vc141.pdb 1.82MB
Kmeans.log 1001B
Kmeans.pch 1.94MB
Kmeans.obj 531KB
pch.obj 3KB
vc141.idb 339KB
Kmeans.tlog
CL.read.1.tlog 29KB
link.read.1.tlog 3KB
Kmeans.lastbuildstate 209B
link.write.1.tlog 604B
CL.write.1.tlog 1KB
link.command.1.tlog 1KB
CL.command.1.tlog 2KB
Kmeans.vcxproj 8KB
x64
Debug
Kmeans.pdb 732KB
Kmeans.ilk 738KB
Kmeans.exe 150KB
Kmeans.sln 1KB
共 28 条
- 1
资源评论
青山渺渺
- 粉丝: 1990
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功