算法核心:该算法是基于点法线之间角度的比较,企图将满足平滑约束的相邻点合并在一起,以一簇点集的形式输出。每簇点集被认为是属于相同平面。
工作原理:首先需要明白,区域增长是从有最小曲率值(curvature value)的点开始的。因此,我们必须计算出所有曲率值,并对它们进行排序。这是因为曲率最小的点位于平坦区域,而从最平坦的区域增长可以减少区域的总数。现在我们来具体描述这个过程:
1.点云中有未标记点,按照点的曲率值对点进行排序,找到最小曲率值点,并把它添加到种子点集;
2.对于每个种子点,算法都会发现周边的所有近邻点。1)计算每个近邻点与当前种子点的法线角度差(reg.setSmoothnessThreshold),如果差值小于设置的阈值,则该近邻点被重点考虑,进行第二步测试;2)该近邻点通过了法线角度差检验,如果它的曲率小于我们设定的阈值(reg.setCurvatureThreshold),这个点就被添加到种子点集,即属于当前平面。
3.通过两次检验的点,被从原始点云去除。
4.设置最小点簇的点数min(reg.setMinClusterSize),最大点簇为max(reg.setMaxClusterSize)。
4.重复1-3步,算法会生成点数在min和max的所有平面,并对不同平面标记不同颜色加以区分。
5.直到算法在剩余点中生成的点簇不能满足min,算法停止工作
附上一个基于区域增长的分割:
#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<sstream>
#include"IO.h"
using namespace std;
int GetRandomNumber()
{
//srand((unsigned)time(NULL));
int RandomNumber;
RandomNumber = rand() % (250) + 0;//0到255之间选择颜色
//生成其他范围的数字:RandomNumber = rand() % (b-a+1) + a;
return RandomNumber;
}
int
main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile <pcl::PointXYZ>("D:\\建筑物屋面提取测试数据\\build.pcd", *cloud) == -1)
{
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);
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;//创建区域分割对象
reg.setMinClusterSize(2);//设置一个类别需要的最小点数
reg.setMaxClusterSize(1000000);//设置一个类别需要的最多点数
reg.setSearchMethod(tree);//设置搜索方法
reg.setNumberOfNeighbours(30);//搜索近邻点数目
reg.setInputCloud(cloud);//设置输入点云
//reg.setIndices (indices);
reg.setInputNormals(normals);//输入法向量
reg.setSmoothnessThreshold(2/ 180.0 * M_PI);//设置平滑阈值
reg.setCurvatureThreshold(1.0);//设置曲率阈值
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
//分割结果显示
pcl::visualization::PCLVisualizer viewer("区域增长提取平面");
viewer.setBackgroundColor(0, 0, 0);//黑色背景
int ii = 0;
IO IOExample;
for (int i = 0; i < clusters.size(); i++)//每一块进行显示
{
vector<pcl::PointXYZ> TempCluster;
for (int i = 0; i < clusters[ii].indices.size(); i++)
{
TempCluster.push_back(cloud->points[clusters[ii].indices[i]]);
}
ii = ii + 1;
stringstream ss;
ss << ii;
string str;
ss >> str;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_SinglePlane(new pcl::PointCloud<pcl::PointXYZ>);
cloud_SinglePlane = IOExample.PointXYZ2Ptr(TempCluster);
int R = GetRandomNumber();
int G = GetRandomNumber();
int B = GetRandomNumber();
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud_SinglePlane, R, G, B);
viewer.addPointCloud<pcl::PointXYZ>(cloud_SinglePlane, single_color, str);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, str);
}
pcl::visualization::PCLVisualizer viewerBeforeRegionGrowing("区域增长分割前点云");
viewerBeforeRegionGrowing.setBackgroundColor(0, 0, 0);//黑色背景
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color_base(cloud, 255, 0, 0);
viewerBeforeRegionGrowing.addPointCloud<pcl::PointXYZ>(cloud, single_color_base, "origin");
viewerBeforeRegionGrowing.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "origin");
while (!viewer.wasStopped() || !viewerBeforeRegionGrowing.wasStopped())
{
viewer.spinOnce(1);
viewerBeforeRegionGrowing.spinOnce(1);
}
return (0);
}
简单分割结果