代码实现思路:
1.将二维图像数据线性化
2.使用K-means聚类;分离出背景色
3.背景与人物像素二值化
4.腐蚀 + 高斯模糊:图像与背景交汇处高斯模糊化
5.更换背景色以及交汇处融合处理
代码:
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat src = imread("../img/toux.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
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(i, j);
Mat tmp = (Mat_(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(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(index);
if (label == cindex) { // 背景
mask.at(row, col) = 0;
}
else {
mask.at(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.更换背景色以及交汇处融合处理
RNG rng(12345);
Vec3b color; //设置的背景色
color[0] = 217;//rng.uniform(0, 255);
color[1] = 60;// rng.uniform(0, 255);
color[2] = 160;// rng.uniform(0, 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 < src.rows; row++) {
for (int col = 0; col < src.cols; col++) {
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);
return 0;
}
结果:
视频背景替换思路:
视频帧背景替换关键API:
1、 cvtColor(frame, hsv, COLOR_BGR2HSV); //转换颜色域
2、 inRange(hsv, Scalar(35, 43, 46), Scalar(155, 255, 255), OutImg); //提取背景
代码:
#include
#include
using namespace cv;
using namespace std;
Mat replace_and_blend(Mat &frame, Mat &mask);
Mat background_01;
Mat background_02;
int main(int argc, char** argv) {
// start here...
background_01 = imread("D:/vcprojects/images/bg_01.jpg");
background_02 = imread("D:/vcprojects/images/bg_02.jpg");
VideoCapture capture;
capture.open("D:/vcprojects/images/01.mp4");
if (!capture.isOpened()) {
printf("could not find the video file...\n");
return -1;
}
char* title = "input video";
char* resultWin = "result video";
namedWindow(title, CV_WINDOW_AUTOSIZE);
namedWindow(resultWin, CV_WINDOW_AUTOSIZE);
Mat frame, hsv, mask;
int count = 0;
while (capture.read(frame)) {
cvtColor(frame, hsv, COLOR_BGR2HSV);
inRange(hsv, Scalar(35, 43, 46), Scalar(155, 255, 255), mask);
// 形态学操作
Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(mask, mask, MORPH_CLOSE, k);
erode(mask, mask, k);
GaussianBlur(mask, mask, Size(3, 3), 0, 0);
Mat result = replace_and_blend(frame, mask);
char c = waitKey(1);
if (c == 27) {
break;
}
imshow(resultWin, result);
imshow(title, frame);
}
waitKey(0);
return 0;
}
Mat replace_and_blend(Mat &frame, Mat &mask) {
Mat result = Mat::zeros(frame.size(), frame.type());
int h = frame.rows;
int w = frame.cols;
int dims = frame.channels();
// replace and blend
int m = 0;
double wt = 0;
int r = 0, g = 0, b = 0;
int r1 = 0, g1 = 0, b1 = 0;
int r2 = 0, g2 = 0, b2 = 0;
for (int row = 0; row < h; row++) {
uchar* current = frame.ptr(row);
uchar* bgrow = background_02.ptr(row);
uchar* maskrow = mask.ptr(row);
uchar* targetrow = result.ptr(row);
for (int col = 0; col < w; col++) {
m = *maskrow++;
if (m == 255) { // 背景
*targetrow++ = *bgrow++;
*targetrow++ = *bgrow++;
*targetrow++ = *bgrow++;
current += 3;
} else if(m==0) {// 前景
*targetrow++ = *current++;
*targetrow++ = *current++;
*targetrow++ = *current++;
bgrow += 3;
} else {
b1 = *bgrow++;
g1 = *bgrow++;
r1 = *bgrow++;
b2 = *current++;
g2 = *current++;
r2 = *current++;
// 权重
wt = m / 255.0;
// 混合
b = b1*wt + b2*(1.0 - wt);
g = g1*wt + g2*(1.0 - wt);
r = r1*wt + r2*(1.0 - wt);
*targetrow++ = b;
*targetrow++ = g;
*targetrow++ = r;
}
}
}
return result;
}
视频中绿布替换成风景背景: