opencv小案例 --- 证件照背景替换

opencv小案例 --- 证件照背景替换_第1张图片

opencv小案例 --- 证件照背景替换_第2张图片

采用GMM分割也可以。 

代码演示:

#include
#include

using namespace std;
using namespace cv;

Mat mat_to_sample(Mat &image);

int main(int argc, char** argv)
{
	Mat src = imread("E:/技能学习/opencv图像分割/test.jpg");
	if (src.empty())
	{
		cout << "could not load image!" << endl;
		return -1;
	}

	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);

	//数据组装
	Mat points = mat_to_sample(src);

	//运行KMeans
	int numCluster = 4;
	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 mask = Mat::zeros(src.size(), CV_8UC1);

	int index = src.rows * 2 + 2;
	int cindex = labels.at(index, 0);
	int height = src.rows;
	int width = src.cols;

	Mat dst;
	src.copyTo(dst);

	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			index = row * width + col;
			int label = labels.at(index, 0); //得到每一个像素的标签
			if (label == cindex) //背景
			{
				dst.at(row, col)[0] = 0;
				dst.at(row, col)[1] = 0;
				dst.at(row, col)[2] = 0;
			}
			else
			{
				mask.at(row, col) = 255;
			}
		}
	}

	//imshow("mask", mask);
	//imshow("kmeans - result", dst);

	//腐蚀 + 高斯模糊
	Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	erode(mask, mask, k); //腐蚀
//	imshow("erode - mask", mask);
	GaussianBlur(mask, mask, Size(3, 3), 0, 0); //高斯模糊
	//imshow("Blur Mask", mask);

	//通道混合
	RNG rng(12345);
	Vec3b color ;
	//color[0] = rng.uniform(0, 255);
	//color[1] = rng.uniform(0, 255);
	//color[2] = rng.uniform(0, 255);

	color[0] = 0;
	color[1] = 0;
	color[2] = 255;


	Mat result(src.size(), src.type());
	double w = 0.0;
	int b = 0, g = 0, r = 0;
	int b1 = 0, g1 = 0, r1 = 0;
	int b2 = 0, g2 = 0, r2 = 0;

	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			index = row * width + col;
			int label = labels.at(index, 0);

			int m = mask.at(row, col);
			if (m == 255)
			{
				result.at(row, col) = src.at(row, col); //前景
			}
			else if(m == 0)
			{
				result.at(row, col) = color; //背景
			}
			else
			{
				w = m / 255.0;
				b1 = src.at(row, col)[0];
				g1 = src.at(row, col)[1];
				r1 = src.at(row, col)[2];

				b2 = color[0];
				g2 = color[1];
				r2 = color[2];

				b = b1 * w + b2 * (1.0 - w);
				g = g1 * w + g2 * (1.0 - w);
				r = r1 * w + r2 * (1.0 - w);

				result.at(row, col)[0] = b;
				result.at(row, col)[1] = g;
				result.at(row, col)[2] = r;
			}
		}
	}

	imshow("背景替换", result);

	waitKey(0);
	destroyAllWindows();
	return 0;
}

Mat mat_to_sample(Mat &image)
{
	int w = image.cols;
	int h = image.rows;
	int samplecount = w * h;
	int dims = image.channels();
	Mat points(samplecount, dims, CV_32F, Scalar(10));

	int index = 0;
	for (int row = 0; row < h; row++)
	{
		for (int col = 0; col < w; col++)
		{
			index = row * w + col;
			Vec3b bgr = image.at(row, col);
			points.at(index, 0) = static_cast(bgr[0]);
			points.at(index, 1) = static_cast(bgr[1]);
			points.at(index, 2) = static_cast(bgr[2]);
		}
	}
	return points;
}

结果展示:

opencv小案例 --- 证件照背景替换_第3张图片            opencv小案例 --- 证件照背景替换_第4张图片

 

 

 

你可能感兴趣的:(Opencv,opencv,计算机视觉,人工智能)