Region growing segmentation(基于区域增长的点云分割)
pcl::RegionGrowing (该类在PCL 1.7.0才有的)
pcl
::NormalEstimation 计算法向量
算法思路:
- 选择种子点:在当前点集A中选择有最小曲率的点加入种子点集
- 区域增长:寻找种子点的邻域点,对于位于A中且与种子点的法向量夹角小于阈值的邻域点,将其加入当前区域中,如果其曲率小于阈值则也加入种子点集
- 对于每个种子点,重复2,最终输出一组类,每个类的点都认为是同一平滑表面的一部分。
region_growing_segmentation.cpp
#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)
{
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);
}
CMakeLists.txt
cmake_minimum_required(
VERSION
2.8
FATAL_ERROR)
project( region_growing_segmentation)
find_package( PCL 1.5 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 })
project( region_growing_segmentation)
find_package( PCL 1.5 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 })