fanfuhan OpenCV 教學112 ~ opencv-112-利用KMeans圖像分割進行背景替換 [去背/分割 前景/背景]

fanfuhan OpenCV 教學112 ~ opencv-112-利用KMeans圖像分割進行背景替換 [去背/分割 前景/背景]

fanfuhan OpenCV 教學112 ~ opencv-112-利用KMeans圖像分割進行背景替換 [去背/分割 前景/背景]


資料來源: https://fanfuhan.github.io/

https://fanfuhan.github.io/2019/05/24/opencv-112/

GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV


KMeans可以實現簡單的證件照片的背景分割提取與替換,大致可以分為如下幾步實現

 01.讀入圖像建立KMenas樣本
 02.使用KMeans圖像分割,指定指定分類數目
 03.取左上角的label得到背景cluster index

 04.生成mask區域,然後高斯模糊進行背景替換


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);

	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;
}


Python

"""
利用KMeans图像分割进行背景替换
"""

import cv2 as cv
import numpy as np

image = 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 = 4
ret, 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()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *