fanfuhan OpenCV 教學111 ~ opencv-111-KMeans圖像分割
fanfuhan OpenCV 教學111 ~ opencv-111-KMeans圖像分割
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/23/opencv-111/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
KMean不光可以對數據進行分類,還可以實現對圖像分割,什麼圖像分割,簡單的說就要圖像的各種像素值,分割為幾個指定類別顏色值,
這種分割有兩個應用,一個可以實現圖像主色彩的簡單提取,
另外針對特定的應用場景可以實現證件照片的背景替換效果,這個方面早期最好的例子就是證件之星上面的背景替換。
當然要想實現類似的效果,絕對不是簡單的KMeans就可以做到的,還有一系列後續的交互操作需要完成。
對圖像數據來說,要把每個像素點作為單獨的樣本,按行組織。
C++
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src = imread("D:/projects/opencv_tutorial/data/images/toux.jpg"); if (src.empty()) { printf("could not load image...\n"); return -1; } namedWindow("input image", WINDOW_AUTOSIZE); imshow("input image", 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 = 3; Mat labels; Mat centers; // RGB 数据转换到样本数据 Mat sample_data = src.reshape(3, sampleCount); Mat data; sample_data.convertTo(data, CV_32F); // 运行K-Means TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1); kmeans(data, clusterCount, labels, criteria, clusterCount, KMEANS_PP_CENTERS, centers); // 显示图像分割结果 int index = 0; 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-Demo", result); waitKey(0); return 0; }
Python
""" KMeans 图像分割 """ import cv2 as cv import numpy as np image = cv.imread('images/toux.jpg') cv.imshow("input", image) # 构建图像数据 data = image.reshape((-1, 3)) data = np.float32(data) # 图像分割 criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0) num_clusters = 4 ret, label, center = cv.kmeans(data, num_clusters, None, criteria, num_clusters, cv.KMEANS_RANDOM_CENTERS) center = np.uint8(center) res = center[label.flatten()] # 显示 result = res.reshape((image.shape)) cv.imshow("result", result) cv.waitKey(0) cv.destroyAllWindows()