1. 直通滤波

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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>);

// 生成数据
cloud->width = 5;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);

for (size_t i = 0; i < cloud->points.size(); ++i){
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}

// 打印滤波前数据
std::cerr << "PointCloud before filtering has: " << std::endl;
for (size_t i = 0; i < cloud->points.size(); ++i){
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << std::endl;
}

// -------------------------------------------------------
// 创建滤波器对象,对z轴设置范围0.0~1.0之间,不在该范围内的点过滤
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*cloud_filtered);

// 打印滤波后数据
std::cerr << "PointCloud after filtering has: " << std::endl;
for (size_t i = 0; i < cloud_filtered->points.size(); ++i){
std::cerr << " " << cloud_filtered->points[i].x << " "
<< cloud_filtered->points[i].y << " "
<< cloud_filtered->points[i].z << std::endl;
}

return 0;
}

可以对坐标轴进行过滤,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Fill in the cloud data
pcl::PCDReader reader;
reader.read("16line.pcd", *cloud);

std::cerr << "Cloud before filtering: " << cloud->points.size() << std::endl;

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered2(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered3(new pcl::PointCloud<pcl::PointXYZ>);

// Create the filtering object
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("x");
pass.setFilterLimits(-5.0, 5.0);
// pass.setFilterLimitsNegative(true);
pass.filter(*cloud_filtered2);
// filter range Y-axis
pass.setInputCloud(cloud_filtered2);
pass.setFilterFieldName("y");
pass.setFilterLimits(-5.0, 5.0);
pass.filter(*cloud_filtered3);
// filter range Z-axis
pass.setInputCloud(cloud_filtered3);
pass.setFilterFieldName("z");
pass.setFilterLimits(-0.5, 3.0);
pass.filter(*cloud_filtered);

2. VoxelGrid体素网格法下采样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>


int main(){
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);

// 点云对象读取
pcl::PCDReader reader;
reader.read("../table_scene_lms400.pcd", *cloud);

std::cerr << "PointCloud before filtering has: " << cloud->width * cloud->height
<< " data points.(" <<pcl::getFieldsList(*cloud) << ")." << std::endl;

// 创建体素网格滤波器对象
pcl::VoxelGrid<pcl::PointXYZ> vg;
vg.setInputCloud(cloud); // 设置输入点云
vg.setLeafSize(0.01f, 0.01f, 0.01f); // 设置体素网格大小
vg.filter(*cloud_filtered); // 执行滤波操作

// 输出滤波后的点云信息
std::cerr << "PointCloud after filtering has: " << cloud_filtered->width * cloud_filtered->height
<< " data points.(" << pcl::getFieldsList(*cloud_filtered) << ")." << std::endl;

return 0;
}