python对双目相机拍摄视频每一帧进行分开保存

前言

  我们针对视频每一帧进行立体匹配时候,想要获取视频每一帧的图像,所以编写了如下代码。
  我们使用的是双目相机,所以根据双目相机拍摄的视频进行分割,然后根据每一帧图像进行左右图像保存。

代码如下(示例):

import cv2
import os

# 读取视频文件
video = cv2.VideoCapture("output.mp4")

# 获取视频的帧数
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))

# 创建左图像和右图像的文件夹
if not os.path.exists("left_video"):
    os.makedirs("left_video")
if not os.path.exists("right_video"):
    os.makedirs("right_video")

# 遍历每一帧
for i in range(frame_count):
    # 读取帧
    ret, frame = video.read()
    if not ret:
        break

    # 将帧分为左图像和右图像
    left_frame = frame[:, :frame.shape[1] // 2, :]
    right_frame = frame[:, frame.shape[1] // 2:, :]

    # 保存左图像和右图像
    cv2.imwrite(f"left_video/frame_{i}.jpg", left_frame)
    cv2.imwrite(f"right_video/frame_{i}.jpg", right_frame)

# 释放视频文件
video.release()

猜你喜欢

转载自blog.csdn.net/weixin_43788282/article/details/128998206