算法思想
首先依据点的曲率值对点进行排序,之所以排序,是因为区域生长算法是从曲率最小的点开始生长的,这个点就是初始种子点,初始种子点所在的区域即为最平滑的区域,从最平滑的区域开始生长可减少分割片段的总数,提高效率。
算法流程
设置一空的种子点序列和空的聚类数组,选好初始种子后,将其加入到种子点序列中,并搜索邻域点。对每一个邻域点,比较邻域点的法线与当前种子点的法线之间的夹角,小于平滑阀值的将当前点加入到当前区域。然后检测每一个邻域点的曲率值,小于曲率阀值的加入到种子点序列中。在进行种子点领域判断后,删除当前的种子点,利用新加入的种子点继续生长,重复进行以上生长过程,直到种子序列被清空。一个区域生长完成,将其加入聚类数组。最后,利用曲率值从小到大排序,顺序选择输入点集的点作为种子点加入到种子序列中,重复以上生长的步骤,这样就通过区域生长实现了点云分割。
算法流程总结:
- 种子周围的临近点和种子点云相比较
- 法线的方向是否足够相近
- 曲率是否足够小
- 如果满足2,3则该点可用做种子点
- 如果只满足2,则归类而不做种子点
- 从某个种子出发,其“子种子”不再出现则一类聚集完成
- 类的规模既不能太大也不能太小
代码展示:
#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>
typedef pcl::PointXYZ PointT;
int main ( )
{
//点云的类型
pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
//打开点云
if ( pcl::io::loadPCDFile("/home/ancy/Desktop/tradition/data/tutorials/table_scene_lms400.pcd", *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
//设置搜索的方式
pcl::search::Search<PointT>::Ptr tree(new pcl::search::KdTree<PointT>);
//求法线
pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
pcl::NormalEstimation<PointT, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod (tree);
normal_estimator.setInputCloud (cloud);
normal_estimator.setKSearch (50);
normal_estimator.compute (*normals);
//直通滤波在Z轴的0到1米之间
pcl::IndicesPtr indices (new std::vector <int>);
pcl::PassThrough<PointT> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.0, 1.0);
pass.filter (*indices);
//聚类对象<点,法线>
pcl::RegionGrowing<PointT, pcl::Normal> reg;
reg.setMinClusterSize (5000); //最小的聚类的点数
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;
// 输出第一个聚类的数量
std::cout << "First cluster has " << clusters[0].indices.size () << " points." << endl;
std::cout << "These are the indices of the points of the initial" <<
std::endl << "cloud that belong to the first cluster:" << std::endl;
int 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);
}
效果展示:
测试文件地址: table_scene_lms400.pcd