一、绿布抠图-背景图替换
主要是基于图像色彩空间,转换为HSV色彩空间实现mask层提取、然后通过一些简单的图像处理比如形态学开闭操作、高斯模糊等到完整的mask区域,运用mask区域生成权重系数,对绿幕图像与背景图像重新融合生成一个新输出图像,完成抠图。
自己代码实现 代码实践
//视频绿布切割背景替换
void QuickDemo::beijing_tihuan_demo()
{
VideoCapture captureCamear("D:/01.mp4");
//get视频属性 注意也是可以set设置的 但是注意如果是摄像头的话他硬件是否支持set的参数
int frame_width = captureCamear.get(CAP_PROP_FRAME_WIDTH);
int frame_height = captureCamear.get(CAP_PROP_FRAME_HEIGHT);
int frame_counts = captureCamear.get(CAP_PROP_FRAME_COUNT);
int fps = captureCamear.get(CAP_PROP_FPS);
std::cout << "frame_width = " << frame_width << std::endl;
std::cout << "frame_height = " << frame_height << std::endl;
std::cout << "frame_counts = " << frame_counts << std::endl;
std::cout << "fps = " << fps << std::endl;
//CAP_PROP_FOURCC 获取原来的编码类型 注意size的大小不能变
VideoWriter writer("D:/test.mp4", captureCamear.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
VideoCapture Camerwriter("D:/lane.avi");
Mat frameCamear;
Mat frameVedio;
while (true)
{
captureCamear.read(frameCamear);//读取视频 每一帧 从而操作视频就是操作图像了
Camerwriter.read(frameVedio);
Mat hsv;
cvtColor(frameCamear, hsv, COLOR_BGR2HSV);
Mat mask;
inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask);//输入hsv图像、 根据绿色作为分界则传入hsv绿色的hsv最大最下值
//imshow("inRange mask", mask);//只有黑白 把绿色都变成1白色 其他变成0黑色
resize(frameVedio, frameVedio, Size(frame_width, frame_height), 0, 0, INTER_LINEAR);
bitwise_not(mask, mask);//取反 黑变白 白变黑 就是相当于把绿色部分变成0黑色、人部分变成1白色
frameCamear.copyTo(frameVedio, mask);
writer.write(frameVedio);//写入文件
if (frameVedio.empty())
{
break;
}
imshow("frame", frameVedio);
int c = waitKey(10);
if (c == 27)
break;
}
//captureVedio.release();
captureCamear.release();//摄像头资源需要释放
writer.release();//视频写入变量也需要释放资源
}
参考贾志刚老师的代码实践 代码实践
//加入高斯模糊的绿布抠图替换
Mat background_01;
Mat background_02;
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.0;
int r = 0, g = 0, b = 0;
int b1 = 0, g1 = 0, r1 = 0;
int b2 = 0, g2 = 0, r2 = 0;
for (int row = 0; row < h; row++) {
uchar* current = frame.ptr<uchar>(row);
uchar* bgrow = background_01.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;
}
void QuickDemo::beijing_tihuan_demo1()
{
background_01 = imread("D:/8519639.jpg");
background_02 = imread("D:/2.png");
resize(background_02, background_02, Size(background_01.cols, background_01.rows));
VideoCapture capture;
capture.open("D:/01.mp4");
if (!capture.isOpened()) {
printf("could not load video file...\n");
return ;
}
VideoCapture captureTwo;
captureTwo.open("D:/dushuhu.mp4");
char* title = "input video";
char* matting_title = "video matting result";
namedWindow(title, WINDOW_AUTOSIZE);
namedWindow(matting_title, WINDOW_AUTOSIZE);
Mat frame, hsv, mask;
Mat frame2;
int count = 0;
Size S = Size((int)capture.get(CAP_PROP_FRAME_WIDTH),
(int)capture.get(CAP_PROP_FRAME_HEIGHT));
while (capture.read(frame)) {
if (captureTwo.read(frame2)) {
resize(frame2, background_01, frame.size());
}
else {
background_02.copyTo(background_01);//如果背景视频播放完毕就是要背景 图片替代
}
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(title, frame);
imshow(matting_title, result);
}
waitKey(0);
return ;
}
|