1.1 载入
ROS提供了解析
1 2 3 4 | import rosbag bag_file = 'test.bag' bag = rosbag.Bag(bag_file, "r") |
1.2 读取信息
1 2 | info = bag.get_type_and_topic_info() print(info) |
可以得到类似如下的信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | TypesAndTopicsTuple( msg_types={ 'sensor_msgs/PointCloud2': '1158d486dd51d683ce2f1be655c3c181', 'sensor_msgs/CompressedImage': '8f7a12909da2c9d3332d540a0977563f' }, topics={ '/pandora/sensor/pandora/hesai40/PointCloud2': TopicTuple( msg_type='sensor_msgs/PointCloud2', message_count=1183, connections=1, frequency=10.071482170278314), '/pandora/sensor/pandora/camera/front_color/compressed': TopicTuple( msg_type='sensor_msgs/CompressedImage', message_count=1183, connections=1, frequency=10.062505847777844) } ) |
1.3 读取topic
可以看到,我的
可以用
该方法可以筛选出想要的
1 2 3 | bag_data = bag.read_messages('/pandora/sensor/pandora/camera/front_color/compressed'): for topic, msg, t in bag_data: pass |
如果不给定
1.4 转换图像数据
但是这里的图片类型是
因此需要使用到
1 2 3 4 5 6 7 8 | import cv2 from cv_bridge import CvBridge bridge = CvBridge() for topic, msg, t in bag_data: cv_image = bridge.compressed_imgmsg_to_cv2(msg, "bgr8") cv2.imshow("Image window", cv_image) cv2.waitKey(3) |
代码中的
1 | print(msg.format) |
所有的编码格式为:
1 2 3 4 5 6 7 8 9 10 11 | mono8: CV_8UC1, grayscale image mono16: CV_16UC1, 16-bit grayscale image bgr8: CV_8UC3, color image with blue-green-red color order rgb8: CV_8UC3, color image with red-green-blue color order bgra8: CV_8UC4, BGR color image with an alpha channel rgba8: CV_8UC4, RGB color image with an alpha channel |
1.5 完整代码
以上的代码连起来就是:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import rosbag import cv2 from cv_bridge import CvBridge bag_file = 'test.bag' bag = rosbag.Bag(bag_file, "r") bridge = CvBridge() bag_data = bag.read_messages('/pandora/sensor/pandora/camera/front_color/compressed') for topic, msg, t in bag_data: cv_image = bridge.compressed_imgmsg_to_cv2(msg, "bgr8") cv2.imshow("Image window", cv_image) cv2.waitKey(3) |
这样就能显示
1.6 图片格式问题
注意,我的
我一开始就是没有注意到有两种图片的格式,因此用
1.7 提取点云
知道了图像的提取方法,提取点云的就简单了,直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 | import rosbag import numpy as np import sensor_msgs.point_cloud2 as pc2 bag_file = 'test.bag' bag = rosbag.Bag(bag_file, "r") bag_data = bag.read_messages('/pandora/sensor/pandora/hesai40/PointCloud2') for topic, msg, t in bag_data: lidar = pc2.read_points(msg) points = np.array(list(lidar)) # 看你想如何处理这些点云 |
这里用到了