PCL1.8.1 分割 - 欧式聚类

Euclidean Cluster Extraction

欧式聚类流程以下:php

设置一个合适的聚类搜索半径Cluster-Tolerance很重要,若是半径太小,那么会分割出多个对象,若是设置过高会形成多个对应聚类到成一个,所以须要测试找到合适的搜索半径。ide

http://pointclouds.org/documentation/tutorials/cluster_extraction.php#cluster-extraction测试

#include <pcl/kdtree/kdtree.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/segmentation/extract_clusters.h>

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);

// Creating the KdTree object for the search method of the extraction
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud (cloud_filtered);

std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.setClusterTolerance (0.02); //设置近邻搜索的搜索半径2cm
ec.setMinClusterSize (100);    //设置一个聚类须要的最少点数目
ec.setMaxClusterSize (25000);  //设置一个聚类须要的最大数目
ec.setSearchMethod (tree);     //设置点云的搜索方法
ec.setInputCloud (cloud_filtered);
ec.extract (cluster_indices);        //从点云中提取聚类并保存到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++;
}
相关文章
相关标签/搜索