一. 算法核心:
该算法是基于点法线之间角度的比较,将满足平滑约束的相邻点合并在一起,以一簇点集的形式输出。每簇点集被认为是属于相同平面。
二. 工作原理:
区域增长是从有最小曲率值(curvature value)的点开始的。因此,我们必须计算出所有曲率值,并对它们进行排序。这是因为曲率最小的点位于平坦区域,而从最平坦的区域增长可以减少区域的总数。现在我们来具体描述这个过程:
1.点云中有未标记点,按照点的曲率值对点进行排序,找到最小曲率值点,并把它添加到种子点集;
2.对于每个种子点,算法都会发现周边的所有近邻点。
1) 计算每个近邻点与当前种子点的法线角度差(reg.setSmoothnessThreshold),如果差值小于设置的阈值,则该近邻点被重点考虑,进行第二步测试;
2) 该近邻点通过了法线角度差检验,如果它的曲率小于我们设定的阈值(reg.setCurvatureThreshold),这个点就被添加到种子点集,即属于当前平面。
3.通过两次检验的点,被从原始点云去除。
4.设置最小点簇的点数min(reg.setMinClusterSize),最大点簇为max(reg.setMaxClusterSize)。
5.重复1-3步,算法会生成点数在min和max的所有平面,并对不同平面标记不同颜色加以区分。
6.直到算法在剩余点中生成的点簇不能满足min,算法停止工作。
三.工程文件中的代码.cpp:
算法的使用流程:
1、输入点云;
2、构建k-dtree;
3、估计法线;
4、区域增长。
// Region growing clustering object.
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> clustering;
clustering.setMinClusterSize(100);
clustering.setMaxClusterSizsetMinClusterSizee(10000);
clustering.setSearchMethod(kdtree);
clustering.setNumberOfNeighbours(30);
clustering.setInputCloud(cloud);
clustering.setInputNormals(normals);
// Set the angle in radians that will be the smoothness threshold
// (the maximum allowable deviation of the normals).
clustering.setSmoothnessThreshold(7.0 / 180.0 * M_PI); // 7 degrees.
// Set the curvature threshold. The disparity between curvatures will be
// tested after the normal deviation check has passed.
clustering.setCurvatureThreshold(1.0);
std::vector <pcl::PointIndices> clusters;
clustering.extract(clusters);
// growth.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#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>
int main(int argc, char** argv)
{
DWORD t1, t2;
t1 = GetTickCount();//以上两句和最后return 0 之上的为计时函数。
//点云的类型
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//打开点云
if (pcl::io::loadPCDFile <pcl::PointXYZ
("C:/Users/10859/Desktop/pointcloudsdata/bunny.pcd", *cloud) == -1)//改成想要输入的点云名称...*cloud就是把输入的点云记录到变量指针cloud中。
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
//设置搜索的方式或者说是结构
pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> >(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);
//直通滤波在Z轴的0到1米之间
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寄存器(区域曾长的对象)
reg.setMinClusterSize(50); //最小的聚类的点数(小于这参数的平面被忽略不计)
reg.setMaxClusterSize(1000000); //最大的(一般随便设置)
reg.setSearchMethod(tree); //搜索方式(采用的默认是K—d树法)
reg.setNumberOfNeighbours(30); //设置搜索的邻域点的个数,周围多少个点决定这是一个平面(决定容错率,设置大时有倾斜也可接受,设置小时检测到的平面会很小)
reg.setInputCloud(cloud); //输入点
//reg.setIndices (indices);
reg.setInputNormals(normals); //输入的法线
reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI); //设置平滑度(设置两个法线在多大夹角内可当做是共面的)
reg.setCurvatureThreshold(1.0); //设置曲率的阈值
//最后也是一个弯曲的阈值,这个决定了比当前考察的点和平均的法线角度,决定是否还有继续探索下去的必要。
//(也就是假设每个点都是平稳弯曲的,那么normal的夹角都很小,但是时间长了偏移的就大了,这个参数就是限制这个用的)
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
//把结果输出到一个簇里面,这个簇会自动把每个平面分成一个vector,可以打印下来看看
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())
{
}
pcl::PCDWriter writer;//将点云写入磁盘
writer.write("C:/Users/10859/Desktop/pointcloudsdata/bunnyresult.pcd",
*colored_cloud, false);//改成想要输出的点云名称
t2 = GetTickCount(); //从这句到return 0之间的两句为计时函数
printf("Use Time:%f\n", (t2 - t1)*1.0 / 1000);
system("pause");
return (0);
}
四.cmake文件参照如下:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(region_growing_segmentation)
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (region_growing_segmentation region_growing_segmentation.cpp)
target_link_libraries (region_growing_segmentation ${PCL_LIBRARIES})
注释:
1.文件输入、输出最好使用绝对路径;
2.代码中涉及到的参数:
setKSearch(): 这是在计算点的法线时,设置邻域内需要多少点来模拟平面计算法线。法线计算:http://geometryhub.net/notes/pointcloudnormal
setMInClusterSize() setMaxClusterSize() 原理中已经提到
setNumberOfneighbours() 指的是区域增长时种子点附近纳入检验的点数
setSmoothnessThreshold() setCurvatureThreshold() 原理中已经提到。这两个阈值的设置尤其重要,它们是region growing segmentation的核心。
3.注意,输入点点类型为 pcl::PointCloudpcl::PointXYZ,输出点pcl::PointXYZRGB,因为分割完之后不同平面信息被 不同颜色标记,而被抛弃的点被红色标记。如下图所示。点类型根据读者输入的点云数据包含的列信息而定,在此不在详细解释。
头部、耳朵、尾巴等都被较好的区分开来.
参考:https://blog.csdn.net/weixin_44849088/article/details/103411003
https://blog.csdn.net/aishuirenjia/article/details/80239562