#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>
#include <pcl/visualization/pcl_visualizer.h>
int
main(int argc, char** argv)
{
// Read in the cloud data
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; //*
// 下采样滤波
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; //*
//创建分割对象和参数
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);
//提取点
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud(cloud_filtered);
extract.setIndices(inliers);
extract.setNegative(false);
// 提取的平面上的点保存到cloud_plane
extract.filter(*cloud_plane);
std::cout <<cloud_plane->points.size() <<:: endl STD;
// the Remove The Planar inliers, The REST Extract
extract.setNegative ( to true );
extract.filter ( * cloud_f);
cloud_filtered = cloud_f;
}
// Create a tree object Kd as the extraction method used in cloud point ,
PCL :: :: Search KdTree <PCL PointXYZ ::> :: the Ptr Tree ( new new PCL :: :: Search KdTree <PCL :: PointXYZ> );
Tree -> setInputCloud (cloud_filtered);
STD :: Vector <PCL: : PointIndices> cluster_indices;
PCL EuclideanClusterExtraction :: <PCL :: PointXYZ> EC; // cluster
ec.setClusterTolerance (0.02 ); // set the search radius neighbor searching 2cm
ec.setMinClusterSize ( 100 ); // get a minimum number of points required for a cluster 100
ec.setMaxClusterSize ( 25000 ); // set the maximum number of points required for a cluster as mesh 25000
ec.setSearchMethod (Tree); // set point cloud search mechanism
ec.setInputCloud (cloud_filtered); // set the original point cloud
ec.extract (cluster_indices); // extracted from the point cloud clustering
// visualization part
PCL :: :: PCLVisualizer Visualization Viewer ( " segmention " );
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";
int v2(0);
pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> cloud_in_color_h(cloud);//赋予显示点云的颜色,随机
viewer.addPointCloud(cloud_cluster, cloud_in_color_h, std::to_string(j));
j++;
Wait until the visualization window is closed.//
}
the while (! viewer.wasStopped ())
{
viewer.spinOnce ( 100 ); // refresh the screen display time, 100 to allow the maximum time to re-render a number of ms.
:: :: this_thread SLEEP Boost (Boost :: :: posix_time microseconds ( 100000 ));
}
return ( 0 );
}