直通滤波器代码passthrough.cpp
#include <iostream> #include <pcl/point_types.h> #include <pcl/filters/passthrough.h> int main (int argc, char** argv) { //定义并实例化一个PointCloud指针对象,并用随机点集赋值给它 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) { point.x = 1024 * rand () / (RAND_MAX + 1.0f); point.y = 1024 * rand () / (RAND_MAX + 1.0f); point.z = 1024 * rand () / (RAND_MAX + 1.0f); } //打印随机生成的点云数据 std::cerr << "Cloud before filtering: " << std::endl; for ((size_t i = 0; i < cloud->points.size (); ++i) std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl; //创建过滤对象 pcl::PassThrough<pcl::PointXYZ> pass; //设置滤波器对象 pass.setInputCloud (cloud); //设置输入点云 pass.setFilterFieldName ("z"); //设置过滤时所需要点云类型的z字段 pass.setFilterLimits (0.0, 1.0); //设置过滤字段上的范围,范围大小为(0.0 ~ 1.0) //pass.setFilterLimitsNegative (true); //设置保留范围内的还是过滤掉范围内的,true为保留 pass.filter (*cloud_filtered); //执行滤波,保存过滤结果在 cloud_filtered //打印出过滤结果相关数据 std::cerr << "Cloud after filtering: " << std::endl; for (size_t i = 0; i < cloud_filtered->points.size (); ++i) std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl; return (0); }
设置编译文件CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(passthrough) find_package(PCL 1.2 REQUIRED) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable (passthrough passthrough.cpp) target_link_libraries (passthrough ${PCL_LIBRARIES})
mkdir build cd build/ cmake .. make
执行程序
cd .. ./build/passthrough
结果如下:
从结果可以看出,滤波后的点云Z轴
上的数据在(0.0~1.0)
的数据范围内