核心函数:
//直通passthrough滤波
pcl::PassThrough<pcl::PointXYZ> pass; //实例化直通滤波器
pass.setInputCloud(cloud_in); //设置输入点云
pass.setFilterFieldName("z");//设置滤波的field
pass.setFilterLimits(0,1);//上述field的滤波范围
pass.setFilterLimitsNegative(true);//设置要保存哪一部分点云,默认为false(field范围内部的点云)
pass.filter(*cloud_filtered);//输出点云到*cloud_filtered
完整代码:
#include <iostream>
#include <ctime>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/passthrough.h>//直通滤波器文件
#include <pcl/visualization/pcl_visualizer.h>
int main() {
std::cout << "Hello, World!" << std::endl;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile("room_scan1.pcd",*cloud_in);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
cloud_filtered->width = cloud_in->width;
cloud_filtered->height = cloud_in->height;
cloud_filtered->is_dense = false;
cloud_filtered->resize(cloud_filtered->height*cloud_filtered->width);
//直通passthrough滤波
pcl::PassThrough<pcl::PointXYZ> pass; //实例化直通滤波器
pass.setInputCloud(cloud_in); //设置输入点云
pass.setFilterFieldName("z");//设置滤波的field
pass.setFilterLimits(0,1);//上述field的滤波范围
pass.setFilterLimitsNegative(true);//设置要保存哪一部分点云,默认为false(field范围内部的点云)
pass.filter(*cloud_filtered);//输出点云到*cloud_filtered
//save cloud_filtered.pcd
pcl::io::savePCDFileASCII("cloud_filtered.pcd",*cloud_filtered);
std::cout<<"save succeed"<<std::endl;
//visualizer
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3d viewer"));
viewer->initCameraParameters();
int v1(0);
viewer->createViewPort(0,0,0.5,1,v1);
viewer->setBackgroundColor(0,128,255,v1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud_filtered,255,0,255);
viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered,single_color,"cloud_filtered",v1);
int v2(0);
viewer->createViewPort(0.5,0,1,1,v2);
viewer->setBackgroundColor(0,0,0,v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color1(cloud_in,255,255,255);
viewer->addPointCloud<pcl::PointXYZ>(cloud_in,single_color1,"cloud_in",v2);//PCL_Visualizer类大多数函数会有一个view_port,这个一定要加上,否则会出现点云不按照自己的目的显示的问题
viewer->addCoordinateSystem(1);
viewer->spin();
return 0;
}
可视化:依次为直通滤波之后的点云、原点云。