Mat source;
Mat points(image_width*image_height, 1, CV_32F, Scalar(10));
int ind = 0;
for (int row = 0; row < image_height; row++) {
for (int col = 0; col < image_width; col++) {
ind = row * image_width + col;//
points.at<float>(ind, 0) = source.at<float>(row, col);
}
}
//运行kmeans
int numCluster = 2;
Mat labels;
Mat centers;
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
kmeans(points, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS, centers);
// 显示图像分割后的结果,一维转多维
Mat result = Mat::zeros(Filter_diff.size(), Filter_diff.type());
for (int row = 0; row < image_height; row++)
{
for (int col = 0; col < image_width; col++)
{
int index = row * image_width + col;
int label = labels.at<int>(index, 0);
result.at<float>(row, col) = label;
}
}