ROS点云和PCL点云转换

 

ROS点云<=>PCL点云

PCL第一代点云:sensor_msgs::PointCloud2 <=> pcl::PointCloud<T>

头文件:

#include <pcl_ros/point_cloud.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_ros/transforms.h>

原型:

void pcl::fromROSMsg(const sensor_msgs::Pointcloud2 &, pcl::PointCloud<T> &);

void pcl::toROSMsg(const pcl::PointCloud<T> &, sensor_msgs::PointCloud2 &);

使用:

sensor_msgs::PointCloud2::ConstPtr& cloud_msg;
pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_cloud(new pcl::PointCloud<pcl::PointXYZ>);

pcl::fromROSMsg(*cloud_msg, *pcl_cloud);

sensor_msgs::PointCloud2 out_cloud_msg;
pcl::toROSMsg(*pcl_cloud, out_cloud_msg);

PCL第二代点云:sensor_msgs::PointCloud2 <=> pcl::PointCloud2<T>

头文件:

#include <pcl_ros/point_cloud.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversiond/pcl_coversions.h>

原型:

void pcl_conversions::toPCL(const sensor_msgs::PointCloud2 &, pcl::PCLPointCloud2 &);
void pcl_conversions::fromPCL(const pcl::PCLPointCloud2 &, const sensor_msgs::Pointcloud2 &);

image-20220915172933665

使用:

sensor_msgs::PointCloud2::ConstPtr& cloud_msg;
pcl::PCLPointCloud2<pcl::PointXYZ>::Ptr pcl_cloud2(new pcl::PCLPointCloud2<pcl::PointXYZ>);

pcl_conversions::toPCL(*cloud_msg, *pcl_cloud2);

sensor_msgs::PointCloud2 out_cloud_msg;
pcl::PCLPointCloud2 pcl2_cloud_filtered;
pcl_conversions::fromPCL(pcl2_cloud_filtered, out_cloud_msg);

参考资料

  1. 博客