#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
int
main (int argc, char** argv)
{
//讀入點雲數據table_scene_lms400.pcd
pcl::PCDReader reader;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
reader.read ("table_scene_lms400.pcd", *cloud);
std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //*
/*從輸入的.PCD文件載入數據後,我們創建了一個VoxelGrid濾波器對數據進行下采樣,我們在這裏進行下采樣的原 因是來加速處理過程,越少的點意味着分割循環中處理起來越快。*/
// Create the filtering object: downsample the dataset using a leaf size of 1cm
pcl::VoxelGrid<pcl::PointXYZ> vg; //體素柵格下采樣對象
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
vg.setInputCloud (cloud);
vg.setLeafSize (0.01f, 0.01f, 0.01f); //設置採樣的體素大小
vg.filter (*cloud_filtered); //執行採樣保存數據
std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl; //*
// Create the segmentation object for the planar model and set all the parameters
pcl::SACSegmentation<pcl::PointXYZ> seg;//創建分割對象
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
pcl::PCDWriter writer;
seg.setOptimizeCoefficients (true); //設置對估計的模型參數進行優化處理
seg.setModelType (pcl::SACMODEL_PLANE);//設置分割模型類別
seg.setMethodType (pcl::SAC_RANSAC);//設置用哪個隨機參數估計方法
seg.setMaxIterations (100); //設置最大迭代次數
seg.setDistanceThreshold (0.02); //設置判斷是否爲模型內點的距離閾值
int i=0, nr_points = (int) cloud_filtered->points.size ();
while (cloud_filtered->points.size () > 0.3 * nr_points)
{
// Segment the largest planar component from the remaining cloud
/*爲了處理點雲中包含多個模型,我們在一個循環中執行該過程,並在每次模型被提取後,我們保存剩餘的點,進行迭代。模型內點通過分割過程獲取,如下*/
seg.setInputCloud (cloud_filtered);
seg.segment (*inliers, *coefficients);
if (inliers->indices.size () == 0)
{
std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
break;
}
//移去平面局內點,提取剩餘點雲
pcl::ExtractIndices<pcl::PointXYZ> extract; //創建點雲提取對象
extract.setInputCloud (cloud_filtered); //設置輸入點雲
extract.setIndices (inliers); //設置分割後的內點爲需要提取的點集
extract.setNegative (false); //設置提取內點而非外點
// Get the points associated with the planar surface
extract.filter (*cloud_plane); //提取輸出存儲到cloud_plane
std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;
// Remove the planar inliers, extract the rest
extract.setNegative (true);
extract.filter (*cloud_f);
*cloud_filtered = *cloud_f;
}
// 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);//設置一個聚類需要的最少點數目爲100
ec.setMaxClusterSize (25000);//設置一個聚類需要的最大點數目爲25000
ec.setSearchMethod (tree);//設置點雲的搜索機制
ec.setInputCloud (cloud_filtered);
ec.extract (cluster_indices);//從點雲中提取聚類,並將點雲索引保存在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>);
//創建新的點雲數據集cloud_cluster,將所有當前聚類寫入到點雲數據集中
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++;
}
return (0);
}
版權聲明:本文爲博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/HERO_CJN/article/details/80172028
————————————————
版權聲明:本文爲CSDN博主「cjn_」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/HERO_CJN/article/details/80172028