前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PCL中的区域生长分割(region growing segmentation)

PCL中的区域生长分割(region growing segmentation)

作者头像
点云PCL博主
发布2019-07-30 16:40:26
6.2K0
发布2019-07-30 16:40:26
举报
文章被收录于专栏:点云PCL点云PCL

首先注意一点,这里是region growing segmentation,不是color-based region growing segmentation.

算法核心:该算法是基于点法线之间角度的比较,企图将满足平滑约束的相邻点合并在一起,以一簇点集的形式输出。每簇点集被认为是属于相同平面。

工作原理:首先需要明白,区域增长是从有最小曲率值(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,算法停止工作。

算法具体的伪码表示:http://pointclouds.org/documentation/tutorials/region_growing_segmentation.php#region-growing-segmentation(以下代码向左滑动可以看到完整版)

代码语言:javascript
复制
#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>

intmain (int argc, char** argv){
 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
 if ( pcl::io::loadPCDFile <pcl::PointXYZ> ("region_growing_tutorial.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::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.setMinClusterSize (50);
 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);}

注释:

1.文件输入、输出最好使用绝对路径;

2.代码中涉及到的参数:

setKSearch(): 这是在计算点的法线时,设置邻域内需要多少点来模拟平面计算法线。法线计算:http://geometryhub.net/notes/pointcloudnormal

setMInClusterSize() setMaxClusterSize() 原理中已经提到

setNumberOfneighbours() 指的是区域增长时种子点附近纳入检验的点数

setSmoothnessThreshold() setCurvatureThreshold() 原理中已经提到。这两个阈值的设置尤其重要,它们是region growing segmentation的核心。

3.注意,输入点点类型为 pcl::PointCloud<pcl::PointXYZ>,输出点为pcl::PointXYZRGB,因为分割完之后不同平面信息被 不同颜色标记,而被抛弃的点被红色标记。如下图所示。点类型根据读者输入的点云数据包含的列信息而定,在此不在详细解释。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 点云PCL 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档