利用ROS工具从bag包中提取图片和.csv文件

1.查看bag文件中topic、内容:

rosbag info xx.bag

2.安装图片处理工具

sudo apt-get install ffmpeg

3. 新建存储图片的文件夹cam0,执行如下命令:

rosrun image_view extract_images _sec_per_frame:=0.01 image:=/mynteye/left/image_mono

这种方式提取的图片的名称是从0依次排序的,若想提取为图片名是时间戳的需要手写一个.py文件,利用python回放。下面是我的.py文件。

# coding:utf-8
#!/usr/bin/python
 
# Extract images from a bag file.
 
#PKG = 'beginner_tutorials'
import roslib   #roslib.load_manifest(PKG)
import rosbag
import rospy
import decimal
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
 
# Reading bag filename from command line or roslaunch parameter.
#import os
#import sys
 
camera0_path = '/home/ubuntu/code/dataset/loop/cam0/data/'
camera1_path = '/home/ubuntu/code/dataset/loop/cam1/data/'
 
class ImageCreator():
 
 
    def __init__(self):
        self.bridge = CvBridge()
        with rosbag.Bag('/home/ubuntu/code/dataset/looppp/loop.bag', 'r') as bag:  #要读取的bag文件;
            for topic,msg,t in bag.read_messages():
                if topic == "/mynteye/left/image_mono": #图像的topic;
                        try:
                            cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
                        except CvBridgeError as e:
                            print e
                        timestr = msg.header.stamp.to_sec()
                        #%.6f表示小数点后带有6位,可根据精确度需要修改;
                        timer = 100000000 * timestr
                        image_name = "%d" % timer + ".png" #图像命名:时间戳.png
                        cv2.imwrite(camera0_path + image_name, cv_image)  #保存;
                elif topic == "/mynteye/right/image_mono": #图像的topic;
                        try:
                            cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
                        except CvBridgeError as e:
                            print e
                        timestr = msg.header.stamp.to_sec()
                        #%.6f表示小数点后带有6位,可根据精确度需要修改;
                        timer = 100000000 * timestr
                        image_name = "%d" % timer + ".png" #图像命名:时间戳.png
                        cv2.imwrite(camera1_path + image_name, cv_image)  #保存;
 
if __name__ == '__main__':
 
    #rospy.init_node(PKG)
 
    try:
        image_creator = ImageCreator()
    except rospy.ROSInterruptException:
        pass

4. 打开.bag文件所在位置,执行如下命令:

rosbag play xx.bag

5.将非图片数据提取为.csv格式存储:

rostopic echo -b xx.bag -p /mynteye/left/image_mono > cam0data.csv
rostopic echo -b xx.bag -p /mynteye/right/image_mono > cam1data.csv
rostopic echo -b xx.bag -p /mynteye/imu/imu_raw > imudata.csv
posted @ 2020-03-24 15:59  不拿曾经看以后  阅读(2351)  评论(0编辑  收藏  举报