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

how to compute the centroid of the PointCloud2

asked 2018-11-14 02:58:44 -0500

zhonghao gravatar image

I am considering how can I compute the centroid of sensor_msgs/PointCloud2, is there a direct way to do this? Because I only find he pcl::CentroidPoint< PointT > Class Template Reference *[link text] ( ) on the website.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-11-14 06:54:37 -0500

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.

edit flag offensive delete link more


Hi! I am trying to convert an image in a point cloud using PCL and I am using the code's lines that you have written; but my input is different, so I don't know how to change the line:

pcl::PCLPointCloud2 point_cloud2;

How can I using an image as input instead of sensor_msgs/PointCloud2? Consider that I have a frame with all necessary informations (intrinsic and extrinsic parameters of camera, depth, and so on). Thanks!

alessiadele gravatar image alessiadele  ( 2019-05-05 11:46:40 -0500 )edit

Please can you ask a new question instead of tagging on to the end of this one.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-05-06 08:08:59 -0500 )edit

Question Tools



Asked: 2018-11-14 02:58:44 -0500

Seen: 1,712 times

Last updated: Nov 14 '18