系统:ubuntu
语言:C++
代码解释如下:
#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)
{
DWORD t1, t2;
t1 = GetTickCount();//以上两句和最后return 0 之上的为计时函数。
//点云的类型
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//打开点云
if (pcl::io::loadPCDFile <pcl::PointXYZ>("resultFloor局部.pcd", *cloud) == -1)//改成想要输入的点云名称...*cloud就是把输入的点云记录到变量指针cloud中。
{
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);
//直通滤波在Z轴的0到1米之间
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寄存器(区域曾长的对象)
reg.setMinClusterSize(50); //最小的聚类的点数(小于这参数的平面被忽略不计)
reg.setMaxClusterSize(1000000); //最大的(一般随便设置)
reg.setSearchMethod(tree); //搜索方式(采用的默认是K—d树法)
reg.setNumberOfNeighbours(30); //设置搜索的邻域点的个数,周围多少个点决定这是一个平面(决定容错率,设置大时有倾斜也可接受,设置小时检测到的平面会很小)
reg.setInputCloud(cloud); //输入点
//reg.setIndices (indices);
reg.setInputNormals(normals); //输入的法线
reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI); //设置平滑度(设置两个法线在多大夹角内可当做是共面的)
reg.setCurvatureThreshold(1.0); //设置曲率的阈值
//最后也是一个弯曲的阈值,这个决定了比当前考察的点和平均的法线角度,决定是否还有继续探索下去的必要。
//(也就是假设每个点都是平稳弯曲的,那么normal的夹角都很小,但是时间长了偏移的就大了,这个参数就是限制这个用的)
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
//把结果输出到一个簇里面,这个簇会自动把每个平面分成一个vector,可以打印下来看看
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())
//{
//}
pcl::PCDWriter writer;//将点云写入磁盘
writer.write("resultFloor局部_Cluster_viewer.pcd", *colored_cloud, false);//改成想要输出的点云名称
t2 = GetTickCount(); //从这句到return 0之间的两句为计时函数
printf("Use Time:%f\n", (t2 - t1)*1.0 / 1000);
return (0);
}
输出结果: