pyrealsense2库的学习笔记

pyrealsense2是英特尔实感(Intel RealSense)SDK 2.0 的 Python 封装库,用于与 RealSense 设备进行交互和获取数据,例如彩色图像、深度图像、点云以及设备的姿态信息等

import pyrealsense2 as rs
import cv2
import numpy as np

# 创建 pipeline 对象
pipeline = rs.pipeline()

# 配置 pipeline,启用彩色和深度流,设置分辨率和帧率
config = rs.config()
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)  
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)  

# 启动 pipeline
profile = pipeline.start(config)

# 将深度图对齐到彩色图
align_to = rs.stream.color  
align = rs.align(align_to)

try:
    while True:
        # 等待下一帧数据
        frames = pipeline.wait_for_frames()

        # 获取对齐后的彩色和深度帧
        aligned_frames = align.process(frames)
        color_frame = aligned_frames.get_color_frame()
        depth_frame = aligned_frames.get_depth_frame()

        if not color_frame or not depth_frame:
            continue

        # 将彩色和深度帧转换为 numpy 数组
        color_image = np.asanyarray(color_frame.get_data())
        depth_image = np.asanyarray(depth_frame.get_data())

        # 显示彩色图像
        cv2.imshow('Color Image', color_image)

        # 可根据需要对深度图像进行处理或显示
        depth_gray = cv2.convertScaleAbs(depth_image, alpha=255 / 4000) 
        cv2.imshow('Depth Image', depth_gray)

        # 按下 'q' 键退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):  
            break
finally:
    # 停止 pipeline 并关闭所有窗口
    pipeline.stop()
    cv2.destroyAllWindows()

下面来解析这段程序 

【初始化】

 在使用 RealSense 设备获取数据(如深度图像、彩色图像等)时,需要通过创建管道来管理数据的采集、处理和传输流程。

pipeline = rs.pipeline()

创建一个配置对象 config ,用于设置要获取的数据流的相关参数,例如流的类型、分辨率、帧率等。

config = rs.config()

使用配置对象 config 来启用彩色流和深度流

config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)  
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)  

通过 pipeline 对象的 start 方法,并传入之前创建的配置对象 config ,启动数据流的获取。将返回的配置信息存储在 profile 变量中。

profile = pipeline.start(config)

【获取图像】

指定要将深度流对齐到彩色流,创建了一个对齐对象 align 。

align_to = rs.stream.color  
align = rs.align(align_to)

这部分开始了一个 try-except 结构中的 try 块,并进入一个无限循环,用于持续获取和处理帧数据

try:
    while True:

 等待并获取 RealSense 设备下一帧数据

 frames = pipeline.wait_for_frames()

 pipeline.wait_for_frames() 是用于等待并获取 RealSense 设备下一帧数据的方法。

当调用这个方法时,它会阻塞程序的执行,直到新的一组帧数据准备好。这里的帧数据通常包含了彩色相机和深度相机等相机采集到的信息。

一旦有新的帧数据可用,该方法会返回一个包含这些帧数据的 frames对象。

通过这个对象,你可以使用 get_***_frame 函数来获取特定类型的帧,例如彩色帧get_color_frame()或深度帧get_depth_frame()

对得到的frames对象中获取的帧进行处理,存储在aligned_frames中

aligned_frames = align.process(frames)

 获得这些帧图像

color_frame = aligned_frames.get_color_frame()
depth_frame = aligned_frames.get_depth_frame()

 如果没有获取到彩色帧或者深度帧,就跳过本次循环,继续等待下一帧

if not color_frame or not depth_frame:
            continue

将彩色帧和深度帧的数据转换为 numpy 数组,以便进行后续的处理和显示。

        color_image = np.asanyarray(color_frame.get_data())
        depth_image = np.asanyarray(depth_frame.get_data())

【显示图像】

 使用 cv2 库的 imshow 函数显示彩色图像

 cv2.imshow('Color Image', color_image)#前面是标题/后面是要显示的内容

 对深度图像进行处理,将其转换为灰度图像,并显示出来。

depth_gray = cv2.convertScaleAbs(depth_image, alpha=255 / 4000) 
cv2.imshow('Depth Image', depth_gray)

【关闭显示】 

等待 1 毫秒,如果用户按下了 q 键(通过判断 cv2.waitKey 的返回值),就退出循环。

  if cv2.waitKey(1) & 0xFF == ord('q'):  
            break

 无论是否出现异常,都会执行 finally 块中的代码。这里停止了数据流的获取,并关闭了所有使用 cv2 显示的窗口。

finally:
    pipeline.stop()
    cv2.destroyAllWindows()

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值