【Opencv项目实战】背景替换:帮你的证件照换个背景色

方法一、基于removebg的背景替换

1.1、removebg介绍

removebg官网:一款在线AI自动抠图免费工具(桌面版APP需注册账号)

removebg网站提供了API 接口,可以直接调用并实现抠图。图片必须有一个明显是前景的主题,例如人、动物、产品或汽车(更多信息)。

  • 使用API or APP每个月只有50张图片的免费处理权限;而网站的使用无限制且全免费。
    • 免费处理权限:只支持最高 25 万像素的小分辨率图片(如:625 × 400 像素),想要获得高像素抠图则需要花费credit。用完之后就会拒绝你的API请求。
    • 支持上传最大12 兆字节的任何JPG 或 PNG 图像。如果图像分辨率大于25 兆像素,则会将其调整为该最大分辨率。
    • 可以通过 API 同时处理500 张图像。在这里插入图片描述

1.2、环境配置

详细过程如下:
(1)安装removebg包:pip install removebg
离线安装包下载:Links for removebg
(2)Get API Key

获取 API Key 的详细过程如下:

- 11、removebg官网 - Pricing - Sign up free - 邮箱注册与账号激活。
- 22、登录 - My Account - APP -Keys - New API Key - Create API Key - Copy API Key - Save changes
- (my API Key:2BBwNqN61oZpaPqKmiF2ZGey)

1.3、算法详解

(1)调用API接口:RemoveBg函数完成图片抠图

函数说明:rmbg = RemoveBg(API_Key, "error.log")
输入参数:
				API_Key				字符串格式
				"error.log"			固定格式

(2)调用remove_background_from_img_file函数完成背景替换。

函数说明:rmbg.remove_background_from_img_file(img_file_path, size, bg_color)
输入参数:
				img_file_path		原始图片的路径
				size				输出图片的质量:regular=0.25MP(默认),hd=4MP,4k=up to 10MP。
				bg_color			输出图片的背景(如:green)
输出参数:
				自动保存结果在输入路径

1.4、实战:单张图片背景替换

在这里插入图片描述

from removebg import RemoveBg

rmbg = RemoveBg("YOUR-API-KEY", "error.log")		
rmbg.remove_background_from_img_file("photo.jpg")
# 输出图片,4k分辨率,白色背景

1.5、实战:多张图片背景替换

from removebg import RemoveBg
import os

rmbg = RemoveBg("YOUR-API-KEY", "error.log")
path='%s/picture' %os.getcwd()		# 将生成结果放在当前路径的picture文件夹中
# os.getcwd() 获取当前工作路径

# os.listdir():返回指定的文件夹包含的文件或文件夹的名字的列表。
for pic in os.listdir(path):  		
    rmbg.remove_background_from_img_file('%s\%s' %(path, pic))

removebg包:一键抠图&换证件照底色
Python教程:去除背景(附多种方式)
python代码的打包.exe与自定义安装

1.6、实战:UI自选择图片进行背景替换

在这里插入图片描述

from removebg import RemoveBg
import win32ui

dlg = win32ui.CreateFileDialog(1)           # 1表示打开文件对话框
dlg.SetOFNInitialDir('E:/图片')              # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName()                # 获取选择的文件名称

rmbg = RemoveBg("kRSbRHJZePGWnEBfKeqFbtSz", "error.log")        # 引号内是你获取的API
rmbg.remove_background_from_img_file(filename)                  # 图片地址


方法二、基于backgroundremover的背景替换

2.1、backgroundremover介绍

BackgroundRemover是一个命令行工具,从图像和视频中删除背景。

github源代码:BackgroundRemover
在线使用:BackgroundRemover.app

在这里插入图片描述

2.2、环境配置

安装python - backgroundremover包:pip install backgroundremover

图像或视频的使用方式:通过命令行使用

2.2.1、Image命令行实现

Remove the background from a local file image:

backgroundremover -i "/path/to/image.jpeg" -o "output.png"

图像的高级用法
(1)Sometimes it is possible to achieve better results by turning on alpha matting.

backgroundremover -i "/path/to/image.jpeg" -a -ae 15 -o "output.png"

(2)change the model for diferent background removal methods between u2netp, u2net, or u2net_human_seg

backgroundremover -i "/path/to/image.jpeg" -m "u2net_human_seg" -o "output.png"
# 通过Pycharm调用
import os
os.system('backgroundremover -i "cg.jpg" -o "cg_output.jpg"')

2.2.2、Video命令行实现

(1)remove background from video and make transparent mov

backgroundremover -i "/path/to/video.mp4" -tv -o "output.mov"

(2)remove background from local video and overlay it over other video

backgroundremover -i "/path/to/video.mp4" -tov "/path/to/videtobeoverlayed.mp4" -o "output.mov"

(3)remove background from local video and overlay it over an image

backgroundremover -i "/path/to/video.mp4" -toi "/path/to/videtobeoverlayed.mp4" -o "output.mov"

(4)remove background from video and make transparent gif

backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"

(5)Make matte key file (green screen overlay).Make a matte file for premier

backgroundremover -i "/path/to/video.mp4" -mk -o "output.matte.mp4"

视频的高级用法
(1)Change the framerate of the video (default is set to 30)

backgroundremover -i "/path/to/video.mp4" -fr 30 -tv -o "output.mov"

(2)Set total number of frames of the video (default is set to -1, ie the remove background from full video)

backgroundremover -i "/path/to/video.mp4" -fl 150 -tv -o "output.mov"

(3)Change the gpu batch size of the video (default is set to 1)

backgroundremover -i "/path/to/video.mp4" -gb 4 -tv -o "output.mov"

(4)Change the number of workers working on video (default is set to 1)

backgroundremover -i "/path/to/video.mp4" -wn 4 -tv -o "output.mov"

(5)change the model for diferent background removal methods between u2netp, u2net, or u2net_human_seg and limit the frames to 150

backgroundremover -i "/path/to/video.mp4" -m "u2net_human_seg" -fl 150 -tv -o "output.mov"

猜你喜欢

转载自blog.csdn.net/shinuone/article/details/129244416