文章目录
- Part.I Introduction
- Part.II Main_body
- Chap.I 源码简析
- Chap.II 效果展示
- Part.III 源码
- Reference
Part.I Introduction
本文将介绍如何用 OpenCV 更换图片的背景色(附有完整代码)。
Part.II Main_body
Chap.I 源码简析
配置部分:包括输入文件名、输出文件名、需要更换的背景色(可以尝试随机背景色)
string filename_in = "A:/OHanlon/Desktop/1017451354889.png";string filename_out = "A:/OHanlon/Desktop/a.png";Vec3b color; // 设置的背景色RNG rng(12345);color[0] = 255; // rng.uniform(0, 255);color[1] = 255; // rng.uniform(0, 255);color[2] = 255; // rng.uniform(0, 255);
数据处理部分的流程大致如下:
- 将二维图像数据线性化
- 使用 K-means 聚类;分离出背景色
- 背景与前景二值化
- 腐蚀 + 高斯模糊:图像与背景交汇处高斯模糊化
- 更换背景色以及交汇处融合处理
调用 OpenCV 里面的主要的函数
// k 均值聚类
kmeans(data, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS);
// 高斯模糊
GaussianBlur(mask, mask, Size(3, 3), 0, 0);
Chap.II 效果展示
示例一:
存在如下几个问题:
- 图片中的文字和箭头也被当作背景给消掉了
- 非背景和背景的边缘有些模糊,可能做了模糊处理,有可能原图的分辨率就很低
示例二
这个还不错
Part.III 源码
整个源码如下:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>using namespace std;
using namespace cv;bool _changeBackgroundColor()
{
#pragma region BASIC Configurationstring filename_in = "A:/OHanlon/Desktop/1017451354889.png";string filename_out = "A:/OHanlon/Desktop/a.png";Vec3b color; // 设置的背景色RNG rng(12345);color[0] = 255; // b rng.uniform(0, 255);color[1] = 255; // g rng.uniform(0, 255);color[2] = 255; // r rng.uniform(0, 255);
#pragma endregion#pragma region ProcessingMat src = imread(filename_in);if (src.empty()) {printf("could not load image...\n");return false;}namedWindow("原图", 0);cvResizeWindow("原图", 500, 500);imshow("原图", src);// 1.将二维图像数据线性化Mat data;for (int i = 0; i < src.rows; i++) //像素点线性排列for (int j = 0; j < src.cols; j++){Vec3b point = src.at<Vec3b>(i, j);Mat tmp = (Mat_<float>(1, 3) << point[0], point[1], point[2]);data.push_back(tmp);}// 2.使用K-means聚类;分离出背景色int numCluster = 4;Mat labels;TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);kmeans(data, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS);// 3.背景与人物二值化Mat mask = Mat::zeros(src.size(), CV_8UC1);int index = src.rows * 2 + 2; //获取点(2,2)作为背景色int cindex = labels.at<int>(index);/* 提取背景特征 */for (int row = 0; row < src.rows; row++){for (int col = 0; col < src.cols; col++){index = row * src.cols + col;int label = labels.at<int>(index);if (label == cindex) { // 背景mask.at<uchar>(row, col) = 0;}else {mask.at<uchar>(row, col) = 255;}}}//imshow("mask", mask);// 4.腐蚀 + 高斯模糊:图像与背景交汇处高斯模糊化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);// 5.更换背景色以及交汇处融合处理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 < src.rows; row++){for (int col = 0; col < src.cols; col++){int m = mask.at<uchar>(row, col);if (m == 255) {result.at<Vec3b>(row, col) = src.at<Vec3b>(row, col); // 前景}else if (m == 0) {result.at<Vec3b>(row, col) = color; // 背景}else {/* 融合处理部分 */w = m / 255.0;b1 = src.at<Vec3b>(row, col)[0];g1 = src.at<Vec3b>(row, col)[1];r1 = src.at<Vec3b>(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<Vec3b>(row, col)[0] = b;result.at<Vec3b>(row, col)[1] = g;result.at<Vec3b>(row, col)[2] = r;}}}
#pragma endregionnamedWindow("背景替换", 0);cvResizeWindow("背景替换", 500, 500);imshow("背景替换", result);imwrite(filename_out, result);waitKey(0);return true;
}
Reference
- OpenCV案例(五): 更换背景色