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

Revision history [back]

In this part:

int j = 0;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
for (std::vector<pcl::PointIndices>::const_iterator it = clusters.begin (); it != clusters.end (); ++it)
  {
      for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
      cloud_cluster->points.push_back (cloud->points[*pit]); //*
      cloud_cluster->width = cloud_cluster->points.size ();
      cloud_cluster->height = 1;
      cloud_cluster->is_dense = true;
      std::ostringstream ss;
      ss << j;
      pcl::PCDWriter writer5;
     writer5.write<pcl::PointXYZRGB> ("~/catkin_ws/cloud_objects"+ss.str()+".pcd", *cloud_cluster, false);
     std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
     j++;
 }

you are adding each new cluster to previous one(s) and saving again. Therefore the result will be exactly as you explained.

You should clear point cloud at each iteration and fill it with only points of new cluster.

Try adding cloud_cluster->clear(); for each cluster iteration, like;

int j = 0;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
for (std::vector<pcl::PointIndices>::const_iterator it = clusters.begin (); it != clusters.end (); ++it)
  {
      cloud_cluster->clear();
      for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
      cloud_cluster->points.push_back (cloud->points[*pit]); //*
      cloud_cluster->width = cloud_cluster->points.size ();
      cloud_cluster->height = 1;
      cloud_cluster->is_dense = true;
      std::ostringstream ss;
      ss << j;
      pcl::PCDWriter writer5;
     writer5.write<pcl::PointXYZRGB> ("~/catkin_ws/cloud_objects"+ss.str()+".pcd", *cloud_cluster, false);
     std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
     j++;
 }

In this part:

int j = 0;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
for (std::vector<pcl::PointIndices>::const_iterator it = clusters.begin (); it != clusters.end (); ++it)
  {
      for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
      cloud_cluster->points.push_back (cloud->points[*pit]); //*
      cloud_cluster->width = cloud_cluster->points.size ();
      cloud_cluster->height = 1;
      cloud_cluster->is_dense = true;
      std::ostringstream ss;
      ss << j;
      pcl::PCDWriter writer5;
     writer5.write<pcl::PointXYZRGB> ("~/catkin_ws/cloud_objects"+ss.str()+".pcd", *cloud_cluster, false);
     std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
     j++;
 }

you are adding each new cluster to previous one(s) and saving again. Therefore the result will be exactly as you explained.

You should clear point cloud at each iteration and fill it with only points of new cluster.

Try adding cloud_cluster->clear(); for each cluster iteration, like;

int j = 0;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
for (std::vector<pcl::PointIndices>::const_iterator it = clusters.begin (); it != clusters.end (); ++it)
  {
      cloud_cluster->clear();
      for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
      cloud_cluster->points.push_back (cloud->points[*pit]); //*
      cloud_cluster->width = cloud_cluster->points.size ();
      cloud_cluster->height = 1;
      cloud_cluster->is_dense = true;
      std::ostringstream ss;
      ss << j;
      pcl::PCDWriter writer5;
     writer5.write<pcl::PointXYZRGB> ("~/catkin_ws/cloud_objects"+ss.str()+".pcd", *cloud_cluster, false);
     std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
     j++;
 }

UPDATE: Also you can check this.