【学习笔记】使用python带时间戳提取rosbag中的图像和雷达数据

使用sick的TIM561雷达采集数据,topic nam是/scan,摄像头录制图像,topic name是/usb_cam/image_raw。脚本如下:

# 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 cv2
import os
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
 
rgb_path = '/home/kanghao/bagfiles/bag_image/'
laser_path= '/home/kanghao/bagfiles/laser_data/'
 
class ImageCreator():
    def __init__(self):
        self.bridge = CvBridge()
        with rosbag.Bag('/home/kanghao/bagfiles/2019-02-19-15-23-09.bag', 'r') as bag:  #要读取的bag文件;
            for topic,msg,t in bag.read_messages():
                if topic == "/usb_cam/image_raw&#
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现软同步两组相机数据并生成新的ROS bag文件,需要使用ROS的一些工具和Python的一些库。具体步骤如下: 1. 安装ROSPython库 首先需要安装ROS和相关的Python库,比如rospy、cv_bridge等。 2. 读取原始的ROS bag文件 使用ROSrosbag库读取原始的ROS bag文件,并提取出两组相机数据。 ```python import rosbag bag = rosbag.Bag('original.bag') cam1_msgs = [] cam2_msgs = [] for topic, msg, t in bag.read_messages(topics=['/cam1/image_raw', '/cam2/image_raw']): if topic == '/cam1/image_raw': cam1_msgs.append((msg, t.to_sec())) elif topic == '/cam2/image_raw': cam2_msgs.append((msg, t.to_sec())) ``` 3. 实现软同步 实现软同步需要根据两组相机数据之间的时间戳进行插值。这里可以使用Python的numpy库和scipy库来实现。 ```python import numpy as np from scipy.interpolate import interp1d # 将两组相机数据时间戳取出来 cam1_times = [t for _, t in cam1_msgs] cam2_times = [t for _, t in cam2_msgs] # 将时间戳转化为相对时间 cam1_rel_times = np.array(cam1_times) - cam1_times[0] cam2_rel_times = np.array(cam2_times) - cam2_times[0] # 对两组相机数据时间戳进行插值 f1 = interp1d(cam1_rel_times, cam1_msgs, axis=0, bounds_error=False) f2 = interp1d(cam2_rel_times, cam2_msgs, axis=0, bounds_error=False) # 以较短的时间戳为准进行插值 start_time = max(cam1_rel_times[0], cam2_rel_times[0]) end_time = min(cam1_rel_times[-1], cam2_rel_times[-1]) timestamps = np.linspace(start_time, end_time, int((end_time - start_time) / 0.1)) # 生成新的相机数据 new_cam1_msgs = [f1(t) for t in timestamps] new_cam2_msgs = [f2(t) for t in timestamps] ``` 4. 写入新的ROS bag文件 将生成的新的相机数据写入新的ROS bag文件。 ```python new_bag = rosbag.Bag('synced.bag', 'w') for msg, t in new_cam1_msgs: new_bag.write('/cam1/image_raw', msg, rospy.Time.from_sec(t)) for msg, t in new_cam2_msgs: new_bag.write('/cam2/image_raw', msg, rospy.Time.from_sec(t)) new_bag.close() ``` 完整代码如下: ```python import rosbag import numpy as np from scipy.interpolate import interp1d bag = rosbag.Bag('original.bag') cam1_msgs = [] cam2_msgs = [] for topic, msg, t in bag.read_messages(topics=['/cam1/image_raw', '/cam2/image_raw']): if topic == '/cam1/image_raw': cam1_msgs.append((msg, t.to_sec())) elif topic == '/cam2/image_raw': cam2_msgs.append((msg, t.to_sec())) cam1_times = [t for _, t in cam1_msgs] cam2_times = [t for _, t in cam2_msgs] cam1_rel_times = np.array(cam1_times) - cam1_times[0] cam2_rel_times = np.array(cam2_times) - cam2_times[0] f1 = interp1d(cam1_rel_times, cam1_msgs, axis=0, bounds_error=False) f2 = interp1d(cam2_rel_times, cam2_msgs, axis=0, bounds_error=False) start_time = max(cam1_rel_times[0], cam2_rel_times[0]) end_time = min(cam1_rel_times[-1], cam2_rel_times[-1]) timestamps = np.linspace(start_time, end_time, int((end_time - start_time) / 0.1)) new_cam1_msgs = [f1(t) for t in timestamps] new_cam2_msgs = [f2(t) for t in timestamps] new_bag = rosbag.Bag('synced.bag', 'w') for msg, t in new_cam1_msgs: new_bag.write('/cam1/image_raw', msg, rospy.Time.from_sec(t)) for msg, t in new_cam2_msgs: new_bag.write('/cam2/image_raw', msg, rospy.Time.from_sec(t)) new_bag.close() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值