livox_lidar_camera_calibration学习--bag转pcd

运行命令:

roslaunch camera_lidar_calibration pcdTransfer.launch

1. 加载保存点云

利用rosbag的遍历命令将bag里面的每个点的xyzi读出来,保存到pointData中

// rosbag 遍历bag的数据,将其保存
void loadAndSavePointcloud(int index) {
    string path = input_bag_path + int2str(index) + ".bag";
    fstream file_;
    file_.open(path, ios::in);
    if (!file_) {
        cout << "File " << path << " does not exit" << endl;
        return;
    }
    ROS_INFO("Start to load the rosbag %s", path.c_str());
    rosbag::Bag bag;
    try {
        bag.open(path, rosbag::bagmode::Read);
    } catch (rosbag::BagException e) {
        ROS_ERROR_STREAM("LOADING BAG FAILED: " << e.what());
        return;
    }

    vector<string> types;
    types.push_back(string("livox_ros_driver/CustomMsg")); 
    rosbag::View view(bag, rosbag::TypeQuery(types));

    int cloudCount = 0;
    for (const rosbag::MessageInstance& m : view) {
        livox_cloud = *(m.instantiate<livox_ros_driver::CustomMsg>()); // message type

        for(uint i = 0; i < livox_cloud.point_num; ++i) {
            pointData myPoint;
            myPoint.x = livox_cloud.points[i].x;
            myPoint.y = livox_cloud.points[i].y;
            myPoint.z = livox_cloud.points[i].z;
            myPoint.i = livox_cloud.points[i].reflectivity;

            vector_data.push_back(myPoint);
        }
        ++cloudCount;
        if (cloudCount >= threshold_lidar) {
            break;
        }
    }
    dataSave(index);
    vector_data.clear();
}

2. 保存pcd文件

首先保存文件头,然后将每个点保存到pcd里面

// 将文件头写入
void writeTitle(const string filename, unsigned long point_num) {
    ofstream outfile(filename.c_str(), ios_base::app);
    if (!outfile) {
        cout << "Can not open the file: " << filename << endl;
        exit(0);
    }
    else {
        outfile << "# .PCD v.7 - Point Cloud Data file format" << endl;
        outfile << "VERSION .7" << endl;
        outfile << "FIELDS x y z intensity" << endl;
        outfile << "SIZE 4 4 4 4" << endl;
        outfile << "TYPE F F F F" << endl;
        outfile << "COUNT 1 1 1 1" << endl;
        outfile << "WIDTH " << long2str(point_num) << endl;
        outfile << "HEIGHT 1" << endl;
        outfile << "VIEWPOINT 0 0 0 1 0 0 0" << endl;
        outfile << "POINTS " << long2str(point_num) << endl;
        outfile << "DATA ascii" << endl;
    }
    ROS_INFO("Save file %s", filename.c_str());
}

void writePointCloud(const string filename, const vector<pointData> singlePCD) {
    ofstream outfile(filename.c_str(), ios_base::app);
    if (!outfile) {
        cout << "Can not open the file: " << filename << endl;
        exit(0);
    }
    else {
        for (unsigned long i = 0; i < singlePCD.size(); ++i) {
            outfile << float2str(singlePCD[i].x) << " " << float2str(singlePCD[i].y) << " " << float2str(singlePCD[i].z) << " " << int2str(singlePCD[i].i) << endl;
        }
    }
}

void dataSave(int index) {
    string outputName = output_path + int2str(index) + ".pcd";
    writeTitle(outputName, vector_data.size());
    writePointCloud(outputName, vector_data);
}

在ROS(Robot Operating System)中,如果你想要将数据主题从默认设置更改为`/livox/lidar`,并且数据类型改为Livox激光雷达生成的点云数据(通常表示为`livox_frame`),同时希望以"Points"风格展示,你需要做以下几件事: 1. 首先,确认你的节点或客户端订阅的是名为`/livox/lidar`的点云数据,而不是之前的默认值。这通常涉及到更新`rospy.Subscriber`或者`message_filters.Subscriber`等相关的订阅代码。 ```python import rospy from sensor_msgs.msg import PointCloud2 # 替换为从livox_msgs.msg导入livox_frame subscriber = rospy.Subscriber('/livox/lidar', livox_frame.LivoxFrame, your_callback_function) ``` 2. `your_callback_function`是你处理接收到的数据的地方,它会接收`LivoxFrame`消息类型的数据。 3. 如果你需要改变数据发布或显示的样式为"Points",这取决于具体的后端处理和可视化工具。例如,在Rviz中,你可以在加载点云数据源时指定它的显示类型为"Points"。 ```python import rclpy from rclpy.node import Node from visualization_msgs.msg import MarkerArray class MyNode(Node): def __init__(self): super().__init__('my_node') self.publisher = self.create_publisher(MarkerArray, '/livox/lidar_points', 10) def publish_lidar_points(self, frame_data): # 将 LivoxFrame 换为MarkerArray并发布,这里假设已经处理了换过程 marker_array_msg = convert_to_marker_array(frame_data) self.publisher.publish(marker_array_msg) ``` 请确保`convert_to_marker_array`函数能够将`LivoxFrame`换成`MarkerArray`,其中包含了"Points"类型的标记。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值