PCL1.8.1 多个分割点云的保存

徘徊边缘 提交于 2019-12-06 02:37:02

 std::vector <pcl::PointIndices>

在使用分割算法分割点云时,会生成多个pcl::PointIndices,通常会保存在std::vector中,以下分割算法会生成多个pcl::PointIndices:

std::vector<pcl::PointIndices> cluster_indices;

//欧式聚类分割
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.extract (cluster_indices);

//区域增长分割
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
reg.extract (cluster_indices);

保存pcl::PointIndices到文件的代码实现如下:

#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>

//输入点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
//保存分割后的点云索引组
std::vector<pcl::PointIndices> cluster_indices;

int j = 0;
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
{
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
  for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit)
    cloud_cluster->points.push_back (cloud_filtered->points[*pit]); //*
  cloud_cluster->width = cloud_cluster->points.size ();
  cloud_cluster->height = 1;
  cloud_cluster->is_dense = true;

  std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
  std::stringstream ss;
  ss << "cloud_cluster_" << j << ".pcd";
  writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
  j++;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!