PCL学习:点云分割-基于颜色的区域生长分割

基于颜色的区域生长分割

基于颜色的区域生长分割原理上和基于曲率、法线的分割方法是一致的。只不过比较目标换成了颜色,去掉了点云规模上 限的限制。可以认为,同一个颜色且挨得近,是一类的可能性很大,不需要上限来限制。所以这种方式比较适合用于室内场景分割。尤其是复杂室内场景,颜色分割可以轻松的将连续的场景点云变成不同的物体。哪怕是高低不平的地面,没法用采样一致分割器抽掉,颜色分割算法同样能完成分割任务。

算法分为两步:

(1)分割,当前种子点和领域点之间色差小于色差阀值的视为一个聚类;

(2)合并,聚类之间的色差小于色差阀值和并为一个聚类,且当前聚类中点的数量小于聚类点数量的与最近的聚类合并在一起;

测试示例: 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace pcl::console;
int
main (int argc, char** argv)
{

	if(argc<2)
	{
		std::cout<<".exe xx.pcd -b_n 0 -kn 50 -st_n 30 -ct_n 0.05 -bc 0 -fc 10 -nc 0 -dt 10 -ct 6 -rt 5 -mt 600" <::Ptr cloud (new pcl::PointCloud);
	if ( pcl::io::loadPCDFile  (argv[1], *cloud) == -1)
	{
		std::cout << "Cloud reading failed." << std::endl;
		return (-1);
	}
	end = time(0); 
	diff[0] = difftime (end, start); 
	PCL_INFO ("\Loading pcd file takes(seconds): %d\n", diff[0]); 
	pcl::search::Search ::Ptr tree = boost::shared_ptr > (new pcl::search::KdTree);

	//Noraml estimation step(1 parameter)
	pcl::search::Search::Ptr tree1 = boost::shared_ptr > (new pcl::search::KdTree);
	pcl::PointCloud ::Ptr normals (new pcl::PointCloud );
	pcl::NormalEstimation normal_estimator;
	normal_estimator.setSearchMethod (tree);
	normal_estimator.setInputCloud (cloud);
	normal_estimator.setKSearch (KN_normal);
	normal_estimator.compute (*normals);
	end = time(0); 
	diff[1] = difftime (end, start)-diff[0]; 
	PCL_INFO ("\Estimating normal takes(seconds): %d\n", diff[1]); 
	//Optional step: cutting away the clutter scene too far away from camera
	pcl::IndicesPtr indices (new std::vector );
	if(Bool_Cuting)//是否通过z轴范围对待处理数据进行裁剪
	{

		pcl::PassThrough pass;
		pass.setInputCloud (cloud);
		pass.setFilterFieldName ("z");
		pass.setFilterLimits (near_cuting, far_cuting);
		pass.filter (*indices);
	}
	// Region growing RGB 
	pcl::RegionGrowingRGB reg;  //创建PointXYZRGB类型的区域生长分割对象
	reg.setInputCloud (cloud);                    //设置输入点云
	if(Bool_Cuting)reg.setIndices (indices);      //设置输入点云的索引
	reg.setSearchMethod (tree);                   //设置点云的搜索机制
	reg.setDistanceThreshold (DistanceThreshold); //设置距离阈值,用于判断两点是否是相邻点
	reg.setPointColorThreshold (ColorThreshold);  //设置两点颜色阈值,用于判断两点是否属于同一类
	reg.setRegionColorThreshold (RegionColorThreshold); //设置两类区域区域颜色阈值,用于判断两类区域是否聚类合并
	reg.setMinClusterSize (MinClusterSize);       //设置一个聚类的最少点数目为600
	if(b_n)
	{
		reg.setSmoothModeFlag(true);
		reg.setCurvatureTestFlag(true);

		reg.setInputNormals (normals);
		reg.setSmoothnessThreshold (SmoothnessThreshold / 180.0 * M_PI);
		reg.setCurvatureThreshold (CurvatureThreshold);
	}
	std::vector  clusters;
	reg.extract (clusters);
	end = time(0); 
	diff[2] = difftime (end, start)-diff[0]-diff[1]; 
	PCL_INFO ("\RGB region growing takes(seconds): %d\n", diff[2]); 

	pcl::PointCloud ::Ptr colored_cloud = reg.getColoredCloud ();
	pcl::visualization::CloudViewer viewer ("基于颜色的区域生长算法实现点云分割");
	viewer.showCloud (colored_cloud);
	while (!viewer.wasStopped ())
	{
		boost::this_thread::sleep (boost::posix_time::microseconds (100));
	}

	return (0);
}

 .\region_growing_rgb_segmentation.exe ..\..\source\two_human.pcd -b_n 0 -kn 50 -st_n 30 -ct_n 0.05
 -bc 0 -fc 10 -nc 0 -dt 10 -ct 6 -rt 5 -mt 600

Loading pcd file takes(seconds): 32
Estimating normal takes(seconds): 83
RGB region growing takes(seconds): 431

PCL学习:点云分割-基于颜色的区域生长分割_第1张图片

你可能感兴趣的:(PCL,点云库PCL从入门到精通)