多域名一个网站备案网站建设推广专家服务
文章目录
- 原理
- 使用过程
- 代码
- 实验
- 总结
原理
直通滤波器的作用是过滤在指定维度方向上取值不在给定值域内的点,即点云数据有xyz三维坐标,选择一个方向的维度的数据,设置一个范围,在这个范围中的点云会被保留,不在此范围内的点云会被去除掉
使用过程
- 指定一个维度以及该维度下的值域;
- 遍历点云中的每个点,判断该点指定维度上的取值是否在值域内,删除不在值域内的点;
- 遍历结束,留下的点即使构成滤波后的点云
代码
#include <pcl/io/pcd_io.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/cloud_viewer.h>
using namespace std;int main()
{//----------------------------------------- 加载点云 ----------------------------------------pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); //待滤波点云if (pcl::io::loadPCDFile("E:\\*************\\pc_09.pcd", *cloud) < 0){PCL_ERROR("点云文件不存在!\n");system("pause");return -1;}cout << "->加载点云个数:" << cloud->points.size() << endl;//==========================================================================================//----------------------------------------- 直通滤波 ----------------------------------------cout << "->正在进行直通滤波..." << endl;pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>); //滤波后点云pcl::PassThrough<pcl::PointXYZ> pt; // 创建滤波器对象pt.setInputCloud(cloud); //设置输入点云pt.setFilterFieldName("x"); //设置滤波所需字段xpt.setFilterLimits(-0.1, 0.5); //滤除在z轴方向上不在-0.1-1范围内的所有点pt.setFilterLimitsNegative(false); //默认false,保留范围内的点云;true,保存范围外的点云//pt.setKeepOrganized(true); //是否保持点云的组织结构(针对结构点云)pt.filter(*cloud_filtered); //执行滤波,并将滤波后点云保存到cloud_filtered中//去除 NaN 点(只针对结构点云。散乱点云不需要)//vector<int> Idx;//pcl::removeNaNFromPointCloud(*cloud_filtered, *cloud_filtered, Idx);//==========================================================================================//-------------------------------------- 可视化(可选) -------------------------------------pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("滤波前后对比"));/*-----原始点云-----*/int v1(0);viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); //设置第一个视口在X轴、Y轴的最小值、最大值,取值在0-1之间viewer->setBackgroundColor(1, 1, 11, v1); //设置背景颜色,0-1,默认黑色(0,0,0)viewer->addText("befor_filtered", 10, 10, "v1_text", v1);viewer->addPointCloud<pcl::PointXYZ>(cloud, "befor_filtered_cloud", v1);viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "befor_filtered_cloud", v1);viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "befor_filtered_cloud", v1);// 添加坐标轴viewer->addCoordinateSystem(1.0);/*-----滤波后点云-----*/int v2(0);viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);viewer->setBackgroundColor(1, 1, 1, v2);viewer->addText("after_filtered", 10, 10, "v2_text", v2);viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "after_filtered_cloud", v2);viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "after_filtered_cloud", v2);viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "after_filtered_cloud", v2);viewer->addCoordinateSystem(1.0);while (!viewer->wasStopped()){viewer->spinOnce(100);//boost::this_thread::sleep(boost::posix_time::microseconds(100000));std::this_thread::sleep_for(std::chrono::microseconds(100000));}return 0;
}
实验
次数据采用直通滤波消除了在点云x轴方向上 -0.1-0.4范围外的点
总结
优点:
- 直通滤波器简单高效,使用于消除背景操作;
缺点:
- 不适用于复杂场景,在某些复杂的场景中,直通滤波器可能无法有效地过滤出所需的点云,因为它仅依赖于单个维度的范围;
- 不考虑上下文:直通滤波器不考虑点的上下文或周围点的信息,因此可能无法应对一些需要全局上下文考虑的应用场景