Can't elaborate centroids (Euclidean Cluster Extraction)

asked 2020-08-06 13:27:52 -0600

giulianodejulio gravatar image

I'm trying to extract clusters from a point cloud in order to find the centroids of those clusters. I've been following the tutorial on PCL website https://pcl.readthedocs.io/projects/t... .

I'm running ROS Melodic on Ubuntu 18.04.4 and I'm a ROS and PCL newbie.

The program works fine using PCD files (that is, offline) but I'm facing troubles elaborating sensor_msgs::PointCloud2 input cloud (online). The following is a part of the whole code:

  void cloudCB( const sensor_msgs::PointCloud2& input){
  sensor_msgs::PointCloud2 output;
  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZRGB>);
  // elaboration on cloud_filtered ...

  pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
  tree->setInputCloud (cloud_filtered);

  std::vector<pcl::PointIndices> cluster_indices;
  pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
  ec.setClusterTolerance (0.02); // 2cm
  ec.setMinClusterSize (100);
  ec.setMaxClusterSize (25000);
  ec.setSearchMethod (tree);

  ec.setInputCloud (cloud_filtered);
  ec.extract (cluster_indices);

  int j = 0;
  std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> sourceClouds;
  for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it){

    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
    for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit){
      cloud_cluster->points.push_back (cloud_plane->points[*pit]);
    }

    cloud_cluster->width = cloud_cluster->points.size ();
    cloud_cluster->height = 1;
    cloud_cluster->is_dense = true;
    sourceClouds.push_back(cloud_cluster);
        j++;
  }

    pcl::PointXYZRGB centroid;
    std::vector<pcl::PointXYZRGB> centroids;
        for (size_t i = 0; i < sourceClouds.size(); i++){

      pcl::computeCentroid( *(sourceClouds[i]), centroid);
      centroids.push_back(centroid);
      std::cout<<"center of pipe #"<<i+1<<" in ("<<(centroids[i]).x<<", "<<(centroids[i]).y<<", "<<(centroids[i]).z<<")"<<std::endl;
    }

pcl::toROSMsg ( *cloud_plane, output);
output.header.frame_id = input.header.frame_id;
pcl_pub.publish( output);
}

Apart from this warning:

In file included from /home/lenovo/ros_ws/src/passthrough_filter_live/src/passthrough_filter_live.cpp:14:0: /usr/include/pcl-1.8/pcl/sample_consensus/model_types.h: In function ‘void __static_initialization_and_destruction_0(int, int)’: /usr/include/pcl-1.8/pcl/sample_consensus/model_types.h:99:3: warning: ‘pcl::SAC_SAMPLE_SIZE’ is deprecated: This map is deprecated and is kept only to prevent breaking existing user code. Starting from PCL 1.8.0 model sample size is a protected member of the SampleConsensusModel class [-Wdeprecated-declarations] SAC_SAMPLE_SIZE (sample_size_pairs, sample_size_pairs + sizeof (sample_size_pairs) / sizeof (SampleSizeModel)); ^~~~~~~~~~~~~~~ /usr/include/pcl-1.8/pcl/sample_consensus/model_types.h:99:3: note: declared here

the source file is successfully built. I have compared the offline and online versions of the code and I found out that in the online version the instruction

ec.extract (cluster_indices);

doesn't actually fill cluster_indices. As a consequence, the for loop following int j = 0; is not executed.

I would like to know why is that, and if there's any solution to that.

edit retag flag offensive close merge delete