[Image Processing] Video frame extraction of 16-bit depth map and RGB color map, the video is decomposed into continuous image frames-Python

Articles can be reprinted, but the source must be indicated!

I previously learned to use the Intel  Realsense  D435 depth camera to capture RGB color images and 16-bit depth images.

Realizes video display, recording and saving using depth map and RGB color map as image frames.

Based on practical needs, the video needs to be decomposed into continuous image frames. The procedure is as follows:

First, the ordinary video extracts frames from the RGB video and decomposes the video into continuous image frames:

import cv2              #引入图像处理与系统操作的库
import os


def process_rgb_video(i_video, o_video):      # 定义抽帧函数
    cap = cv2.VideoCapture(i_video)                # 获取视频
    expand_name = '_rgb.png'                       # 分解后图片名称后缀
    if not cap.isOpened():                         # 视频路径检测
        print("Please check the path.")
    cnt = 0
    count = 0
    num = 1
    while 1:
        ret, frame = cap.read()                    # 获取图像帧
        cnt += 1                                   # 保存每一帧,修改参数连续5帧后抽取一帧并保存该帧图片,具体由使用者设置
        if cnt % num == 0:
            count += 1
            cv2.imwrite(os.path.join(o_video, str(count) + expand_name), frame)
        if not ret:
            cap.close()                            # 遍历完视频帧后退出
            break


if __name__ == '__main__':
    # 读取视频路径,需要根据本地情况进行修改
    input_rgb   = r'D:\Pycharm\code\realsensecamera\targetvideo_rgb.mp4'
    # 输出图像帧路径,可根据需求进行修改
    output_rgb   = r'D:\Pycharm\code\realsensecamera\rgb'
    # 创建输出路径文件夹
    if os.path.exists(output_rgb):
        print('rgb文件夹存在')
    else:
        print('rgb文件夹缺失,自动创建 ')
        os.mkdir(output_rgb)
    # 调用图像抽帧函数,分解RGB视频
    process_rgb_video(input_rgb, output_rgb)

The follow-up is the decomposition of the 16-bit depth map video, in which h5py is called to read and decompose the 16-bit depth map video:

import os
import h5py         # 用于16位深度图视频的读取与分解
import cv2
import numpy as np

def h52img(input_depth, output_depth):
    # input_depth : h5文件路径
    # output_depth: 图片文件路径
    h5 = h5py.File(input_depth, 'r')            # 读取深度图视频
    os.path.join(output_depth)
    cnt = 0
    for key in h5.keys():
        cnt = cnt + 1                              # 每一帧都进行深度图保存
        img = cv2.imdecode(np.array(h5[key]), -1)  # 16位深度图解码
        img_name = str(cnt) + '_depth'+'.png'                         # 自定义的深度图文件名称
        cv2.imwrite(os.path.join(output_depth, img_name), img)        # 以png格式保存深度图
    h5.close()



if __name__ == '__main__':
    # 目标深度图视频读取路径,需要根据本地情况进行修改
    input_depth  = r'D:\Pycharm\code\DepthCamera\targetvideo_depth.h5'
    # 深度图图像帧保存路径
    output_depth = r'D:\Pycharm\code\DepthCamera\depth'
    # 创建保存路径对应的文件夹
    if os.path.exists(output_depth):
        print('depth文件夹存在')
    else:
        print('depth文件夹缺失,自动创建 ')
        os.mkdir(output_depth)
    # 调用视频分解函数,将目标视频分解为图像帧
    h52img(input_depth, output_depth)

The above is the overall code for video decomposition, which can decompose the acquired RGB and depth map videos into continuous image frames.

Subsequently, point cloud processing will be performed based on the corresponding RGB and depth map image frames.

Guess you like

Origin blog.csdn.net/qq_41026536/article/details/129150418