前言
在目前智能汽车的数据采集(激光雷达数据和视觉数据)的过程中,我们通常在ROS系统中完成数据的记录。因此,我们直接得到的数据是以.bag文件格式保存的。但是,大多数现有感知网络框架的数据格式与 KITTI 数据集的数据格式一致。在 KITTI 数据集中,图像文件保存为 .png 格式,PointCloud 文件保存为 .bin 文件。所以我们需要完成从.bag 文件到.png 和.bin 文件的转换。
一、点云和图片时间同步
1. 使用 message_filters::sync::ApproximateTime 时间戳相近同步
参考代码:
time_sync_node.cpp
#include "ros/ros.h"
#include <rosbag/bag.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/Image.h>
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <message_filters/sync_policies/exact_time.h>
#include <iostream>
using namespace std;
using namespace sensor_msgs;
using namespace message_filters;
ros::Publisher PointCloudInfo_pub;
ros::Publisher ImageInfo_pub;
PointCloud2 syn_pointcloud;
Image syn_iamge;
rosbag::Bag bag;
void Syncallback(const PointCloud2ConstPtr& ori_pointcloud,const ImageConstPtr& ori_image)
{
cout << "\033[1;32m Syn! \033[0m" << endl;
syn_pointcloud = *ori_pointcloud;
syn_iamge = *ori_image;
cout << "syn pointcloud' timestamp : " << syn_pointcloud.header.stamp << endl;
cout << "syn image's timestamp : " << syn_iamge.header.stamp << endl;
bag.write("/lslidar_point_cloud", ros::Time::now(), ori_pointcloud);
bag.write("/usb_cam/image_raw", ros::Time::now(), ori_image);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "hw1");
ros::NodeHandle node;
cout << "\033[1;31m start sync! \033[0m" << endl;
// 建立需要订阅的消息对应的订阅器
message_filters::Subscriber<PointCloud2> PointCloudInfo_sub(node, "/lslidar_point_cloud", 1);
message_filters::Subscriber<Image> ImageInfo_sub(node, "/usb_cam/image_raw", 1);
typedef sync_policies::ApproximateTime<PointCloud2, Image> MySyncPolicy;
Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), PointCloudInfo_sub, ImageInfo_sub); //queue size=10
sync.registerCallback(boost::bind(&Syncallback, _1, _2));
/* 同步文件 */
bag.open("./bag/sync_out.bag", rosbag::bagmode::Write);
ros::spin();
return 0;
}
使用方法:
1. 终端一:roscore
2. 终端二:rosrun time_sync time_sync_node
3. 终端三:rosbag play -r 0.2 out.bag
若无法进入回调函数同步,则说明需要同步的两个topic的时间戳差距比较大。此时需要时间戳对齐到同一起始点。
2. 时间对齐到同一起始点
打印了bag包中点云和图片消息的时间戳,发现点云数据的时间戳不对(录制点云数据时,没有对时)。
方案一:
(1)查看bag内容:rosbag info out.bag;