歐式聚類流程以下: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++; }