// inliers为结果索引// created RandomSampleConsensus object and compute the appropriated model// 圆形
pcl::SampleConsensusModelSphere<pcl::PointXYZ>::Ptr
model_s(new pcl::SampleConsensusModelSphere<pcl::PointXYZ>(cloud));
pcl::RandomSampleConsensus<pcl::PointXYZ>ransac(model_s);
ransac.setDistanceThreshold(.01);
ransac.computeModel();
ransac.getInliers(inliers);// 平面
pcl::SampleConsensusModelPlane<pcl::PointXYZ>::Ptr
model_p(new pcl::SampleConsensusModelPlane<pcl::PointXYZ>(cloud));
pcl::RandomSampleConsensus<pcl::PointXYZ>ransac(model_p);
ransac.setDistanceThreshold(.01);
ransac.computeModel();
ransac.getInliers(inliers);
2. 估计点云表面法线
// estimate normals
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);// Object for normal estimation.
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normalEstimation;//normalEstimation.setIndices()
normalEstimation.setInputCloud(cloud);// For every point, use all neighbors in a radius of 3cm.
normalEstimation.setRadiusSearch(0.03);// A kd-tree is a data structure that makes searches efficient. More about it later.// The normal estimation object will use it to find nearest neighbors.
pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);
normalEstimation.setSearchMethod(kdtree);// Calculate the normals.
normalEstimation.compute(*normals);// visualize normals
pcl::visualization::PCLVisualizer viewer("PCL Viewer");
viewer.setBackgroundColor(0.0,0.0,0.5);
viewer.addPointCloud<pcl::PointXYZ>(cloud,"cloud");// 参数int level=2 表示每n个点绘制一个法向量// 参数float scale=0.01 表示法向量长度缩放为0.01倍
viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, normals,2,0.01,"normals");
// estimate normals
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);// Object for normal estimation.
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normalEstimation;//normalEstimation.setIndices()
normalEstimation.setInputCloud(cloud);// For every point, use all neighbors in a radius of 3cm.
normalEstimation.setRadiusSearch(0.03);// A kd-tree is a data structure that makes searches efficient. More about it later.// The normal estimation object will use it to find nearest neighbors.
pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);
normalEstimation.setSearchMethod(kdtree);// Calculate the normals.
normalEstimation.compute(*normals);// Create the PFH estimation class, and pass the input dataset+normals to it ------计算PFH直方图
pcl::PFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::PFHSignature125> pfh;
pfh.setInputCloud(cloud);
pfh.setInputNormals(normals);// alternatively, if cloud is of tpe PointNormal, do pfh.setInputNormals (cloud);// Create an empty kdtree representation, and pass it to the PFH estimation object.// Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());//pcl::KdTreeFLANN<pcl::PointXYZ>::Ptr tree (new pcl::KdTreeFLANN<pcl::PointXYZ> ()); -- older call for PCL 1.5-
pfh.setSearchMethod(tree);// Output datasets
pcl::PointCloud<pcl::PFHSignature125>::Ptr pfhs(new pcl::PointCloud<pcl::PFHSignature125>());// Use all neighbors in a sphere of radius 5cm// 使用一个半径为5厘米的球体,作为搜索邻域// IMPORTANT: the radius used here has to be larger than the radius used to estimate the surface normals!!!// 重点: 半径必须要比用于估算法向量的邻域半径要大
pfh.setRadiusSearch(0.08);// Compute the features
pfh.compute(*pfhs);