您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:使用Python和OpenCV实现动态背景的画中画效果

51自学网 2025-02-05 12:14:46
  python
这篇教程使用Python和OpenCV实现动态背景的画中画效果写得很实用,希望能帮到您。

在本文中,我们将通过一个详细的Python脚本,使用OpenCV库来为视频添加动态背景。这个脚本将指导你如何读取两个视频文件,一个作为前景,另一个作为背景,并将它们合成一个视频,其中前景视频的特定区域将显示背景视频的内容。

在视频上加入背景动态的画中画。

代码解析与注释

# 视频文件路径input_video_path = 'sc/input_video.mp4'  # 输入视频文件路径output_video_path = 'output_video_bg03.mp4'  # 输出视频文件路径background_video_path = 'sc/bg_03b.mp4'  # MP4视频背景文件路径# 背景透明度(0.0 完全透明,1.0 完全不透明)background_opacity = 0.6# 背景视频的最大帧数(如果需要截断背景视频)max_background_frames = 1000# 读取视频文件cap = cv2.VideoCapture(input_video_path)  # 读取前景视频background_cap = cv2.VideoCapture(background_video_path)  # 读取背景视频# 获取视频帧的尺寸ret, frame = cap.read()  # 读取第一帧if not ret:    print("无法读取视频文件")    raise Exception("无法读取视频文件")  # 如果无法读取,抛出异常height, width = frame.shape[:2]  # 获取帧的高和宽# 定义黑色实心矩形的尺寸变量top_bar_height = int(height * 0.30)  # 顶部黑色实心矩形的高度设置为视频总高度的30%bottom_bar_height = int(height * 0.15)  # 底部黑色实心矩形的高度设置为视频总高度的15%# 定义蒙版的位置和大小mask_top_margin = top_bar_height  # 蒙版顶部距离mask_bottom_margin = bottom_bar_height  # 蒙版底部距离mask_height = height - mask_top_margin - mask_bottom_margin  # 蒙版高度mask_width = width  # 蒙版宽度mask_x = 0  # 蒙版中心x坐标mask_y = mask_top_margin  # 蒙版中心y坐标# 创建蒙版mask = np.zeros((height, width, 3), dtype=np.uint8)  # 创建一个全黑的蒙版cv2.rectangle(mask, (mask_x, mask_y), (mask_x + mask_width, mask_y + mask_height), (255, 255, 255), -1)  # 在蒙版中绘制一个白色矩形# 定义视频写入器fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 定义视频编码格式out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (width, height))  # 创建视频写入对象# 背景视频帧计数器background_frame_index = 0# 遍历视频帧while cap.isOpened():    ret, frame = cap.read()  # 读取前景视频的当前帧    if not ret:        break  # 如果读取结束,跳出循环    # 在视频帧顶部绘制黑色实心矩形    cv2.rectangle(frame, (0, 0), (width, top_bar_height), (0, 0, 0), -1)    # 在视频帧底部绘制黑色实心矩形    cv2.rectangle(frame, (0, height - bottom_bar_height), (width, height), (0, 0, 0), -1)    # 将蒙版应用到帧上    masked_frame = cv2.bitwise_and(frame, mask)    # 读取背景视频的当前帧    background_ret, background_frame = background_cap.read()    if not background_ret or background_frame_index >= max_background_frames:        # 如果背景视频读取失败或达到最大帧数,重新开始背景视频        background_cap.set(cv2.CAP_PROP_POS_FRAMES, 0)        background_frame_index = 0        background_ret, background_frame = background_cap.read()    # 确保背景帧的尺寸与视频帧匹配    background_frame = cv2.resize(background_frame, (width, height))    # 调整背景帧的透明度    background_frame = background_frame.astype(float) * background_opacity    background_frame = np.clip(background_frame, 0, 255).astype(np.uint8)    # 创建反向蒙版    inv_mask = cv2.bitwise_not(mask)    # 将背景帧与蒙版的反向进行混合    masked_background = cv2.bitwise_and(background_frame, inv_mask)    # 将两个混合结果相加    result_frame = cv2.add(masked_frame, masked_background)    # 保存帧    out.write(result_frame)    # 更新背景视频帧索引    background_frame_index += 1# 释放资源cap.release()background_cap.release()out.release()cv2.destroyAllWindows()

注意

以上是通过cv2经测式,如何视频文件大于10M时运行较慢。

结尾与思考

通过上述代码,我们成功地为一个视频添加了动态背景,同时在视频的顶部和底部添加了黑色实心矩形。这个技术可以广泛应用于视频编辑和特效制作中,例如制作新闻节目的背景、电影特效或者增强视频的视觉效果。

这个项目也展示了Python和OpenCV在视频处理方面的强大能力。通过简单的代码,我们就能够实现复杂的视频合成效果。这不仅为视频制作人员提供了便利,也为爱好者提供了一个学习和实验的平台。

在未来,我们可以探索更多的视频处理技术,比如使用深度学习来自动替换视频中的背景,或者开发更复杂的特效来增强视频内容。随着技术的不断进步,视频处理的边界也在不断扩展,为我们提供了无限的可能性。

到此这篇关于使用Python和OpenCV实现动态背景的画中画效果的文章就介绍到这了,更多相关Python OpenCV动态背景内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!


基于Python的A*算法解决八数码问题实现步骤
PyInstaller将Python脚本转为独立可执行文件
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。