AI实现移除视频复杂背景-Background-Matting(1)

引用来源:
@InProceedings{BMSengupta20,
title={Background Matting: The World is Your Green Screen},
author = {Soumyadip Sengupta and Vivek Jayaram and Brian Curless and Steve Seitz and Ira Kemelmacher-Shlizerman},
booktitle={Computer Vision and Pattern Regognition (CVPR)},
year={2020}
}
链接:The World is Your Green Screen

视频背景移除 background matting

从一幅图像中分离前景和背景,俗称抠像。本文所用的视频背景移除方法,基于静止图像的抠像方法,将组成视频的单帧图像中抠出前景,再把这些单帧的前景组成视频,实现视频背景移除。
背景移除需要两张图片,
本方法需要一幅完全的背景图像,作为抠前景的基准背景。先利用tensorflow的分割模型Deeplabv3,获得图像的粗略alpha遮罩,然后对alpha遮罩精细化,由此抠出图像前景。因此,本方法需预先获得背景图像,且背景图像在视频拍摄过程中稳定不变(或可以有微小变化)。

准备工作

从github克隆项目到ubuntu主目录,产生工作目录Background-Matting

git clone https://github.com/senguptaumd/Background-Matting.git

采用conda安装pytorch 1.1.0,tensorflow-gpu 1.14.0

conda create --name back-matting python=3.6
conda activate back-matting

在虚拟环境[back-matting]下安装pytorch和tensorflow-gpu

conda install pytorch=1.1.0 torchvision cudatoolkit=10.0 -c pytorch
pip install tensorflow-gpu==1.14.0
pip install -r requirements.txt

requirements.txt文件在Background-Matting目录下,安装移除背景程序运行所需的python模块。

numpy==1.17.0
opencv-python==3.4.5.20
pandas
Pillow==6.1
scikit-image==0.14.2
scipy==1.2.1
tqdm
tensorboard

单帧图像的推理算法

准备如下数据
实现背景移除,需要以下图像:

(a) 完整的图像(use _img.png)
(b) 无前景的背景图像 ( _back.png )
©需替换的背景图像 (放在 sample_data/background)

sample_data作为测试目录。
sample_data/input下放置:
需要抠像的图像 _img.png,
无前景的背景图像_back.png,
替换背景的图像放到sample_data/background.

预先训练的模型

在Background-Matting下建立Models目录,存放预先训练好的模型。
来源:Google Drive
将模型分别存放到Models/real-fixed-cam, Models/real-hand-held 目录。

预处理

1 分割-Segmentation
背景移除需建立前景的分割遮罩。此处用tensorflow的模型 Deeplabv3+

cd Background-Matting/
git clone https://github.com/tensorflow/models.git
cd models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
cd ../.. 
python test_segmentation_deeplab.py -i sample_data/input

这样在Background-Matting目录下建立了两个模型models和Models,models存放tensorflow的通用模型(含Deeplabv3+),Models存放Background-Matting精细处理alpha遮罩的模型。

注:在test_segmentation_deeplab.py中,若需要,应添加限制显存使用的代码,防止GPU显存溢出。
该操作把Deeplabv3下载,放到deeplab_model目录。对输入图像产生遮罩 _masksDL.png。

2 校准 Alignment
对手持摄像机,需要把纯背景和带前景和背景的图像对准。如果是固定摄像机拍摄,则不需要校准。拍摄应关闭摄像机自动对焦和自动曝光功能,防止背景图像产生变化。

python test_pre_process.py -i sample_data/input

背景移除Background Matting

python test_background-matting_image.py -m real-hand-held -i sample_data/input/ -o sample_data/output/ -tb sample_data/background/0001.png

对固定摄像机选择 -m real-fixed-cam

图片
图片左边分别是背景图和输入的抠像图片,右边是抠出的前景并叠加新的背景图片。

视频背景移除的操作步骤

去除背景,视频与静止图像的操作相同。首先把视频转换成一组单帧图像,对每帧图像实施分割,校准,去背景。再把去除背景的单帧图像合成视频。

数据准备

(a) 带前景和背景的视频 (teaser.mov)
(b) 无前景的背景图像 ( teaser_back.png )
©与前景融合的目标背景 ( target_back.mov)
作者提供了手持摄像机的样本视频:sample_video/
固定摄像机样本视频:sample_video_fixed/
下载源: 下载数据

预处理

1 帧抽取:

cd Background-Matting/sample_video
mkdir input background
ffmpeg -i teaser.mov input/%04d_img.png -hide_banner
ffmpeg -i target_back.mov background/%04d.png -hide_banner

