- 这是一个demo,用来解释直通滤波
- 首先加载必要的头文件
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h> //直通滤波
#include <pcl/visualization/pcl_visualizer.h>
- 实例化点云对象
//定义输入和输出点云
//输入的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_in_ptr(new pcl::PointCloud<pcl::PointXYZ>);
//滤波后的点云
pcl::PointCloud<PointT>::Ptr cloud_filtered(new pcl::PointCloud<PointT>);
- 随机生成一个点云
//创建随机点云
point_cloud_in_ptr->width = 100000;
point_cloud_in_ptr->height = 1;
point_cloud_in_ptr->is_dense = false;
point_cloud_in_ptr->resize(point_cloud_in_ptr->width * point_cloud_in_ptr->height);
float a = -1,b = 1;
for (size_t i = 0; i < point_cloud_in_ptr->points.size(); ++i)
{
point_cloud_in_ptr->points[i].x = rand() * 1.0 / RAND_MAX * (b - a) + a;
point_cloud_in_ptr->points[i].y = rand() * 1.0 / RAND_MAX * (b - a) + a;
point_cloud_in_ptr->points[i].z = rand() * 1.0 / RAND_MAX * (b - a) + a;
}
- 这个点云是在区间 x y z 在[-1 ,1]的正方形无序点云
- 将 Z 轴(蓝色) 在 [-0.5 ,0.5] 区间内的数据过滤
//直通滤波对象
pcl::PassThrough<PointT> pass;
//设置直通滤波
pass.setInputCloud(point_cloud_in_ptr);
//设置处理字段是z轴
pass.setFilterFieldName("z");
//处理范围区间
pass.setFilterLimits(-0.5, 0.5);
//设置是过滤区间内还是区间外 true = 内 false = 外
pass.setFilterLimitsNegative(true);
pass.filter(*cloud_filtered);
- 全部代码
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/pcl_visualizer.h>
/*
* 直通滤波测试
*/
typedef pcl::PointXYZ PointT;
int main() {
//定义输入和输出点云
//输入的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_in_ptr(new pcl::PointCloud<pcl::PointXYZ>);
//滤波后的点云
pcl::PointCloud<PointT>::Ptr cloud_filtered(new pcl::PointCloud<PointT>);
//创建随机点云
point_cloud_in_ptr->width = 100000;
point_cloud_in_ptr->height = 1;
point_cloud_in_ptr->is_dense = false;
point_cloud_in_ptr->resize(point_cloud_in_ptr->width * point_cloud_in_ptr->height);
float a = -1,b = 1;
for (size_t i = 0; i < point_cloud_in_ptr->points.size(); ++i)
{
point_cloud_in_ptr->points[i].x = rand() * 1.0 / RAND_MAX * (b - a) + a;
point_cloud_in_ptr->points[i].y = rand() * 1.0 / RAND_MAX * (b - a) + a;
point_cloud_in_ptr->points[i].z = rand() * 1.0 / RAND_MAX * (b - a) + a;
}
//直通滤波对象
pcl::PassThrough<PointT> pass;
//设置直通滤波
pass.setInputCloud(point_cloud_in_ptr);
//设置处理字段是z轴
pass.setFilterFieldName("z");
//处理范围区间
pass.setFilterLimits(-0.5, 0.5);
//设置是过滤区间内还是区间外
pass.setFilterLimitsNegative(true);
pass.filter(*cloud_filtered);
std::cerr << "原始点云经过直通滤波处理后的大小 = " << cloud_filtered->size() << std::endl;
pcl::visualization::PCLVisualizer viewer("v1");
viewer.addPointCloud(cloud_filtered);
viewer.addCoordinateSystem();
viewer.spin();
return 0;
}