PCL——点云分割(区域生长的分割)

点云分割是根据空间、几何和纹理等特征对点云进行划分,使得同一划分内的点云拥有相似的特征。

基于区域生长的分割

种子点:根据点的曲率值对点云进行排序,曲率最小的点叫做初始种子点,
(1)区域生长算法从曲率最小的种子点开始生长,初始种子点所在区域为最平滑区域,从初始种子点所在的区域开始生长可减小分割片段的总数,从而提高算法的效率。
(2)设置一空的聚类区域C和空的种子点序列Q,聚类数组L。
(3)选好初始种子点,将其加入种子点序列Q中,并搜索该种子点的领域点,计算每一个领域点法线与种子点法线之间的夹角,小于设定的平滑阀值时,将领域点加入到C中,同时判断该领域点的曲率值是否小于曲率阀值,将小于曲率阈值的领域点加入种子点序列Q中,在邻域点都判断完成后,删除当前种子点,在Q中重新选择新的种子点重复上述步骤,直到Q中序列为空,一个区域生长完成,将其加入聚类数组L中。
(4)利用曲率值从小到大排序,顺序选择输入点集 的点作为种子点加入到种子点序列中,重复以上生长的步骤。

#include 
#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 -kn 50 -bc 0 -fc 10.0 -nc 0 -st 30 -ct 0.05" << endl;

		return 0;
	}
	time_t start, end, diff[5], option;
	start = time(0);
	int KN_normal = 10; 
	bool Bool_Cuting = false;
	float far_cuting = 10, near_cuting = 0, SmoothnessThreshold = 5.0, CurvatureThreshold = 0.05;
	parse_argument(argc, argv, "-kn", KN_normal);
	parse_argument(argc, argv, "-bc", Bool_Cuting);
	parse_argument(argc, argv, "-fc", far_cuting);
	parse_argument(argc, argv, "-nc", near_cuting);
	parse_argument(argc, argv, "-st", SmoothnessThreshold);
	parse_argument(argc, argv, "-ct", CurvatureThreshold);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	if (pcl::io::loadPCDFile <pcl::PointXYZ>(argv[1], *cloud) == -1)
	{
		std::cout << "Cloud reading failed." << std::endl;
		return (-1);
	}
	end = time(0);
	diff[0] = difftime(end, start);
	PCL_INFO("\tLoading pcd file takes(seconds): %d\n", diff[0]);
	//Noraml estimation step(1 parameter)
	
	pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> >(new pcl::search::KdTree<pcl::PointXYZ>);//����һ��ָ��kd����������Ĺ���ָ��
	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(KN_normal);
	normal_estimator.compute(*normals);
	end = time(0);
	diff[1] = difftime(end, start) - diff[0];
	PCL_INFO("\tEstimating normal takes(seconds): %d\n", diff[1]);
	//optional step: cutting the part are far from the orignal point in Z direction.2 parameters
	pcl::IndicesPtr indices(new std::vector <int>);
	if (Bool_Cuting)
	{

		pcl::PassThrough<pcl::PointXYZ> pass;
		pass.setInputCloud(cloud);
		pass.setFilterFieldName("z");
		pass.setFilterLimits(near_cuting, far_cuting);
		pass.filter(*indices);
	}


	
	pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
	reg.setMinClusterSize(100);
	reg.setMaxClusterSize(500000);
	reg.setSearchMethod(tree);
	reg.setNumberOfNeighbours(10);//然后设置参考的邻域点数,也就是看看周边的多少个点来决定这是一个平面(这个参数至关重要,决定了你的容错率,如果设置的很大,那么从全局角度看某一个点稍微有点歪也可以接受,如果设置的很小则通常检测到的平面都会很小)
	reg.setInputCloud(cloud);
	if (Bool_Cuting)reg.setIndices(indices);
	reg.setInputNormals(normals);
	reg.setSmoothnessThreshold(5 / 180.0 * M_PI);//平滑阈值,法向量的角度差
	reg.setCurvatureThreshold(0.05);//曲率阈值,代表平坦的程度

	std::vector <pcl::PointIndices> clusters;
	reg.extract(clusters);
	end = time(0);
	diff[2] = difftime(end, start) - diff[0] - diff[1]; 
	PCL_INFO("\tRegion growing takes(seconds): %d\n", diff[2]);

	std::cout << "Number of clusters is equal to " << clusters.size() << std::endl;
	std::cout << "First cluster has " << clusters[0].indices.size() << " points." << endl;




	//保存聚类的点云-------------------------------------------------------------------
	int j = 0;
	for (std::vector<pcl::PointIndices>::const_iterator it = clusters.begin(); it != clusters.end(); ++it)
	{
		pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
		
		for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); pit++)
			cloud_cluster->points.push_back(cloud->points[*pit]);
		cloud_cluster->width = cloud_cluster->points.size();
		cloud_cluster->height = 1;
		cloud_cluster->is_dense = true;

		std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size() << " data points."
			<< std::endl;
		std::stringstream ss;
		ss << "cloud_cluster_" << j << ".pcd";
		pcl::io::savePCDFileASCII(ss.str(), *cloud_cluster);
		cout << ss.str() << "Saved" << endl;
		j++;
	}


	/***
	 * But this function doesn't guarantee that different segments will have different
			* color(it all depends on RNG). Points that were not listed in the indices array will have red color.
	 */
	pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();
	//保存附加颜色的点云
//	pcl::io::savePCDFileASCII("colored_pointCloud.pcd",*colored_cloud);
	pcl::visualization::PCLVisualizer viewer("区域增长分割");
	//    viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,8,"coloredCloud");

	viewer.addPointCloud(colored_cloud);
	//	viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,8);
	while (!viewer.wasStopped())
	{
		viewer.spinOnce();
	}

	return (0);
}

PCL——点云分割(区域生长的分割)_第1张图片
效果:
PCL——点云分割(区域生长的分割)_第2张图片
PCL——点云分割(区域生长的分割)_第3张图片

PCL——点云分割(区域生长的分割)_第4张图片
曲率的计算
点云中曲率的计算

你可能感兴趣的:(pcl,聚类)