本文主要是介绍OpenCV案例(五): 更换背景色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、证件背景替换
代码实现思路:
1.将二维图像数据线性化
2.使用K-means聚类;分离出背景色
3.背景与人物像素二值化
4.腐蚀 + 高斯模糊:图像与背景交汇处高斯模糊化
5.更换背景色以及交汇处融合处理
代码:
#include <opencv2/opencv.hpp>
#include <iostream>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<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.更换背景色以及交汇处融合处理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<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;}}}imshow("背景替换", result);waitKey(0);return 0;
}
结果:
2、视频背景替换
视频背景替换思路:
- Take each frame of the video
- Convert from BGR to HSV color-space
- We threshold the HSV image for a range of blue color
- Now extract the blue object alone, we can do whatever on that image we want.
视频帧背景替换关键API:
1、 cvtColor(frame, hsv, COLOR_BGR2HSV); //转换颜色域
2、 inRange(hsv, Scalar(35, 43, 46), Scalar(155, 255, 255), OutImg); //提取背景
代码:
#include <opencv2/opencv.hpp>
#include <iostream>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 blendint 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<uchar>(row);uchar* bgrow = background_02.ptr<uchar>(row);uchar* maskrow = mask.ptr<uchar>(row);uchar* targetrow = result.ptr<uchar>(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;
}
视频中绿布替换成风景背景:
这篇关于OpenCV案例(五): 更换背景色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!