关于sensor_msgs::Pointcloud转换为pcl::PointCloud的路径咨询:是否可直接转换,还是需先转为sensor_msgs::PointCloud2?
Great questions—let’s break this down clearly, since working with ROS and PCL point clouds can have some tricky nuances!
1. Can sensor_msgs::PointCloud be converted to pcl::PointCloud?
Absolutely yes! It’s totally possible to convert the older ROS sensor_msgs::PointCloud message format to a PCL pcl::PointCloud—you just need to follow a standard two-step workflow (more on that in the next section).
2. Direct conversion vs. intermediate PointCloud2 step?
You cannot directly convert sensor_msgs::PointCloud to pcl::PointCloud with a single PCL utility function. The standard (and officially supported) approach requires an intermediate conversion to sensor_msgs::PointCloud2:
- First, convert the legacy
sensor_msgs::PointCloudto the modernsensor_msgs::PointCloud2format using ROS’s built-in conversion tools. - Then, use PCL’s conversion functions to turn the
PointCloud2into apcl::PointCloud.
Here’s a complete code example to illustrate this:
#include <sensor_msgs/PointCloud.h> #include <sensor_msgs/PointCloud2.h> #include <pcl/point_cloud.h> #include <pcl/point_types.h> #include <pcl_conversions/pcl_conversions.h> #include <sensor_msgs/point_cloud_conversion.h> // Assume you have a populated sensor_msgs::PointCloud message sensor_msgs::PointCloud ros_legacy_cloud; // Step 1: Convert legacy PointCloud to PointCloud2 sensor_msgs::PointCloud2 ros_modern_cloud; sensor_msgs::convertPointCloudToPointCloud2(ros_legacy_cloud, ros_modern_cloud); // Step 2: Convert PointCloud2 to PCL PointCloud pcl::PointCloud<pcl::PointXYZ> pcl_cloud; pcl::fromROSMsg(ros_modern_cloud, pcl_cloud); // If your cloud has intensity data, use pcl::PointXYZI instead: // pcl::PointCloud<pcl::PointXYZI> pcl_cloud_with_intensity; // pcl::fromROSMsg(ros_modern_cloud, pcl_cloud_with_intensity);
Why this two-step process?
sensor_msgs::PointCloud is a deprecated ROS message format—PointCloud2 is the current standard because it supports flexible, arbitrary point fields (like intensity, normals, etc.). PCL’s core conversion utilities are built to work directly with PointCloud2 since it aligns better with PCL’s own point cloud data structure.
内容的提问来源于stack exchange,提问作者user18465277

