PCL——点云分割(区域生长的分割)
发布时间
阅读量:
阅读量
点云分割是基于空间、几何和纹理等特征对点云进行划分,在同一划分区域内的点云具有相似的特征。
基于区域生长的分割
种子点 :根据点云中各点所具有的曲率值对点云进行排序,在所有曲率值中最小的那个叫做初始种子点,
(1)区域生长算法从曲率最小的初始种子点开始生长,在该区域内具有最平滑的部分特性时可有效减少分割片段的数量从而提高算法效率。
(2)初始化阶段需创建一个空聚类区域C以及空的种子点序列Q同时还需要初始化聚类数组L。
(3)选择合适的初始种子并将其加入Q中随后搜索其邻域范围内的所有相邻节点计算这些邻域节点法线与当前种子节点法线之间的夹角当夹角小于预设光滑阈限时将这些邻域节点加入到C区域内同时若这些邻域节点所对应的曲率值低于预先设定好的曲率阈值则也需将其加入到Q序列中完成所有邻域节点判断后删除当前种子节点在Q序列中重新选取新的候选性子节点重复上述操作直到Q序列为空此时完成一个区域生长过程并将该结果加入到聚类数组L中。
(4)通过按曲率从小到大的顺序对所有输入原始数据中的部分关键性子节点依次作为候选性子节点加入到Q序列当中然后重复上述生成过程直至所有可能的有效子结构均被纳入最终分类结果之中
#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/pcl_visualizer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <stdio.h>
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);
}
AI写代码cpp


效果:



曲率的计算
点云中曲率的计算
全部评论 (0)
还没有任何评论哟~
