概述
用 前景/alpha matte 帧序列合成绿幕视频
# -*- coding: utf-8 -*-
# @Time : 2022/6/23 14:51
# @Author : lc
# @File : syn_green_video.py
# @Software: PyCharm
# @Function: Composite green screen video
import os
import torch
import cv2
import torchvision.transforms as transforms
def comGreenVideo(fgr_dir, pha_dir, save_dir, fps):
forcc = cv2.VideoWriter_fourcc(*'mp4v')
bg_green = torch.tensor([120, 255, 155]).div(255).view(3, 1, 1)
# green screen
file_list = os.listdir(fgr_dir)
for file in file_list:
fgr_file_path = os.path.join(fgr_dir, file)
pha_file_path = os.path.join(pha_dir, file)
imgList = os.listdir(fgr_file_path)
save_video_path = save_dir + file + ".mp4"
img_init = cv2.imread(fgr_dir + file + "/00000.jpg", -1)
saveH = img_init.shape[0]
saveW = img_init.shape[1]
video_output = cv2.VideoWriter(save_video_path, forcc, fps, (saveW, saveH), True)
for img in imgList:
fgr_path = fgr_file_path + "/" + img
fgr = cv2.imread(fgr_path, -1)
fgr = transforms.ToTensor()(fgr)
pha_path = pha_file_path + "/" + img
pha = cv2.imread(pha_path, -1)
pha = transforms.ToTensor()(pha)
com_green = fgr * pha + bg_green * (1 - pha)
com_green = com_green.mul(255).byte().cpu().permute(1, 2, 0).numpy()
video_output.write(com_green)
print(file + ".mp4
Saved!")
if __name__ == "__main__":
fps = 30
fgr_dir = r"./fgr_jpg/"
pha_dir = r"./mask/"
save_dir = r"./video/"
comGreenVideo(fgr_dir, pha_dir, save_dir, fps)
最后
以上就是明理可乐为你收集整理的绿幕视频合成的全部内容,希望文章能够帮你解决绿幕视频合成所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复