效果:
代码:
#include <iostream>
#include <chrono>#include <pcl/ModelCoefficients.h> // 模型系数的定义
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h> // 各种点云数据类型
#include <pcl/sample_consensus/method_types.h> // 包含用于采样一致性算法的不同方法的定义,如RANSAC、MSAC等
#include <pcl/sample_consensus/model_types.h> // 包含用于采样一致性算法的不同模型的定义,如平面、球体、圆柱体
#include <pcl/segmentation/sac_segmentation.h> // 包含用于分割点云的采样一致性算法(SACSegmentation)的定义,用于识别点云的几何模型
#include <pcl/filters/extract_indices.h> // 包含用于从点云中提取特定索引的函数和类,用于根据索引提取点云中的子集
#include <pcl/visualization/pcl_visualizer.h> // 包含了用于可视化点云的函数和类,用于在3D视窗中现实点云数据#include <pcl/features/normal_3d.h> // 估计法线
#include <pcl/filters/passthrough.h> // 直通滤波
#include <pcl/filters/voxel_grid.h> // 体素化
#include <pcl/segmentation/extract_clusters.h>
#include <iomanip>typedef pcl::PointXYZ PointT;int main(){pcl::PCDReader reader;pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);reader.read("/home/lrj/work/pointCloudData/table_scene_lms400.pcd", *cloud);std::cout << "PointCloud before filtering has: " << cloud->size() << " data points.\n";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->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 nr_points = (int) cloud_filtered->size();while (cloud_filtered->size() > 0.3 * nr_points){seg.setInputCloud(cloud_filtered);seg.segment(*inliers, *coefficients);if (inliers->indices.size() == 0){std::cout << "Cloud not estimate a planar model for the given dataset.\n";break;}pcl::ExtractIndices<pcl::PointXYZ> extract;extract.setInputCloud(cloud_filtered);extract.setIndices(inliers);extract.setNegative(false);extract.filter(*cloud_plane);extract.setNegative(true);extract.filter(*cloud_f);*cloud_filtered = *cloud_f;}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); // 2cmec.setMinClusterSize(100);ec.setMaxClusterSize(25000);ec.setSearchMethod(tree);ec.setInputCloud(cloud_filtered);ec.extract(cluster_indices);int j=0;for (const auto& cluster: cluster_indices){pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);for (const auto& idx : cluster.indices){cloud_cluster->push_back((*cloud_filtered)[idx]);}cloud_cluster->width = cloud_cluster->size();cloud_cluster->height = 1;cloud_cluster->is_dense = true;std::cout << "PointCloud representing the Cluster: " << cloud_cluster->size() << "data points.\n";std::stringstream ss;ss << std::setw(4) << std::setfill('0') << j;writer.write<pcl::PointXYZ> ("cloud_cluster_" + ss.str() + ".pcd", *cloud_cluster,false);j++;}return(0);}