opencv-112-利用KMeans图像分割进行背景替换 发表于 2019-05-24 | 分类于 opencv 知识点KMeans可以实现简单的证件照片的背景分割提取与替换,大致可以分为如下几步实现 读入图像建立KMenas样本 使用KMeans图像分割,指定指定分类数目 取左上角的label得到背景cluster index 生成mask区域,然后高斯模糊进行背景替换 代码(c++,python)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#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); 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); Mat mask = Mat::zeros(src.size(), CV_8UC1); int index = labels.at<int>(0, 0); labels = labels.reshape(1, height); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int c = labels.at<int>(row, col); if (c == index) { mask.at<uchar>(row, col) = 255; } } } Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); dilate(mask, mask, se); GaussianBlur(mask, mask, Size(5, 5), 0); Mat result = Mat::zeros(src.size(), CV_8UC3); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { float w1 = mask.at<uchar>(row, col) / 255.0; Vec3b bgr = src.at<Vec3b>(row, col); bgr[0] = w1 * 255.0 + bgr[0] * (1.0 - w1); bgr[1] = w1 * 0 + bgr[1] * (1.0 - w1); bgr[2] = w1 * 255.0 + bgr[2] * (1.0 - w1); result.at<Vec3b>(row, col) = bgr; } } imshow("KMeans-image-Demo", result); waitKey(0); return 0;} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748"""利用KMeans图像分割进行背景替换"""import cv2 as cvimport numpy as npimage = cv.imread('images/toux.jpg')cv.imshow("input", image)h, w, ch = image.shape# 构建图像数据data = image.reshape((-1, 3))data = np.float32(data)# 图像分割criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)num_clusters = 4ret, label, center = cv.kmeans(data, num_clusters, None, criteria, num_clusters, cv.KMEANS_RANDOM_CENTERS)# 生成mask区域index = label[0][0]center = np.uint8(center)color = center[0]mask = np.zeros((h, w), dtype=np.uint8)label = np.reshape(label, (h, w))mask[label == index] = 255# 高斯模糊se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))cv.dilate(mask, se, mask)mask = cv.GaussianBlur(mask, (5, 5), 0)cv.imshow("background-mask", mask)# 背景替换result = np.zeros((h, w, ch), dtype=np.uint8)for row in range(h): for col in range(w): w1 = mask[row, col] / 255.0 b, g, r = image[row, col] b = w1 * 255 + b * (1.0 - w1) g = w1 * 0 + g * (1.0 - w1) r = w1 * 255 + r * (1.0 - w1) result[row, col] = (b, g, r)cv.imshow("background-substitution", result)cv.waitKey(0)cv.destroyAllWindows() 结果 代码地址github