[python opencv video抠图并更换背景]

 完整实现

- 可以对前景进行缩放

- 可以对前景进行平移

import cv2
import numpy as np


def adjust_img(img, position_params, size_rate):
    """调整图像
    必须先缩小-再平移

    :param img:
    :param position_params:
    :param size_rate:
    :return:
    """
    h, w = img.shape[:2]

    # 进行缩放
    new_size = [int(size_rate * h), int(size_rate * w)]
    img = cv2.resize(img, (new_size[1], new_size[0]))
    if w >= new_size[1]:
        pad_w = w - new_size[1]
        pad_h = h - new_size[0]
        top, bottom = pad_h // 2, pad_h - (pad_h // 2)
        left, right = pad_w // 2, pad_w - (pad_w // 2)
        img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT,
                                 None,
                                 (0, 0, 0))
    else:
        crop_w = (new_size[1] - w) // 2
        crop_h = (new_size[0] - h) // 2

        img = img[crop_h:crop_h + h, crop_w:crop_w + w]

    # 进行平移
    MAT = np.float32(
        [[1, 0, int(w * position_params[1])], [0, 1, int(h * position_params[0])]])  # 构造平移变换矩阵
    img = cv2.warpAffine(img, MAT, (w, h), borderValue=(0, 0, 0))  # 设置白色填充

    return img


def video_compositing(video_root, mask_root, output_path, background_root, position_params,
                      size_rate):
    """

    :param video_root:
    :param mask_root: mask path
    :param output_path: 输出位置
    :param background_root: 背景图像path
    :param position_params: [0.5, 0.5], [垂直,水平]控制人像在图像中的位置, 计算例如:水平移动像素数/水平像素总数
    :param size_rate: 控制缩放大小,1:原始大小
    :return:
    """

    video = cv2.VideoCapture(video_root)
    mask = cv2.VideoCapture(mask_root)

    if not video.isOpened():
        print(f"Failed to open video file: {video_root}")
        return

    if not mask.isOpened():
        print(f"Failed to open mask file: {mask_root}")
        return

    rval, frame = video.read()
    rval_mask, frame_mask = mask.read()

    fps = video.get(cv2.CAP_PROP_FPS)
    h, w = frame.shape[:2]

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
    num_frame = video.get(cv2.CAP_PROP_FRAME_COUNT)

    # 是否加背景
    if background_root:
        background = cv2.imread(background_root)
        background = cv2.resize(background, (w, h))
    else:
        background = None

    for c in range(int(num_frame)):
        if (frame is None) or (frame_mask is None):
            continue

        # 计算前景
        foreground = frame * (frame_mask / 255)
        foreground = adjust_img(foreground, position_params, size_rate)

        # 是否加背景
        if background_root:
            frame_mask = adjust_img(frame_mask, position_params, size_rate)
            fg_com = foreground + background * (1 - frame_mask / 255)
        else:
            fg_com = foreground

        video_writer.write(fg_com.astype(np.uint8))
        rval, frame = video.read()
        rval_mask, frame_mask = mask.read()

    video_writer.release()

 运行测试

video_compositing(
    'data/video.mp4',
    'data/mask.mp4',
    'data/results.mp4',
    'data/img.jpg',
    position_params=[0., 0.3],  # 垂直,水平
    size_rate=1.8
)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放飞自我的Coder

你的鼓励很棒棒哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值