一、区域增长算法(Region growing segmentation)
在本教程中,我们将学习如何使用在 pcl::RegionGrowing 类中实现的区域增长算法。所述算法的目的是合并在平滑度约束方面足够接近的点。因此,此算法的输出是一组聚类,其中每个聚类是一组被视为同一光滑表面一部分的点。该算法的工作基于点法线之间角度的比较。
二、理论入门
让我们来看看算法是如何工作的。
首先,它根据曲率值对点进行排序。它需要完成,因为该区域从具有最小曲率值的点开始增长。这样做的原因是曲率最小的点位于平坦区域(从最平坦区域增长可以减少分段总数)。所以我们有分类的云。直到云中没有未标记的点,算法选择具有最曲率值小的点并开始区域的增长。这个过程发生如下:拾取的点被添加到称为种子的集合中。对于每个种子点,该算法都会找到它的相邻点。测试每个邻居的法线与当前种子点法线之间的角度。如果角度小于阈值,则将当前点添加到当前区域。之后,测试每个邻居的曲率值。如果曲率小于阈值,则将此点添加到种子中。从种子中删除当前种子。如果种子集变为空,则意味着算法已增大该区域,并且从头开始重复该过程。您可以在下面找到所述算法的伪代码。
Inputs
Initialize:
Algorithm:
三、代码
#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/filter_indices.h> // for pcl::removeNaNFromPointCloud
#include <pcl/segmentation/region_growing.h>
int
main ()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if ( pcl::io::loadPCDFile <pcl::PointXYZ> ("../nono_filtered_inliers.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::removeNaNFromPointCloud(*cloud, *indices);
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
reg.setMinClusterSize (1000);
reg.setMaxClusterSize (1000000);
reg.setSearchMethod (tree);
reg.setNumberOfNeighbours (30);
reg.setInputCloud (cloud);
reg.setIndices (indices);
reg.setInputNormals (normals);
reg.setSmoothnessThreshold (4.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;
std::cout << "First cluster has " << clusters[0].indices.size () << " points." << std::endl;
std::cout << "These are the indices of the points of the initial" <<
std::endl << "cloud that belong to the first cluster:" << std::endl;
std::size_t counter = 0;
while (counter < clusters[0].indices.size ())
{
std::cout << clusters[0].indices[counter] << ", ";
counter++;
if (counter % 10 == 0)
std::cout << std::endl;
}
std::cout << std::endl;
pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud ();
pcl::visualization::CloudViewer viewer ("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped ())
{
}
return (0);
}
四、解释
reg.setSmoothnessThreshold (4.0 / 180.0 * M_PI); reg.setCurvatureThreshold (1.0);
这两行是算法初始化中最重要的部分,因为它们负责上述平滑度约束。第一种方法设置弧度角度,该角度将用作法线偏差的允许范围。如果点法线之间的偏差小于平滑度阈值,则建议它们位于同一聚类中(新点 - 测试点 - 将添加到聚类中)。第二个负责曲率阈值。如果两个点的法线偏差很小,则测试它们曲率之间的差异。如果该值小于曲率阈值,则算法将使用新添加的点继续集群的增长。