直通滤波器,是直接根据滤波器设定的条件,选择自己所需点云。可以选择保留设定范围内的点云,也可以选择滤除设定范围内的点云
,保留或者滤出是由setFilterLimitsNegative进行模式开关的。
代码中,设定z轴的条件,保留z方向范围[0,1]内的点。
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
int main(int argc, char **argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
// Fill in the cloud data
cloud->width = 5;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
for (auto &point : *cloud) //填充点云
{
point.x = 1024 * rand() / (RAND_MAX + 1.0f);
point.y = 1024 * rand() / (RAND_MAX + 1.0f);
point.z = 1024 * rand() / (RAND_MAX + 1.0f);
}
cloud->points[0].z = 1;//为了测试边界条件
cloud->points[1].z = 0;
std::cerr << "Cloud before filtering: " << std::endl;
for (const auto &point : *cloud)
std::cerr << " " << point.x << " "
<< point.y << " "
<< point.z << std::endl;
// Create the filtering object
//设置滤波器对象
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud); //输入点云
pass.setFilterFieldName("z"); //滤波字段
//这里只保留 0.0 < z < 1.0的点云
pass.setFilterLimits(0.0, 1.0); //设置过滤字段的范围
//setFilterLimitsNegative默认设置为false。如果设置true,则表示setFilterLimits范围内的点滤掉
//pass.setFilterLimitsNegative (true);
pass.filter(*cloud_filtered); //执行过滤,并输出到cloud_filtered,但是输入的cloud不会变化
std::cerr << "Cloud before filtering:cloud " << std::endl;
for (const auto &point : *cloud)
std::cerr << " " << point.x << " "
<< point.y << " "
<< point.z << std::endl;
std::cerr << "Cloud after filtering: cloud_filtered" << std::endl;
for (const auto &point : *cloud_filtered)
std::cerr << " " << point.x << " "
<< point.y << " "
<< point.z << std::endl;
return (0);
}
输出结果如下,可以看到只保留了z值在[0,1]的范围。
Cloud before filtering:
0.352222 -0.151883 1
-0.397406 -0.473106 0
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
Cloud before filtering:cloud
0.352222 -0.151883 1
-0.397406 -0.473106 0
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
Cloud after filtering: cloud_filtered
0.352222 -0.151883 1
-0.397406 -0.473106 0
-0.731898 0.667105 0.441304
参考:https://pcl.readthedocs.io/projects/tutorials/en/latest/passthrough.html#passthrough