通过ffmpeg处理,把输入视频和目标背景视频分解成单帧图片,分别放到input和background目录。

2 Segmentation
进入Background-Matting目录

python test_segmentation_deeplab.py -i sample_video/input

对input目录中每帧图像产生一个alpha遮罩 _masksDL.png。

3 Alignment

python test_pre_process_video.py -i sample_video/input -v_name sample_video/teaser_back.png 

对基准背景teaser_back.png进行微调,适应input中每幅图像的背景变化,即每幅图像都产生一个对应的背景png。
对固定摄像机拍摄的视频,可省略校准过程。

背景移除

手持摄像机的视频, 使用sample_video:

python test_background-matting_image.py -m real-hand-held -i sample_video/input/ -o sample_video/output/ -tb sample_video/background

针对real-hand-held模式,经alignment处理,在input中,每个主体图片都跟随一张经校准的背景,因此没有基准背景的-b选项。

固定摄像机视频,使用 sample_video_fixed:

python test_background-matting_image.py -m real-fixed-cam -i sample_video_fixed/input/ -o sample_video_fixed/output/ -tb sample_video_fixed/background/ -b sample_video_fixed/teaser_back.png

要获得输出视频,用ffmpeg把单帧图片合成视频:

cd Background-Matting/sample_video

获得去背景,叠加单色背景的视频:

ffmpeg -r 60 -f image2 -i output/input%04d_matte.png -vcodec libx264 -crf 15 -s 1280x720 -pix_fmt yuv420p teaser_matte.mp4

前景融合目标背景的视频获取:

ffmpeg -r 60 -f image2 -i output/input%04d_compose.png -vcodec libx264 -crf 15 -s 1280x720 -pix_fmt yuv420p teaser_compose.mp4

拍摄视频的注意事项:

1 选择稳定的背景,背景是静止不变的。若拍摄过程中背景光照的变化,背景物的变化,前景运动产生的阴影等都会影响处理效果。
2 不要离背景太近,避免产生阴影。
3 关闭摄像机自动对焦和自动光照控制,避免因前景运动而产生背景焦点变化或光照变化。

经本人验证,本方法有一定移除背景效果,但直接使用该模型对拍摄的视频有要求,例如前景人物与背景要有区分,若背景中有大片白色,则前景的人物不要穿浅色衣服,着深色衣服效果就好很多。运行test_background-matting_image.py时,对原视频质量有所降低。本人计算机GPU为rtx2060 8GB,而作者机器的GPU为rtx2080,故分割一段30s 1080p视频,产生预先遮罩需耗时8min。background-matting则耗时约1000s(1140幅1920x1080png图片),此处理只占用25%GPU资源。因此,作者提供的github代码只是演示代码,不能实时处理高分辨率视频。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Deep Image Matting 是一种图像分割的技术,其目的是将前景对象从背景中分离出来并以 alpha 通道的形式输出。下面是 Deep Image Matting 的使用教程: 1. 下载模型:可以从代码仓库中下载训练好的 Deep Image Matting 模型模型文件名为“model.pth”。 2. 准备图像和背景:准备需要进行分割的图像和对应的背景图像。 3. 安装依赖:在 Python 环境中安装 PyTorch 和依赖项,例如 NumPy、Pillow 和 matplotlib 等。 4. 运行代码:将图像和背景图像作为输入,运行分割代码。输出将是包含 alpha 通道的图像。 以下是一个示例代码,其中 image_path 和 background_path 分别为图像和背景图像的路径: ```python import torch import numpy as np from PIL import Image # 加载模型 model = torch.load("model.pth") # 加载图像和背景 image = Image.open(image_path) background = Image.open(background_path) # 将图像和背景转换为张量 image_tensor = torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0).float() / 255 background_tensor = torch.tensor(np.array(background)).permute(2, 0, 1).unsqueeze(0).float() / 255 # 运行模型 output_tensor = model(image_tensor, background_tensor) # 将结果转换为图像 output_array = (output_tensor.squeeze().detach().cpu().numpy() * 255).astype(np.uint8) output_image = Image.fromarray(output_array, mode="L") # 显示结果 output_image.show() ``` 注意,运行代码需要一定的硬件资源和时间,尤其是对于大尺寸的图像。另外,模型的预测精度也取决于训练数据的质量和多样性。因此,在使用 Deep Image Matting 进行实际应用时,需要对数据进行精细的处理和优化。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值