<div style="font-size:16px;">
<h1 class="pgc-h-arrow-right">实验目的</h1>
<p>通过PCL处理点云数据,从点云数据中提取出待装货货车的点云数据并将其可视化。</p>
<h1 class="pgc-h-arrow-right">所处理点云的原始可视化图像及最终效果图</h1>
<p>原始图:</p>
<div class="pgc-img">
<img alt="056808c64be355fbd5e9f6ba09eae394.png" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-e18fce07ca54d38d0bdd513b997c2234.png">
<p class="pgc-img-caption">原始图</p>
</div>
<p>处理后:</p>
<div class="pgc-img">
<img alt="bf4d112f15724a4b0562e26a816a3435.png" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-d5faead3056a500ae4e2b92c317449a9.png">
<p class="pgc-img-caption">处理后</p>
</div>
<h1 class="pgc-h-arrow-right">处理过程概述</h1>
<ol start="1"><li>首先由于点云数据中点的数量很大,做一些处理时耗时较多,所以第一步是使用体素滤波,实现下采样,即在保留点云原有形状的基础上减少点的数量 减少点云数据,以提高后面对点云处理的速度。</li><li>通过随机采样一致性(前面多出用到)分割地面,将地面从点云中分离出来,这时候地面上的物体就悬空了。</li><li>去除地面后地面上物体都悬空了,这时候就使用统计滤波剔除掉离散点。</li><li>物体悬空后就更好分离了,现在就构建Kdtree通过欧几里得聚类提取获取路面上不同物体,然后车辆就提取了出来。</li></ol>
<h1 class="pgc-h-arrow-right">实验内容</h1>
<ol start="1"><li><strong>上面的基本思路已经写出来了,下面就直接上代码。</strong></li></ol>
<pre class="blockcode"><code>#include #include #include #include #include #include #include #include //随机参数估计方法头文件#include //模型定义头文件#include //基于采样一致性分割的类的头文件#include #include #include #include#includeusing namespace std;Int main (int argc, char** argv){//点云对象pcl::PointCloud<:pointxyz>::Ptr cloud(new pcl::PointCloud<:pointxyz>);//处理后点云对象pcl::PointCloud<:pointxyz>::Ptr cloud_handle(new pcl::PointCloud<:pointxyz>);//从点云文件中读取点云数据pcl::io::loadPCDFile("d:/2.pcd", *cloud);cloud_handle = cloud;/**先使用体素滤波向下采样处理点云* @brief sor*/pcl::VoxelGrid<:pointxyz> sor; //创建滤波对象sor.setInputCloud (cloud); //设置需要过滤的点云给滤波对象sor.setLeafSize (0.5, 0.5, 0.5); //设置滤波时创建的体素体积,单位m,因为保留点云大体形状的方式是保留每个体素的中心点,所以体素体积越大那么过滤掉的点云就越多sor.filter (*cloud); //执行滤波处理/**平面分割,分割出地面* 创建分割时所需要的模型系数对象,coefficients及存储内点的点索引集合对象inliers* @brief coefficients*/pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);pcl::PointIndices::Ptr inliers (new pcl::PointIndices);pcl::SACSegmentation<:pointxyz> seg; // 创建分割对象seg.setOptimizeCoefficients (true); // 可选择配置,设置模型系数需要优化// 必要的配置,设置分割的模型类型,所用的随机参数估计方法,距离阀值,输入点云seg.setModelType (pcl::SACMODEL_PLANE); //设置模型类型seg.setMethodType (pcl::SAC_RANSAC); //设置随机采样一致性方法类型seg.setDistanceThreshold (1); //设定距离阀值,距离阀值决定了点被认为是局内点是必须满足的条件//表示点到估计模型的距离最大值seg.setInputCloud (cloud);//引发分割实现,存储分割结果到点几何inliers及存储平面模型的系数coefficientsseg.segment (*inliers, *coefficients);//创建点云提取对象,将处理后的点云提取出来pcl::ExtractIndices<:pointxyz> extract;extract.setInputCloud (cloud);extract.setIndices (inliers);extract.setNegative (true);extract.filter (*cloud);/**统计滤波去除离散点* @brief sta*/pcl::StatisticalOutlierRemoval<:pointxyz> sta; //创建滤波器对象sta.setInputCloud (cloud); //设置待滤波的点云sta.setMeanK (2000); //设置在进行统计时考虑查询点临近点数sta.setStddevMulThresh (0.05); //设置判断是否为离群点的阀值,根据原理来说,阈值越小过滤掉的点就越多sta.filter (*cloud); //存储// 建立kd-tree对象用来搜索pcl::search::KdTree<:pointxyz>::Ptr kdtree(new pcl::search::KdTree<:pointxyz>);kdtree->setInputCloud(cloud);// Euclidean 聚类对象.pcl::EuclideanClusterExtraction<:pointxyz> clustering;// 设置聚类的最小值 4mclustering.setClusterTolerance(4);// 设置聚类的小点数和最大点云数clustering.setMinClusterSize(5000);//clustering.setMaxClusterSize(25000);//设置搜索方式clustering.setSearchMethod(kdtree);clustering.setInputCloud(cloud);std::vector<:pointindices> clusters;clustering.extract(clusters);//处理后保存的聚类是已降序排序的,所以第一个聚类就是我们想要的聚类long num = 0;int j = 0;for (std::vector<:pointindices>::const_iterator i = clusters.begin(); i != clusters.end |
|