0

I try to convert from pcl::PointCloud<pcl::PointXYZ> to pcl::PCLPointCloud2 But the conversion returns an empty point cloud. This is my code:

pcl::PCLPointCloud2 cloud_inliers_pcl2;
pcl::toPCLPointCloud2(cloud_inliers, cloud_inliers_pcl2);

I can print out the cloud "cloud_inliers" which is in the

pcl::PointCloud<pcl::PointXYZ>

But the pcl::PCLPointCloud2 returns empty fields

krmogi
  • 2,588
  • 1
  • 10
  • 26
Sant
  • 41
  • 3

1 Answers1

0

This is a bit late but others searching the same topic may find this useful.

To convert between PCLPointCloud2 and PointT types you can use:

//Convert from PCLPointCloud2 to PointT pcl::fromPCLPointCloud2(*pc2_cloud_ptr, *xyzrgb_cloud_ptr);

and

///Convert from PointT to PCLPointCloud2 pcl::toPCLPointCloud2(*xyzrgb_cloud_ptr, *pc2_cloud_ptr);

where my pointers to the point cloud are defined as:

pcl::PCLPointCloud2::Ptr pc2_cloud_ptr (new pcl::PCLPointCloud2);

and

pcl::PointCloudpcl::PointXYZRGB::Ptr xyzrgb_cloud_ptr (new pcl::PointCloudpcl::PointXYZRGB);

remember to include: #include <pcl/conversions.h>

Note however that if you're using the Point Cloud Library (PCL) instead of the Robotics Operating System (ROS) library you don't need to convert but rather just use PointT types (e.g. pcl::PointCloudpcl::PointXYZRGB::Ptr). Some examples in PCL tutorials use PCLPointCloud2 types but I find that PointT types work just as well without needing to convert between the types. I guess that the tutorials are for older versions of the PCL. the downsampling tutorial is a good example: https://pcl.readthedocs.io/projects/tutorials/en/latest/voxel_grid.html I used a PointT type instead of the PCLPointCloud2 type used in the tutorial and it works fine in PCL 1.11.

Note: I've recently started learning to use the Point Cloud Library for my master dissertation. Happy to learn more about converting between data structures in PCL. The documentation seems insufficient and needs trial and error to understand the data structures.

Craig
  • 21
  • 3