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++;
}