一、数据采集
1、ros包数据采集
rosbag record -a -O output_filename --duration=6
//设置bag包名字为 my_rosbag.bag
rosbag record -a -O my_rosbag.bag --duration=6
2、参数解释
- -a:订阅所有话题。
- -O output_filename:指定输出文件名称。
- –duration=6:指定录制持续时间为6秒。
(6秒是因为如果时间太常保存的图片较多占用较大空间)
3、结束录制
Ctrl+c
二、摄像头数据解析
1、将ros包中的image保存为jpeg格式的图片
//安装必要的包
//将 <distro> 替换为你的ROS发行版,例如 noetic、melodic等。
sudo apt-get install ros-<distro>-image-view
2、运行image_view包中的extract_images节点
rosrun image_view extract_images _sec_per_frame:=1 _filename_format:="/home/user/images/frame%04d.jpg" image:=/camera/image_raw
- rosrun image_view extract_images:运行image_view包中的extract_images节点。
- _sec_per_frame:=1:设置每秒保存一帧图像。你可以根据需要调整这个参数。
- _filename_format:=“/home/user/images/frame%04d.jpg”:设置保存图像的文件名格式,这里将保存为 frame0001.jpg,frame0002.jpg,依此类推。指定的文件夹路径需要存在。
- image:=/camera/image_raw:指定图像话题名为/camera/image_raw。
3、播放rosbag文件
在另一个终端中,播放你的rosbag文件:
rosbag play my_rosbag.bag
三、激光数据解析
1、将ros包中的scan保存为pcd格式的点云数据
//安装必要的包
//将 <distro> 替换为你的ROS发行版,例如 noetic、melodic等。
sudo apt-get install ros-<distro>-pcl-ros
2、创建python脚本
#!/usr/bin/env pythonimport rospy
import rosbag
import sensor_msgs.point_cloud2 as pc2
import pcl
from sensor_msgs.msg import PointCloud2def save_pointcloud_as_pcd(bag_file, topic_name, output_dir):#打开rosbag文件进行读取。bag = rosbag.Bag(bag_file, 'r')#遍历指定话题中的消息。for topic, msg, t in bag.read_messages(topics=[topic_name]):if isinstance(msg, PointCloud2):#将ROS PointCloud2消息转换为PCL PointCloud。pc = pc2.read_points(msg, field_names=("x", "y", "z"), skip_nans=True)#创建PCL点云对象并填充点云数据。cloud = pcl.PointCloud()cloud.from_list(list(pc))#使用消息的时间戳作为文件名的一部分。timestamp = t.to_sec()pcd_filename = f"{output_dir}/cloud_{int(timestamp)}.pcd"#将PCL点云保存为PCD文件。pcl.save(cloud, pcd_filename)rospy.loginfo(f"Saved {pcd_filename}")bag.close()if __name__ == '__main__':import argparseparser = argparse.ArgumentParser(description='Extract and save point cloud data from a rosbag.')parser.add_argument('bag_file', help='Input ROS bag file')parser.add_argument('topic_name', help='PointCloud2 topic name')parser.add_argument('output_dir', help='Output directory to save PCD files')args = parser.parse_args()rospy.init_node('extract_pointcloud')save_pointcloud_as_pcd(args.bag_file, args.topic_name, args.output_dir)
3、运行python脚本
//将/point_cloud_topic替换为你的实际点云数据话题名,将/home/user/pcd_files替换为你想要保存PCD文件的目录。
python extract_pointcloud.py my_rosbag.bag /point_cloud_topic /home/user/pcd_files