读取激光雷达点云bin文件并使用rosbag保存

读取激光雷达点云bin文件并保存为rosbag

Apollo Scape数据是以bin文件来储存点云的,从kitti上找到了将kitti数据转到rosbag的工具包,但不想拘泥与kitti的数据形式,所以就自己写了一个小工具。
先附上Apollo Scape的网址:
http://apolloscape.auto/tracking.htm
以及kitti的工具包:
https://github.com/tomas789/kitti2bag
下面开始正文 :

数据准备

apollo的数据是以bin文件的形式存储在不同的frame文件夹下面的,我们将每个bin文件的地址找到保存在一个文档里,方便之后在ros生成消息时找到。

    cur_path = os.path.dirname(os.path.realpath(__file__))
    yaml_path = os.path.join(cur_path, 'config.yaml')

    with open(yaml_path, 'r', encoding='utf-8') as f:
        config = f.read()
        config = yaml.load(config)

    result_path = config['result_path']
    list_path = result_path + 'list.txt'
    if os.path.exists(list_path):
        os.remove(list_path)
    if not os.path.exists(result_path):
        raise Exception('ERROR: NO EXIST RESULT PATH!')

    dataset_path = ''
    dataset_list = []
    num_datalist = 0
    if config['train'] == False and config['data_class_name'] == 'apollo':
        dataset_path = 'detection_test/'
        dataset_path = config['root_path'] + dataset_path
        if not os.path.exists(dataset_path):
            raise Exception('ERROR: NO EXIST DATASET PATH!')
        with open(list_path, 'w+') as f:
            for pcd_id in os.listdir(dataset_path):
                pcd_path = os.path.join(dataset_path, pcd_id)
                for frame_id in os.listdir(pcd_path):
                    frame_path = os.path.join(pcd_path, frame_id)
                    for bin_id in os.listdir(frame_path):
                        bin_path = os.path.join(frame_path, bin_id)
                        f.writelines(bin_path + '\n')
                        dataset_list.append(bin_path)
                        num_datalist += 1

创建bag包,别忘记close:

bag = rosbag.Bag(f=result_path + 'dataset.bag', mode='w')

python下的ros使用

之前用的比较多的开发环境是c++,这次开始学习python下的ros,有一些不同的。发布消息和初始化节点:

def publisher(dataset_list, num):
    pub = rospy.Publisher('bin_to_bag', PointCloud2, queue_size=10)
    rospy.init_node('publisher', anonymous=True)

    header = std_msgs.msg.Header()
    header.frame_id = 'tobag'
    header.stamp = rospy.get_rostime()
    fields = [PointField('x', 0, PointField.FLOAT32, 1),
              PointField('y', 4, PointField.FLOAT32, 1),
              PointField('z', 8, PointField.FLOAT32, 1),
              PointField('intensity', 12, PointField.FLOAT32, 1)]
    with progressbar.ProgressBar(max_value=num) as bar:
        for i in range(num):
            cloud = read_bin_file(dataset_list[i])
            msg_point_cloud = point_cloud2.create_cloud(header=header,
                                                        fields=fields,
                                                        points=cloud)
            pub.publish(msg_point_cloud)
            bag.write('bin_to_bag', msg_point_cloud)
            bar.update(i)

这里的PointCloud2是sensor_msg.msg中的消息类型。要生成这个消息类型可以用到point_cloud2中的create_cloud方法,要有header,PointField,points。header 和c++一样,附了一个frame_id和stamp。PointField是要给出点云特征,因为需要用到强度,所以这里定义了4个特征:x,y,z,intensity。如果不需要强度,可以直接用create_cloud_xyz32。
读bin文件生成一帧点云:

def read_bin_file(path):
    cloud_np = np.fromfile(path, dtype=np.float32, count=-1).reshape([-1, 4])
    size = np.size(cloud_np, 0)
    cloud = []
    for i in range(size):
        point = [cloud_np[i, 0], cloud_np[i, 1],
                 cloud_np[i, 2], cloud_np[i, 3]]
        cloud.append(point)
    return cloud

效果

在这里插入图片描述

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!要使用C++提取ROS bag文件中的点云数据并进行处理,您需要使用ROS中提供的相关库函数。以下是一个简单的示例代码,展示了如何读取ROS bag文件中的点云数据并进行处理: ```cpp #include <rosbag/bag.h> #include <rosbag/view.h> #include <sensor_msgs/PointCloud2.h> int main(int argc, char** argv) { // 初始化ROS节点 ros::init(argc, argv, "pointcloud_extraction"); // 打开ROS bag文件 rosbag::Bag bag; bag.open("/path/to/your/rosbag.bag", rosbag::bagmode::Read); // 设置要提取的话题 std::vector<std::string> topics; topics.push_back("/your/pointcloud/topic"); // 创建一个时间范围,如果您只需要特定时间段的数据,可以进行设置 rosbag::View view(bag, rosbag::TopicQuery(topics)); // 遍历ROS bag文件中的消息 for (rosbag::MessageInstance const& msg : view) { // 检查消息类型是否为点云消息 if (msg.getDataType() == "sensor_msgs/PointCloud2") { // 将消息转换为点云消息 sensor_msgs::PointCloud2::ConstPtr pointcloud_msg = msg.instantiate<sensor_msgs::PointCloud2>(); if (pointcloud_msg != nullptr) { // 在这里对点云数据进行处理,可以使用PCL库或自定义的算法 // 示例:打印点云中的点数量 int num_points = pointcloud_msg->width * pointcloud_msg->height; std::cout << "Number of points in the point cloud: " << num_points << std::endl; } } } // 关闭ROS bag文件 bag.close(); return 0; } ``` 请注意,上述示例中的"/your/pointcloud/topic"需要替换为您要提取的实际点云话题的名称。此外,您还可以使用PCL(Point Cloud Library)等库对点云数据进行更详细的处理。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值