'pcl::PointCloud<pcl::PointXYZ> to pcl::PointCloud<pcl::PointXYZ>::Ptr (Covert poincloud to ptr)

I am using PCL 1.3. Is there any function to convert pointcloud to pointcloud::ptr. I am new at PCL libraries. I have a ROS node subscribing to sensor_msgs/PoinCloud2 topic then I am converting it into pcl::Poincloud and now I want to do pcl::StatisticalOutlierRemoval. The outlier removal function takes in the pointers, not the Poincloud itself(That's what I understood). So for that purpose I would like to know the solution of how to convert between them?

pcl::PointCloud<pcl::PointXYZ> point_cloud
pcl::fromPCLPointCloud2(cloud_filtered, point_cloud);


pcl::removeNaNFromPointCloud(point_cloud, point_cloud, nan_idx);

pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (point_cloud);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
sor.filter (*point_cloud);

error: no matching function for call to ‘pcl::StatisticalOutlierRemovalpcl::PointXYZ::setInputCloud(**pcl::PointCloudpcl::PointXYZ**&)’

note: candidate: void pcl::PCLBase::setInputCloud(const PointCloudConstPtr&) [with PointT = pcl::PointXYZ; pcl::PCLBase::PointCloudConstPtr = boost::shared_ptr<const pcl::PointCloudpcl::PointXYZ >]



Solution 1:[1]

pcl::PointCloud<pcl::PointXYZ>::Ptr is a shared_ptr - so, simply speaking, it is a wrapper around a pointer that manages it's lifetime (in our case a wrapper around a pointer to pcl::PointCloud<pcl::PointXYZ>).

You instatiate it by passing a pointer, such as the one that comes from a new: pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud(new pcl::PointCloud< pcl::PointXYZ>);

Since pcl::fromPCLPointCloud2 expects a pcl::PointCloud<pcl::PointXYZ>, you need dereferencing: pcl::fromPCLPointCloud2(cloud_filtered, *point_cloud);

Solution 2:[2]

Worth noting also is that with ros_pcl you can directly have a subscriber that directly subscribes to:

const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& message

when sensor_msgs::PointCloud2 messages are published on the subscribed topic this will work out of the box and you do not have to call:

pcl::fromPCLPointCloud2(cloud_filtered, point_cloud);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Loyman
Solution 2 Joachim D