1。将时间戳不同的点云和图像进行时间戳同步
1。1 rqt_bag ***.bag 可以查看bag的时间戳
1.2.rosbag info ***.bag
rosbag info 2021-04-19-12-26-14.bag
path: 2021-04-19-12-26-14.bag
version: 2.0
duration: 34.9s
start: Apr 19 2021 12:26:14.40 (1618806374.40)
end: Apr 19 2021 12:26:49.31 (1618806409.31)
size: 1.8 GB
messages: 700
compression: none [700/700 chunks]
types: sensor_msgs/Image [060021388200f6f0f447d0fcd9c64743]
sensor_msgs/PointCloud2 [1158d486dd51d683ce2f1be655c3c181]
topics: /lmx_image 350 msgs : sensor_msgs/Image
/lmx_pc 350 msgs : sensor_msgs/PointCloud2
1.3 代码参考:https://blog.csdn.net/qq_35632833/article/details/105666950
Cmakelists.txt
cmake_minimum_required(VERSION 3.0.2)
project(lidar_img_syn)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
sensor_msgs
message_filters
)
find_package(PCL 1.7 REQUIRED)
catkin_package(
CATKIN_DEPENDS
roscpp
std_msgs
sensor_msgs
message_filters
)
include_directories(include ${catkin_INCLUDE_DIRS})
include_directories(include ${PCL_INCLUDE_DIRS})
add_executable(lidar_img_syn_lmx src/lidar_img_syn_node.cpp)
target_link_libraries (lidar_img_syn_lmx ${catkin_LIBRARIES})
代码部分,注意生成的结果时间戳不是相同的,是基本相近的。
#include "ros/ros.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;
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;
PointCloudInfo_pub.publish(syn_pointcloud);
ImageInfo_pub.publish(syn_iamge);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "hw1");
ros::NodeHandle node;
cout << "\033[1;31m hw1! \033[0m" << endl;
// 建立需要订阅的消息对应的订阅器
message_filters::Subscriber<PointCloud2> PointCloudInfo_sub(node, "/rslidar_points", 1);
message_filters::Subscriber<Image> ImageInfo_sub(node, "/zed/zed_node/left/image_rect_color", 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));
PointCloudInfo_pub = node.advertise<PointCloud2>("/djq_pc", 10);
ImageInfo_pub = node.advertise<Image>("/djq_image", 10);
ros::spin();
return 0;
}
2。把bag包里的图像和点云分割成一帧一帧的,
https://blog.csdn.net/zbr794866300/article/details/107830939
1。提取点云为pcd
rosrun pcl_ros bag_to_pcd <*.bag> <topic> <output_directory>
2。提取图像包括时间戳
需要修改1)设置图片保存路径 2)包的名称 3)图像话题的名称
#coding:utf-8
import roslib;
import rosbag
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
path='/home/zbr/bagfile/image/' #存放图片的位置
class ImageCreator():
def __init__(self):
self.bridge = CvBridge()
with rosbag.Bag('water3.bag', 'r') as bag: #要读取的bag文件;
for topic,msg,t in bag.read_messages():
if topic == "/usb_cam/image_raw": #图像的topic;
try:
cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print e
timestr = "%.6f" % msg.header.stamp.to_sec()
#%.6f表示小数点后带有6位,可根据精确度需要修改;
image_name = timestr+ ".jpg" #图像命名:时间戳.jpg
cv2.imwrite(path+image_name, cv_image) #保存;
if __name__ == '__main__':
#rospy.init_node(PKG)
try:
image_creator = ImageCreator()
except rospy.ROSInterruptException:
pass
3。把pcd转成bin格式。
https://gitee.com/lyy54321/Tools_RosBag2KITTI
看了这么久,这个工具箱是最好用的。
1。/home/studieren/shared_dir/bishe_lmx/ros_package/src/lidar_img_syn
3。/home/studieren/syc_data/get_image.py
4。/home/studieren/syc_data/pcd2bin