这次我们将展示一个简单的滤波的案例--把不符合的值去掉
代码
#include <iostream> #include <pcl/point_types.h> #include <pcl/filters/passthrough.h>intmain (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 datacloud->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 << "Cloud before filtering: " << 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;// Create the filtering objectpcl::PassThrough<pcl::PointXYZ> pass;pass.setInputCloud (cloud);pass.setFilterFieldName ("z");pass.setFilterLimits (0.0, 1.0);//pass.setFilterLimitsNegative (true);pass.filter (*cloud_filtered);std::cerr << "Cloud after filtering: " << 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到1之间的随机数组成点云,然后把0到1之间的点留下,别的点滤掉。
结果:
Cloud before filtering:0.352222 -0.151883 -0.106395-0.397406 -0.473106 0.292602-0.731898 0.667105 0.441304-0.734766 0.854581 -0.0361733-0.4607 -0.277468 -0.916762 Cloud after filtering:-0.397406 -0.473106 0.292602-0.731898 0.667105 0.441304