在本教程中,我们将学习如何使用pcl::RegionGrowing 类实现的区域增长算法。算法的目的是合并在平滑度足够接近的点。因此,该算法的输出是一组簇,其中每个簇是一组点,这些点被认为是同一光滑表面的一部分。该算法的工作是基于点法线之间角度的比较。
理论:
首先,它按曲率值对点进行排序。之所以需要这样做,是因为该区域从具有最小曲率值的点开始增长。这样做的原因是,具有最小曲率的点位于平坦的区域(从最平坦的区域开始增长可以减少区段的总数)。
所以我们得到了排序后的云。直到点云中没有未标记的点为止,该算法提取曲率值最小的点并开始区域增长。此过程如下所示:
拾取的点将添加到称为“种子”的集中
对于每个种子点,该算法都会找到其相邻点。
测试每个相邻点的法线与当前种子点法线之间的角度。如果角度小于阈值,则将当前点添加到当前区域。
之后,对每个相邻点进行曲率值测试。如果曲率小于阈值,则该点将添加到种子。
当前种子将从种子集中移除。
如果种子集变为空,这意味着算法已经扩大了区域,并且从一开始就重复该过程。
源码:
创建 region_growing_segmentation.cpp 文件
- 1#include <iostream>
- 2#include <vector>
- 3#include <pcl/point_types.h>
- 4#include <pcl/io/pcd_io.h>
- 5#include <pcl/search/search.h>
- 6#include <pcl/search/kdtree.h>
- 7#include <pcl/features/normal_3d.h>
- 8#include <pcl/visualization/cloud_viewer.h>
- 9#include <pcl/filters/filter_indices.h> // for pcl::removeNaNFromPointCloud
- 10#include <pcl/segmentation/region_growing.h>
- 11
- 12int
- 13main ()
- 14{
- 15 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
- 16 if ( pcl::io::loadPCDFile <pcl::PointXYZ> ("region_growing_tutorial.pcd", *cloud) == -1)
- 17 {
- 18 std::cout << "Cloud reading failed." << std::endl;
- 19 return (-1);
- 20 }
- 21
- 22 pcl::search::Search<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
- 23 pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
- 24 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
- 25 normal_estimator.setSearchMethod (tree);
- 26 normal_estimator.setInputCloud (cloud);
- 27 normal_estimator.setKSearch (50);
- 28 normal_estimator.compute (*normals);
- 29
- 30 pcl::IndicesPtr indices (new std::vector <int>);
- 31 pcl::removeNaNFromPointCloud(*cloud, *indices);
- 32
- 33 pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
- 34 reg.setMinClusterSize (50);
- 35 reg.setMaxClusterSize (1000000);
- 36 reg.setSearchMethod (tree);
- 37 reg.setNumberOfNeighbours (30);
- 38 reg.setInputCloud (cloud);
- 39 reg.setIndices (indices);
- 40 reg.setInputNormals (normals);
- 41 reg.setSmoothnessThreshold (3.0 / 180.0 * M_PI);
- 42 reg.setCurvatureThreshold (1.0);
- 43
- 44 std::vector <pcl::PointIndices> clusters;
- 45 reg.extract (clusters);
- 46
- 47 std::cout << "Number of clusters is equal to " << clusters.size () << std::endl;
- 48 std::cout << "First cluster has " << clusters[0].indices.size () << " points." << std::endl;
- 49 std::cout << "These are the indices of the points of the initial" <<
- 50 std::endl << "cloud that belong to the first cluster:" << std::endl;
- 51 std::size_t counter = 0;
- 52 while (counter < clusters[0].indices.size ())
- 53 {
- 54 std::cout << clusters[0].indices[counter] << ", ";
- 55 counter++;
- 56 if (counter % 10 == 0)
- 57 std::cout << std::endl;
- 58 }
- 59 std::cout << std::endl;
- 60
- 61 pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud ();
- 62 pcl::visualization::CloudViewer viewer ("Cluster viewer");
- 63 viewer.showCloud(colored_cloud);
- 64 while (!viewer.wasStopped ())
- 65 {
- 66 }
- 67
- 68 return (0);
- 69}
说明:
1、相关的头文件
- 1#include <iostream>
- 2#include <vector>
- 3#include <pcl/point_types.h>
- 4#include <pcl/io/pcd_io.h>
- 5#include <pcl/search/search.h>
- 6#include <pcl/search/kdtree.h>
- 7#include <pcl/features/normal_3d.h>
- 8#include <pcl/visualization/cloud_viewer.h>
- 9#include <pcl/filters/filter_indices.h> // for pcl::removeNaNFromPointCloud
- 10#include <pcl/segmentation/region_growing.h>
- 11
2、加载点云pcd文件
- 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);
- }
3、计算点的法线
- 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);
4、去除一些无效点
- pcl::IndicesPtr indices (new std::vector <int>);
- pcl::removeNaNFromPointCloud(*cloud, *indices);
5、实例化pcl::RegionGrowing。它是一个模板类,有两个参数:
PointT—要使用的点的类型(在给定的示例中为pcl::PointXYZ)
NormalT—要使用的法线类型(在给定的示例中为pcl::Normal)
然后设置最小和最大集群大小。这意味着分割完成后,所有点小于最小值(或大于最大值)的簇都将被丢弃。最小值和最大值的默认值分别为1和“尽可能多”。
- pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
- reg.setMinClusterSize (50);//最小集群大小
- reg.setMaxClusterSize (1000000);//最大集群大小
- reg.setSearchMethod (tree);//使用kdtree最近邻搜索
- 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);
6、输出一些信息
- 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;
7、RegionGrowing类提供了一个方法,该方法返回彩色云,其中每个集群都有自己的颜色。因此,在这部分代码中,pcl::visualization::CloudViewer被实例化,用于查看分割结果-相同颜色的云。
- 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、将以下行添加到CmakeList.txt文件:
- 1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
- 2
- 3project(region_growing_segmentation)
- 4
- 5find_package(PCL 1.5 REQUIRED)
- 6
- 7include_directories(${PCL_INCLUDE_DIRS})
- 8link_directories(${PCL_LIBRARY_DIRS})
- 9add_definitions(${PCL_DEFINITIONS})
- 10
- 11add_executable (region_growing_segmentation region_growing_segmentation.cpp)
- 12target_link_libraries (region_growing_segmentation ${PCL_LIBRARIES})
2、运行
$ ./region_growing_segmentation
3、输出
