import cv2
import mediapipe as mp
import numpy as np
mp_drawing = mp.solutions.drawing_utils
mp_selfie_segmentation = mp.solutions.selfie_segmentation
# 图片人物抠图:
IMAGE_FILES = ["face_example_2.jpg"]
BG_COLOR = (0, 255, 0) # 背景颜色也可以使用其他的照片,要求与原照片尺寸一致
bg_image = cv2.imread('seasand3.jpg')
height, width ,_ = bg_image.shape
MASK_COLOR = (255, 255, 255) # mask图片颜色
with mp_selfie_segmentation.SelfieSegmentation(model_selection=0) as selfie_segmentation:
for idx, file in enumerate(IMAGE_FILES):
image = cv2.imread(file)
image = cv2.resize(image,(width,height))
# 在处理之前需要转换图片到RGB颜色空间
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = selfie_segmentation.process(image)
# 在背景图像上绘制分割图
#为了改善边界周围的分割,可以考虑在 results.segmentation_mask进行双边过滤
# np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1最后参数越小,包括的边缘越多
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.3
#生成纯色图像,白色的mask图纸
#fg_image = np.zeros(image.shape, dtype=np.uint8)
#fg_image[:] = MASK_COLOR
fg_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if bg_image is None:
# 背景为纯色
bg_image = np.zeros(image.shape, dtype=np.uint8)
bg_image[:] = BG_COLOR
output_image = np.where(condition, fg_image, bg_image)
cv2.imshow('output_image',output_image)
cv2.imwrite('output_image' + '.jpg', output_image)
cv2.waitKey(0)
#cv2.imwrite('selfie0.png', output_image)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Segmentation.zip (5个子文件)
output_image.jpg 220KB
face_example_2.jpg 245KB
stream_video.py 1KB
static_image.py 2KB
seasand3.jpg 87KB
共 5 条
- 1
资源评论
kings802
- 粉丝: 3
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功