用realsensed435录制数据集并跑orb-slam2

1.  <利用ros记录话题,获得bag文件>

首先launch相机节点:

roslaunch realsense2_camera rs_camera.launch

rosbag获得.bag 文件:

(xxx为.bag文件的命名,后面两个是相机的rgb和depth话题)

rosbag record -O xxx /camera/color/image_raw /camera/depth/image_rect_raw

2.  <使用Python代码将bag文件转成图片png输出>

注意是Python2代码。

更改输出路径和.bag文件路径(5个路径和2个话题)

# -*- coding: utf-8 -*-

import roslib
import rosbag
import rospy
import cv2
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError

rgb = '/home/sangfor/realsense-data/RGBD_d435/rgb/'  #rgb path
depth = '/home/sangfor/realsense-data/RGBD_d435/depth/'   #depth path
bridge = CvBridge()

file_handle1 = open('/home/sangfor/realsense-data/RGBD_d435/depth.txt', 'w')
file_handle2 = open('/home/sangfor/realsense-data/RGBD_d435/rgb.txt', 'w')

with rosbag.Bag('/home/sangfor/xxx.bag', 'r') as bag:
    for topic,msg,t in bag.read_messages():
        if topic == "/camera/depth/image_rect_raw":  #depth topic
            cv_image = bridge.imgmsg_to_cv2(msg)
            timestr = "%.6f" %  msg.header.stamp.to_sec()   #depth time stamp
            image_name = timestr+ ".png"
            path = "depth/" + image_name
            file_handle1.write(timestr + " " + path + '\n')
            cv2.imwrite(depth + image_name, cv_image)
        if topic == "/camera/color/image_raw":   #rgb topic
            cv_image = bridge.imgmsg_to_cv2(msg,"bgr8")
            timestr = "%.6f" %  msg.header.stamp.to_sec()   #rgb time stamp
            image_name = timestr+ ".jpg"
            path = "rgb/" + image_name
            file_handle2.write(timestr + " " + path + '\n')
            cv2.imwrite(rgb + image_name, cv_image)
file_handle1.close()
file_handle2.close()

改好了之后打开终端输入以下指令执行python脚本

python 提取图像.py

3.associate.py

由于深度图及彩色图的时间戳并非严格一一对齐,存在一定的时间差,因此需将深度图及彩色图按照时间戳最接近原则进行两两配对。将associate.py脚本文件存储至room文件夹下,如图所示:

用realsensed435录制数据集并跑orb-slam2_第1张图片

这两个代码都有可能!

./associate.py depth.txt rgb.txt > associations.txt
./associate.py rgb.txt depth.txt > associations.txt

4.运行orb-slam2

 ./Examples/RGB-D/rgbd_tum Vocabulary/ORBvoc.txt Examples/RGB-D/TUM1.yaml /home/sangfor/realsense-data/RGBD_d435 /home/sangfor/realsense-data/RGBD_d435/associations.txt

你可能感兴趣的:(python,opencv,开发语言)