Segmentation
SACSegmentation
采用采样一致性方法对点云进行分割
1 #include <iostream> 2 #include <pcl/ModelCoefficients.h> 3 #include <pcl/io/pcd_io.h> 4 #include <pcl/point_types.h> 5 #include <pcl/sample_consensus/method_types.h> 6 #include <pcl/sample_consensus/model_types.h> 7 #include <pcl/segmentation/sac_segmentation.h> 8 9 int 10 main(int argc, char** argv) 11 { 12 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 13 pcl::io::loadPCDFile("D:\\in.pcd", *cloud); 14 15 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients); 16 pcl::PointIndices::Ptr inliers(new pcl::PointIndices); 17 // Create the segmentation object 18 pcl::SACSegmentation<pcl::PointXYZ> seg; 19 // Optional 20 seg.setOptimizeCoefficients(true); 21 // Mandatory 22 seg.setModelType(pcl::SACMODEL_PLANE); 23 seg.setMethodType(pcl::SAC_RANSAC); 24 seg.setDistanceThreshold(0.01); 25 26 seg.setInputCloud(cloud); 27 seg.segment(*inliers, *coefficients); 28 29 if (inliers->indices.size() == 0) 30 { 31 PCL_ERROR("Could not estimate a planar model for the given dataset."); 32 return (-1); 33 } 34 35 std::cerr << "Model coefficients: " << coefficients->values[0] << " " 36 << coefficients->values[1] << " " 37 << coefficients->values[2] << " " 38 << coefficients->values[3] << std::endl; 39 40 int num = inliers->indices.size(); 41 pcl::PointCloud<pcl::PointXYZ>::Ptr cloudOut(new pcl::PointCloud<pcl::PointXYZ>); 42 cloudOut->width = num; 43 cloudOut->height = 1; 44 cloudOut->points.resize(cloudOut->width*cloudOut->height); 45 for (int i=0;i<num;++i) 46 { 47 cloudOut->points[i] = cloud->points[inliers->indices[i]]; 48 } 49 pcl::io::savePCDFileASCII("D:\\out.pcd", *cloudOut); 50 return (0); 51 }
EuclideanClusterExtraction
RegionGrowing
The purpose of the algorithm is to merge the points that are close enough in terms of the smoothness constraint. Thereby, the output of this algorithm is the set of clusters, were each cluster is a set of points that are considered to be a part of the same smooth surface. The work of this algorithm is based on the comparison of the angles between the points normals.
First of all it sorts the points by their curvature value. It needs to be done because the region begins its growth from the point that has the minimum curvature value. The reason for this is that the point with the minimum curvature is located in the flat area (growth from the flattest area allows to reduce the total number of segments).
So we have the sorted cloud. Until there are unlabeled points in the cloud, algorithm picks up the point with minimum curvature value and starts the growth of the region. This process occurs as follows:
The picked point is added to the set called seeds.
For every seed point algorithm finds neighbouring points.
Every neighbour is tested for the angle between its normal and normal of the current seed point. If the angle is less than threshold value then current point is added to the current region.
After that every neighbour is tested for the curvature value. If the curvature is less than threshold value then this point is added to the seeds.
Current seed is removed from the seeds.
If the seeds set becomes empty this means that the algorithm has grown the region and the process is repeated from the beginning. You can find the pseudocode for the said algorithm below.
#include <iostream> #include <vector> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/search/search.h> #include <pcl/search/kdtree.h> #include <pcl/features/normal_3d.h> #include <pcl/visualization/cloud_viewer.h> #include <pcl/filters/passthrough.h> #include <pcl/segmentation/region_growing.h> #include <thread> using namespace std::chrono_literals; int main(int argc, char** argv) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile <pcl::PointXYZ>("D:\\pcd\\region_growing_tutorial.pcd", *cloud) == -1) { std::cout << "Cloud reading failed." << std::endl; return (-1); } pcl::search::Search<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>); pcl::PointCloud <pcl::Normal>::Ptr normals(new pcl::PointCloud <pcl::Normal>); pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator; normal_estimator.setSearchMethod(tree); normal_estimator.setInputCloud(cloud); normal_estimator.setKSearch(50); normal_estimator.compute(*normals); pcl::IndicesPtr indices(new std::vector <int>); pcl::PassThrough<pcl::PointXYZ> pass; pass.setInputCloud(cloud); pass.setFilterFieldName("z"); pass.setFilterLimits(0.0, 1.0); pass.filter(*indices); pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; reg.setMinClusterSize(50); reg.setMaxClusterSize(1000000); reg.setSearchMethod(tree); reg.setNumberOfNeighbours(30); reg.setInputCloud(cloud); //reg.setIndices (indices); reg.setInputNormals(normals); reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI); reg.setCurvatureThreshold(1.0); std::vector <pcl::PointIndices> clusters; reg.extract(clusters); std::cout << "Number of clusters is equal to " << clusters.size() << std::endl; pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud(); pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); //viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2); viewer->setBackgroundColor(1, 1, 1); viewer->addCoordinateSystem(1.0, "global"); viewer->addPointCloud(colored_cloud); while (!viewer->wasStopped()) { viewer->spinOnce(100); std::this_thread::sleep_for(100ms); } return (0); }
ConditionalEuclideanClustering
A segmentation algorithm that clusters points based on Euclidean distance and a user-customizable condition that needs to be hold.
ModelOutlierRemoval
根据指定的模型及其参数,从点云中抽取符合该模型的点云
// modelparameter for this sphere: // position.x: 0, position.y: 0, position.z:0, radius: 1 pcl::ModelCoefficients sphere_coeff; sphere_coeff.values.resize(4); sphere_coeff.values[0] = 0; sphere_coeff.values[1] = 0; sphere_coeff.values[2] = 0; sphere_coeff.values[3] = 1; pcl::ModelOutlierRemoval<pcl::PointXYZ> sphere_filter; sphere_filter.setModelCoefficients(sphere_coeff); sphere_filter.setThreshold(0.05); sphere_filter.setModelType(pcl::SACMODEL_SPHERE); sphere_filter.setInputCloud(cloud); sphere_filter.filter(*cloud_sphere_filtered);