前言
使用pcl点云库,对点云进行处理,提取平面,最后通过法向量估计平面的外轮廓。
一、数据来源?
3D线激光传感器采集的点云数据。
二、提取步骤
1.读取点云数据
代码如下(示例):
#pragma warning (disable:4996)
#include <pcl/io/pcd_io.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/features/normal_3d.h>
#include <pcl/point_cloud.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/features/boundary.h>
#include <vector>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;
reader.read("cloud_out_ascii1.pcd", *cloud);
2.提取平面(通过点云高度筛选)
代码如下(示例):
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(1.4, 10.0);
pass.setFilterLimitsNegative(false);
pass.filter(*cloud_plane);
3.计算平面法向量
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normEst;
//pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
pcl::PointCloud < pcl::Normal>::Ptr normals_plane(new pcl::PointCloud<pcl::Normal>);
normEst.setInputCloud(cloud_plane);
normEst.setSearchMethod(tree);
normEst.setKSearch(70);
normEst.compute(*normals_plane);
4.计算轮廓
pcl::PointCloud<pcl::Boundary> boundaries;
pcl::BoundaryEstimation<pcl::PointXYZ, pcl::Normal, pcl::Boundary> est;
//pcl::PointCloud < pcl::Normal>::Ptr normals_plane(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_plane(new pcl::search::KdTree<pcl::PointXYZ>);
est.setInputCloud(cloud_plane);
est.setInputNormals(normals_plane);
est.setSearchMethod(tree_plane);
est.setKSearch(40); //一般这里的数值越高,最终边界识别的精度越好
est.compute(boundaries);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_boundary(new pcl::PointCloud<pcl::PointXYZ>);
for (int i = 0; i < cloud_plane->size(); i++)
{
if (boundaries[i].boundary_point > 0)
{
cloud_boundary->points.push_back(cloud_plane->points[i]);
}
}
5. 轮廓可视化
//可视化
boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("边界提取"));
int v1(0);
MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
MView->setBackgroundColor(0.3, 0.3, 0.3, v1);
MView->addText("Raw point clouds", 10, 10, "v1_text", v1);
int v2(0);
MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
MView->setBackgroundColor(0.5, 0.5, 0.5, v2);
MView->addText("Boudary point clouds", 80, 80, "v2_text", v2);//将80,80改为10,10结果如右图
MView->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud", v1);
MView->addPointCloud<pcl::PointXYZ>(cloud_boundary, "cloud_boundary", v2);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "sample cloud", v1);
MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "cloud_boundary", v2);
MView->addCoordinateSystem(1.0);
MView->initCameraParameters();
MView->spin();
该处使用的url网络请求的数据。
总结
通过这种方法提取轮廓速度慢。后续需要加速处理。