Realsense D深度图像采集与保存:python代码和Open3D后处理

Realsense D深度图像采集与保存:python代码和Open3D后处理

深度图像是计算机视觉和三维重建中常用的一种数据类型,而Realsense D系列相机可以提供高质量的深度图像。本文将介绍如何使用Python编写程序来采集Realsense D相机的深度图像,并将其保存为.txt文本文件和.pcd点云文件,以方便后续使用Open3D进行处理。

首先,我们需要安装相应的软件包。确保已经安装了pyrealsense2和open3d两个Python库。

pip install pyrealsense2 open3d

接下来,导入所需的库:

import pyrealsense2 as rs
import numpy as np
import open3d as o3d

我们将使用pyrealsense2库来与Realsense D相机进行通信和图像采集,并使用numpy库来处理深度图像数据。open3d库则用于后续的点云可视化和处理。

首先,创建一个函数来采集深度图像并将其保存为.txt文件:

def save_depth_txt(file_path):
    # 配置深度流
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    
    # 开启流
    pipeline.start(config)
    
    # 得到深度传感器实例
    profile = pipeline.get_active_profile()
    depth_sensor = profile.get_device().first_depth_sensor()
    
    # 设置深度传感器参数
    depth_scale = depth_sensor.get_depth_scale()
    clipping_distance = 1.0
    
    # 创建一个numpy数组来存储深度图像数据
    depth_image = np.zeros((480, 640), dtype=np.float32)
    
    try:
        # 循环采集深度图像
        while True:
            # 等待下一帧
            frames = pipeline.wait_for_frames()
            
            # 取得深度帧
            depth_frame = frames.get_depth_frame()
            
            # 将深度帧转换为numpy数组
            depth_data = np.asarray(depth_frame.get_data(), dtype=np.uint16)
            
            # 将深度数据进行单位转换和裁剪
            depth_image = depth_data * depth_scale
            depth_image[depth_image > clipping_distance] = 0
            
            # 保存深度图像为.txt文件
            np.savetxt(file_path, depth_image, fmt='%.4f', delimiter='\t')
            
            # 按下'q'键退出循环
            key = cv2.waitKey(1)
            if key == ord('q'):
                break
    
    finally:
        # 关闭流和窗口
        pipeline.stop()

在上述代码中,我们通过配置深度流来设置深度图像的分辨率和格式。然后,通过调用pipeline.start()来开启数据流,并获取深度传感器的实例以进行参数设置。

在每个循环迭代中,我们等待下一帧深度图像,并将其转换为numpy数组。通过乘以深度尺度因子depth_scale,我们可以将深度数据转换为实际的深度值。由于深度传感器的测量范围是有限的,所以我们可以根据需要进行裁剪。

最后,我们使用np.savetxt()函数将深度图像数据保存为.txt文件。

接下来,让我们编写另一个函数来采集深度图像并将其保存为.pcd点云文件:

def save_depth_pcd(file_path):
    # 配置深度流
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    
    # 开启流
    pipeline.start(config)
    
    # 得到深度传感器实例
    profile = pipeline.get_active_profile()
    depth_sensor = profile.get_device().first_depth_sensor()
    
    # 设置深度传感器参数
    depth_scale = depth_sensor.get_depth_scale()
    clipping_distance = 1.0
    
    # 创建一个PointCloud对象
    pcd = o3d.geometry.PointCloud()
    
    try:
        # 循环采集深度图像
        while True:
            # 等待下一帧
            frames = pipeline.wait_for_frames()
            
            # 取得深度帧
            depth_frame = frames.get_depth_frame()
            
            # 将深度帧转换为numpy数组
            depth_data = np.asarray(depth_frame.get_data(), dtype=np.uint16)
            
            # 将深度数据进行单位转换和裁剪
            depth_image = depth_data * depth_scale
            depth_image[depth_image > clipping_distance] = 0
            
            # 将深度图像转换为点云
            points = rs.pointcloud.calculate(depth_frame)
            vertices = np.asarray(points.get_vertices(), dtype=np.float32)
            pcd.points = o3d.utility.Vector3dVector(vertices)
            
            # 保存点云为.pcd文件
            o3d.io.write_point_cloud(file_path, pcd)
            
            # 按下'q'键退出循环
            key = cv2.waitKey(1)
            if key == ord('q'):
                break
    
    finally:
        # 关闭流和窗口
        pipeline.stop()

在上述代码中,我们首先配置深度流,并通过pipeline.start()开启数据流。然后,我们获取深度传感器的实例并设置参数。

在每个循环迭代中,我们等待下一帧深度图像,并将其转换为numpy数组。将深度数据乘以深度尺度因子depth_scale后,我们可以得到实际的深度值。同样地,我们可以根据需要对深度数据进行裁剪。

接着,我们使用open3d库创建一个PointCloud对象pcd,并将深度图像转换为点云数据。然后,我们可以使用o3d.io.write_point_cloud()函数将点云保存为.pcd文件。

最后,我们需要调用上述两个函数来采集深度图像并保存为.txt和.pcd文件:

# 保存为.txt文件
save_depth_txt('depth_image.txt')

# 保存为.pcd文件
save_depth_pcd('depth_cloud.pcd')

通过调用这两个函数,并将文件路径作为参数传递进去,我们可以分别保存深度图像为.txt和.pcd文件。

以上就是使用Python编写的程序来采集Realsense D相机的深度图像,并将其保存为.txt文本文件和.pcd点云文件的方法。借助于Open3D库,我们可以进一步对保存的深度图像和点云进行可视化和处理。请注意,确保在运行程序前已经正确连接了Realsense D相机,并安装了相应的软件包。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值