【Intel Realsense D435】16位深度图和RGB颜色图的视频显示、录制和保存(Python)

这篇具有很好参考价值的文章主要介绍了【Intel Realsense D435】16位深度图和RGB颜色图的视频显示、录制和保存(Python)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

文章可以转载,但是必须表明出处!

本文使用Intel Realsense D435深度相机,拍摄RGB颜色图及16位深度图。

同时实现了以深度图和RGB颜色图为图像帧的视频显示、录制与保存。

以下为总体程序:

'''
使用realsense相机录制视频
'''

#!/usr/bin/env python
# coding=utf-8
import time
import h5py         # 深度图格式所在库
import pyrealsense2 as rs
import numpy as np
import cv2
import os

class Camera(object):
    '''
    realsense相机处理类
    '''
    def __init__(self, width=1280, height=720, fps=30):   # 图片格式可根据程序需要进行更改

        self.width = width
        self.height = height
        self.pipeline = rs.pipeline()
        self.config = rs.config()
        self.config.enable_stream(rs.stream.color, self.width, self.height, rs.format.bgr8, fps)
        self.config.enable_stream(rs.stream.depth, self.width, self.height, rs.format.z16,  fps)
        # self.config.enable_stream(rs.stream.infrared, 1, self.width, self.height, rs.format.y8, fps)
        # self.config.enable_stream(rs.stream.infrared, 2, self.width, self.height, rs.format.y8, fps)
        self.pipeline.start(self.config)      # 获取图像视频流


    def get_frame(self):
        frames = self.pipeline.wait_for_frames()   # 获得frame (包括彩色,深度图)
        colorizer = rs.colorizer()                 # 创建伪彩色图对象
        depth_to_disparity = rs.disparity_transform(True)
        disparity_to_depth = rs.disparity_transform(False)

        # 创建对齐对象
        align_to = rs.stream.color                 # rs.align允许我们执行深度帧与其他帧的对齐
        align = rs.align(align_to)                 # “align_to”是我们计划对齐深度帧的流类型。
        aligned_frames = align.process(frames)
        # 获取对齐的帧
        aligned_depth_frame = aligned_frames.get_depth_frame()      # aligned_depth_frame是对齐的深度图
        color_frame   = aligned_frames.get_color_frame()
        # left_frame  = frames.get_infrared_frame(1)
        # right_frame = frames.get_infrared_frame(2)
        color_image     = np.asanyarray(color_frame.get_data())
        colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
        depthx_image    = np.asanyarray(aligned_depth_frame.get_data())  # 原始深度图
        # left_frame   = np.asanyarray(left_frame.get_data())
        # right_frame  = np.asanyarray(right_frame.get_data())

        return color_image, depthx_image, colorizer_depth
        # left_frame, right_frame

    def release(self):
        self.pipeline.stop()


if __name__ == '__main__':

    # 视频保存路径
    os.mkdir(f'D://Realsense//Video//{int(time.time())}')
    video_path         = f'D://Realsense//Video//{int(time.time())}//targetvideo_rgb.mp4'
    video_depthc_path  = f'D://Realsense//Video//{int(time.time())}//targetvideo_depthcolor.mp4'
    video_depth16_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depth.h5'
    # video_left_path    = f'D://Realsense//Video//Stereo_left//{int(time.time())}_left.mp4'
    # video_right_path   = f'D://Realsense//Video//Stereo_right//{int(time.time())}_right.mp4'


    # 初始化参数
    fps, w, h = 30, 1280, 720
    # fps, w, h = 30, 640, 480
    mp4        = cv2.VideoWriter_fourcc(*'mp4v')   # 视频格式
             # 视频保存而建立对象
    # wr_left    = cv2.VideoWriter(video_left_path, mp4, fps, (w, h),      isColor=False)
    # wr_right   = cv2.VideoWriter(video_right_path, mp4, fps, (w, h),     isColor=False)


    # 完成相机初始化

    cam = Camera(w, h, fps)
    flag_V = 0
    idx = 0
    id  = 0
    print('录制视频请按: s, 保存视频或退出请按:q')


    while True:
            # 读取图像帧,包括RGB图和深度图
            color_image, depthxy_image, colorizer_depth = cam.get_frame()
            cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
            cv2.imshow('RealSense', color_image)
            key = cv2.waitKey(1)

            if key & 0xFF == ord('s') :
                flag_V = 1
                # 创建视频文件
                wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True)
                wr_colordepth = cv2.VideoWriter(video_depthc_path, mp4, fps, (w, h), isColor=True)
                wr_depth = h5py.File(video_depth16_path, 'w')
                print('...录制视频中...')
            if flag_V == 1:
                # 保存图像帧
                wr.write(color_image)                          # 保存RGB图像帧
                wr_colordepth.write(colorizer_depth)           # 保存相机自身着色深度图
                # wr_left.write(left_image)                      # 保存左帧深度图
                # wr_right.write(right_image)                    # 保存右帧深度图
                # res, depth16_image = cv2.imencode('.png', depthxy_image)  # 深度图解码方式一:点云小,但是出错
                depth16_image = cv2.imencode('.png', depthxy_image)[1]      # 深度图解码方式二:文件较大,测试稳定
                depth_map_name = str(id).zfill(5) + '_depth.png'
                # wr_depth[str(idx).zfill(5)] = depth16_image          #  储存方法:1 前3帧和没11帧出现高质量点云,其他错误
                wr_depth[depth_map_name] = depth16_image           #  储存方法:2 所有点云准确,但是点云质量不高
                idx += 1
                id = id + 1
            if key & 0xFF == ord('q') or key == 27:
                cv2.destroyAllWindows()
                print('...录制结束/直接退出...')
                break
    # 录制完毕,释放对象
    wr.release()
    wr_colordepth.release()
    # wr_left.release()
    # wr_right.release()
    wr_depth.close()
    cam.release()
    print(f'若保存视频,则视频保存在:{video_path}')

程序阅读如下:

1.库引用

import time         # 获取时间信息,用于创建不同文件名称的文件夹
import h5py         # 深度图格式所在库
import pyrealsense2 as rs # 相机集成库,用于相机初始化与相机视频流获取
import numpy as np  # 将图像帧转换为数据信息以进行图像保存
import cv2          # 视觉图像库,用于保存图像帧与视频
import os           # 系统操作库,用于系统路径寻觅与文件夹创建

2.相机初始化函数

class Camera(object):
    '''
    realsense相机处理类
    '''
    def __init__(self, width=1280, height=720, fps=30):   # 图片格式可根据程序需要进行更改

        self.width = width                #获取图像帧设置
        self.height = height
        self.pipeline = rs.pipeline()     #开放图像帧管道
        self.config = rs.config()         #配置初始化
        self.config.enable_stream(rs.stream.color, self.width, self.height, rs.format.bgr8, fps)
        self.config.enable_stream(rs.stream.depth, self.width, self.height, rs.format.z16,  fps)
        # self.config.enable_stream(rs.stream.infrared, 1, self.width, self.height, rs.format.y8, fps)
        # self.config.enable_stream(rs.stream.infrared, 2, self.width, self.height, rs.format.y8, fps)
        self.pipeline.start(self.config)      # 获取图像视频流

3.图像流获取函数 

    def get_frame(self):
        frames = self.pipeline.wait_for_frames()   # 获得frame (包括彩色,深度图)
        colorizer = rs.colorizer()                 # 创建伪彩色图对象
        depth_to_disparity = rs.disparity_transform(True)
        disparity_to_depth = rs.disparity_transform(False)
        # 创建对齐对象
        align_to = rs.stream.color                 # rs.align允许我们执行深度帧与其他帧的对齐
        align = rs.align(align_to)                 # “align_to”是我们计划对齐深度帧的流类型。
        aligned_frames = align.process(frames)
        # 获取对齐的帧
        aligned_depth_frame = aligned_frames.get_depth_frame()      # aligned_depth_frame是对齐的深度图
        color_frame   = aligned_frames.get_color_frame()
        # left_frame  = frames.get_infrared_frame(1)
        # right_frame = frames.get_infrared_frame(2)
        
        color_image     = np.asanyarray(color_frame.get_data())     # 将图像帧转换为矩阵格式
        colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
        depthx_image    = np.asanyarray(aligned_depth_frame.get_data())  
        # left_frame   = np.asanyarray(left_frame.get_data())
        # right_frame  = np.asanyarray(right_frame.get_data())

        return color_image, depthx_image, colorizer_depth           # 返回图像数据值
        # left_frame, right_frame

