ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

You'll first need to convert the sensor_msgs/PointCloud2 into a pcl::PointCloud object as below. Note you may have to change to type of the point cloud to match what you have.

#include <pcl/conversions.h>


pcl::PCLPointCloud2 point_cloud2;  // Input PointCloud2 message
pcl::PointCloud<pcl::PointXYZ> pclObj;  // pcl version of the point cloud

// Do the conversion
pcl::fromPCLPointCloud2( point_cloud2, pclObj);

Now you can loop through the pclObj.points vector adding them to a pcl::CentroidPoint object and then extract the centroid at the end. I Don't think there's a direct method to get the centroid from the PointCloud2 message itself.