RealSense View 录制的bag包图像提取为TUM数据集格式

记录如何将Realsense View录制的bag包提取数据,并转化为TUM数据集格式
录制的数据集格式为彩色 640x480 RGB8;深度 640x480 16位,提取需要根据该函数使用

提取原文件

  使用rs-convert工具,采用-i和-r参数,具体使用方法参考
RS_RAW文件夹必须已经存在

rs-convert -i RS.bag -r RS_RAW/

  提取后的文件如图所示
在这里插入图片描述
在这里插入图片描述
  经过对格式进行分析,需要提取的格式是后缀为.raw的文件,_Color_为彩色文件,_Depth_为深度图文件

转化为TUM格式

  彩色图的数据存储为[R,G,B,R,G,B…],深度图的数据存储为[H,L,H,L…],借助Numpy读取处理,借助cv2转化。使用时需要根据自己的图片格式对img_shape参数进行调整,根据深度图格式对color_image = np.fromfile(read_name, np.uint16).reshape(img_shape)语句进行整。

# RealSenseRAWToTUM.py
import os
import cv2
import sys
import numpy as np

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("python RealSenseRAWToTUM.py src_path out_path")
        exit()

    src_path = sys.argv[1]
    out_path = sys.argv[2]
    out_rgb_path = os.path.join(out_path, "rgb")
    out_depth_path = os.path.join(out_path, "depth")
    img_shape = (480, 640)
    if os.path.exists(src_path):
        print("查找到源文件")
    else:
        print("源文件夹不存在")
        exit()

    if os.path.exists(out_path):
        if not os.path.exists(out_rgb_path):
            os.mkdir(out_rgb_path)
        if not os.path.exists(out_depth_path):
            os.mkdir(out_depth_path)
    else:
        os.mkdir(out_path)
        os.mkdir(out_rgb_path)
        os.mkdir(out_depth_path)
    print("输出文件夹检查完毕")

    all_raw_files = os.listdir(src_path)
    depth_raw_files = []
    color_raw_files = []

    for _ in all_raw_files:
        if "raw" in _:
            if "Color" in _:
                color_raw_files.append(_)
            elif "Depth" in _:
                depth_raw_files.append(_)

    if len(color_raw_files) != len(color_raw_files):
        print("深度图彩色图数量不等")
        exit(
### 将TUM数据集转换为ROS Bag格式 为了实现从TUM数据集到ROS bag格式的转换,可以采用多种方法和技术栈。一种常见的方式是通过编写Python脚本来读取TUM数据集中存储的时间戳、RGB图像以及深度图,将其封装进ROS消息中保存bag文件内。 #### 方法概述 利用`rospy`库与`cv_bridge`模块处理图像信息写入bag文件是一个可行方案。具体来说: - 使用标准输入输出流或直接解析ASCII编码下的`.txt`标注文件获取时间戳; - 对应加载同名编号的图片资源作为传感器采集帧; - 创建相应的ROS话题发布这些数据点; - 启动记录节点完成序列化过程[^1]。 #### 实现细节 下面给出一段示范性的代码片段用于说明上述流程: ```python import rospy from sensor_msgs.msg import Image, CameraInfo from cv_bridge import CvBridge import os import sys import numpy as np def tum_to_ros_bag(tum_dir, output_file): bridge = CvBridge() # 初始化ros node 和 publisher pub_rgb = rospy.Publisher('/camera/rgb/image_raw', Image, queue_size=10) pub_depth = rospy.Publisher('/camera/depth_registered/image_raw', Image, queue_size=10) rospy.init_node('tum_converter') with open(os.path.join(tum_dir,'rgbd_dataset_freiburg1_xyz','rgb.txt')) as f: lines = f.readlines()[3:] # 跳过前三行元数据 for line in lines: timestamp, filename = line.strip().split()[:2] try: img_path = os.path.join(tum_dir,"rgb",filename+".png") msg = bridge.cv2_to_imgmsg(cv2.imread(img_path), encoding="bgr8") msg.header.stamp.secs=int(float(timestamp)) msg.header.frame_id='world' pub_rgb.publish(msg) except Exception as e: print(e) continue # 类似地处理深度图... ``` 此段程序仅展示了部分逻辑——即针对彩色图像的部分;对于深度图像同样适用相同模式进行适配即可。需要注意的是,在实际应用前还需调整路径参数匹配本地环境配置。 此外,还可以借助第三方工具如`image_view`中的`extract_images`功能反向操作验证结果正确性[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值