4.相机功能释放函数

    def release(self):
        self.pipeline.stop()            # 停止相机拍摄

5.主程序:文件路径需要根据本地情况进行修改,图片格式根据需求进行修改

if __name__ == '__main__':

    # 视频保存路径   路径需要根据本地情况进行修改
    os.mkdir(f'D://Realsense//Video//{int(time.time())}')
    video_path         = f'D://Realsense//Video//{int(time.time())}//targetvideo_rgb.mp4'
    video_depthc_path  = f'D://Realsense//Video//{int(time.time())}//targetvideo_depthcolor.mp4'
    video_depth16_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depth.h5'
    # video_left_path    = f'D://Realsense//Video//Stereo_left//{int(time.time())}_left.mp4'
    # video_right_path   = f'D://Realsense//Video//Stereo_right//{int(time.time())}_right.mp4'


    # 初始化参数
    fps, w, h = 30, 1280, 720
    # fps, w, h = 30, 640, 480
    mp4        = cv2.VideoWriter_fourcc(*'mp4v')   # 视频格式
    # 视频保存对象
    # wr_left    = cv2.VideoWriter(video_left_path, mp4, fps, (w, h),      isColor=False)
    # wr_right   = cv2.VideoWriter(video_right_path, mp4, fps, (w, h),     isColor=False)
    # 相机初始化
    cam = Camera(w, h, fps)
    flag_V = 0
    idx = 0
    id  = 0
    print('录制视频请按: s, 保存视频或退出请按:q')
    # 循环保存图像帧以建立视频
    while True:
            # 读取图像帧,包括RGB图和深度图
            color_image, depthxy_image, colorizer_depth = cam.get_frame()
            cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
            cv2.imshow('RealSense', color_image)
            key = cv2.waitKey(1)

            if key & 0xFF == ord('s') :
                flag_V = 1
                # 创建视频文件
                wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True)
                wr_colordepth = cv2.VideoWriter(video_depthc_path, mp4, fps, (w, h), isColor=True)
                wr_depth = h5py.File(video_depth16_path, 'w')
                print('...录制视频中...')
            if flag_V == 1:
                # 保存图像帧
                wr.write(color_image)                          # 保存RGB图像帧
                wr_colordepth.write(colorizer_depth)           # 保存相机自身着色深度图
                # wr_left.write(left_image)                      # 保存左帧深度图
                # wr_right.write(right_image)                    # 保存右帧深度图
                depth16_image = cv2.imencode('.png', depthxy_image)[1]      # 深度图解码
                depth_map_name = str(id).zfill(5) + '_depth.png'
                wr_depth[depth_map_name] = depth16_image                    # 所有点云准确
                id = id + 1
            if key & 0xFF == ord('q') or key == 27:
                cv2.destroyAllWindows()
                print('...录制结束/直接退出...')
                break

    # 录制完毕,释放视频对象
    wr.release()
    wr_colordepth.release()
    # wr_left.release()
    # wr_right.release()
    wr_depth.close()
    cam.release()
    print(f'若保存视频,则视频保存在:{video_path}')

图片拍摄结果:

RGB图

d435深度图格式,数码相机,python,算法,视频

16位深度图

d435深度图格式,数码相机,python,算法,视频

 伪彩色图

d435深度图格式,数码相机,python,算法,视频

总结:

对比上一篇文章,该程序完整的保存了16位深度图,而非将其转换为8位深度图;

16位深度图保存采用的h5py格式,在后续进行图片抽帧时需要对应的图片抽取程序。

具体代码可查看我后续的博客

 文章来源地址https://www.toymoban.com/news/detail-522022.html

到了这里,关于【Intel Realsense D435】16位深度图和RGB颜色图的视频显示、录制和保存(Python)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Ubuntu 20.04 Intel RealSense D435i 相机标定教程

    报错:sumpixel_test.cpp:2:10: fatal error: backward.hpp: 没有那个文件或目录,将sumpixel_test.cpp中# include \\\"backward.hpp\\\"改为:#include “code_utils/backward.hpp”。 报错 创建rs_imu_calibration.launch 找到realsense-ros包,进入/catkin_ws/src/realsense2_camera/launch(路径仅供参考),复制其中的rs_camera.launch,并重

    2024年01月16日
    浏览(34)
  • 【深度相机D435i】Windows+Ubuntu下调用D435i利用Python读取、保存RGB、Depth图片

    最近组里面的项目需要用到D435i深度相机采集深度图片,所以记录一下在Windows+Ubuntu的环境下使用D435i深度相机的流程,以及如何利用python读取、保存常见的RGB、Depth图片。 D435i 在小巧外形中采用英特尔模块和视觉处理器,是一个功能强大的一体产品,可与可定制软件配合使用

    2024年02月02日
    浏览(32)
  • (已修正精度 1mm左右)Realsense d435i深度相机+Aruco+棋盘格+OpenCV手眼标定全过程记录

    最近帮别人做了个手眼标定,然后我标定完了大概精度能到1mm左右。所以原文中误差10mm可能是当时那个臂本身的坐标系有问题。然后用的代码改成了基于python的,放在下面。 新来的小伙伴可以只参考前面的代码就可以完成标定了。 有问题的话可以留言,一起交流~ 手眼标定

    2024年02月04日
    浏览(32)
  • Realsense d435i驱动安装、配置及校准

    写在前面 本文是在ubuntu20.04下安装,其它版本大同小异。可能出现的问题,主要由各自安装相关库版本不一致导致,故问题不一,但一般很好解决,正常情况下不会出现。 Intel Realsense 深度摄像头D435i将D435强大的深度感知能力和惯性测量单元(IMU)结合起来,可满足RGBD、单目、

    2024年02月02日
    浏览(37)
  • SLAM算法与工程实践——相机篇:RealSense D435使用(2)

    下面是SLAM算法与工程实践系列文章的总链接,本人发表这个系列的文章链接均收录于此 下面是专栏地址: 这个系列的文章是分享SLAM相关技术算法的学习和工程实践 参考: realsense相机两种获取相机内外参的方式 使用Realsense D435i运行VINS-Fusion kalibr标定realsenseD435i --多相机标定

    2024年02月03日
    浏览(30)
  • 机械臂手眼标定realsense d435相机——眼在手上python、matlab

    两周内看了好多博客,博客上的代码甚至github上的代码都试过了一遍,各种语言matlab、c++、python,了解到了许多做手眼标定的平台——halcon、ros(这俩还需要从头开始学,时间不太够用),最后看到了鱼香ros的博客,参考了一下并总结完整,附链接 此博客仅记录学习过程总结

    2024年02月15日
    浏览(36)
  • C++ api调用realsense d435相机,并计算真实世界坐标值

            在使用Intel RealSense相机进行编程时,首先需要创建一个 rs2::pipeline 对象,并使用该对象启动相机的数据流。在启动数据流后,相机将根据配置的参数生成相应的数据流,例如深度、彩色或红外流,并将这些数据传输到计算机中。 RS2_STREAM_DEPTH :指定启用的流类型为

    2024年02月11日
    浏览(32)
  • realsense D435i 实现外部时钟触发硬件同步多相机数据采集

    最近有一个调试D435i相机的工作,需要使得三个相机能够完成硬件触发的同步,具体来说,就是有一个固定频率的外部脉冲信号,使得三个相机能够根据外部脉冲信号的硬件触发完成双目图片、深度图片、彩色图片、IMU数据的实时响应采集,因为外部脉冲信号是通过一个精确

    2024年01月16日
    浏览(34)
  • C++ api调用realsense d435相机,将坐标转换到相机坐标系

            在使用Intel RealSense相机进行编程时,首先需要创建一个 rs2::pipeline 对象,并使用该对象启动相机的数据流。在启动数据流后,相机将根据配置的参数生成相应的数据流,例如深度、彩色或红外流,并将这些数据传输到计算机中。 RS2_STREAM_DEPTH :指定启用的流类型为

    2024年02月16日
    浏览(32)
  • Realsense D435i Yolov5目标检测实时获得目标三维位置信息

    - Colorimage: - Colorimage and depthimage: 1.一个可以运行YOLOv5的python环境 2.一个realsense相机和pyrealsense2库 在下面两个环境中测试成功 win10 python 3.8 Pytorch 1.10.2+gpu CUDA 11.3 NVIDIA GeForce MX150 ubuntu16.04 python 3.6 Pytorch 1.7.1+cpu 修改模型配置文件,以yolov5s为例。 如果使用自己训练的模型,需要进

    2024年02月04日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包