1、輸入原始圖片
2、代碼實現:
#include<opencv2\opencv.hpp> #include<iostream> using namespace std; using namespace cv; int main() { Mat src = imread("C:/Users/lzg/Desktop/opencv_test/Project1/1.png"); if (src.empty()) { cout << "meiyoutu" << endl; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); Scalar colorTab[] = { Scalar(0,0,255), Scalar(0,255,0), Scalar(255,0,0), Scalar(0,255,255), Scalar(255,0,255) }; 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()); int index = 0; for (int row = 0; row < height; row++) { //RGB數據轉換到樣本數據 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]); } } //運行Kmeans TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1); //迭代停止條件 kmeans(points, clusterCount, labels, criteria, 3, KMEANS_PP_CENTERS, centers); //顯示圖像分割結果,要把樣本數據點轉換回去 Mat result = 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<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]; } } imshow("KMeans image segmentation demo", result); waitKey(0); return 0;
效果圖: