赞
踩
以下代码实现沿着指定的维度滤除指定范围以外或以内的点。
passthrough.cpp
#include <iostream> #include <pcl/point_types.h> #include <pcl/filters/passthrough.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>); // 5个点的点云,坐标值介于-1和1之间 //https://blog.csdn.net/qq_45182713/article/details/123760203 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); } std::cerr << "Cloud before filtering: " << std::endl; for (const auto& point: *cloud) std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl; // 创建PassThrough滤波器对象 pcl::PassThrough<pcl::PointXYZ> pass; pass.setInputCloud (cloud); pass.setFilterFieldName ("z"); // 指定滤波维度 pass.setFilterLimits (0.0, 1.0); // 指定滤波范围(0.0,1.0) pass.setNegative (true); // false: 滤除范围外的点(默认);true: 滤除范围内的点 pass.filter (*cloud_filtered); std::cerr << "Cloud after filtering: " << std::endl; for (const auto& point: *cloud_filtered) std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl; return (0); }
CMakeLists.txt
cmake_minimum_required(VERSION 3.5 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})
编译并运行:
./passthrough
(1)沿着x轴滤波: pass.setFilterFieldName (“x”);
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.352222 -0.151883 -0.106395
(2)沿着y轴滤波: pass.setFilterFieldName (“y”);
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.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
(3)沿着z轴滤波: pass.setFilterFieldName (“z”);
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
(4)滤除范围内的点: pass.setFilterFieldName (“z”); pass.setNegative (true);
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.352222 -0.151883 -0.106395
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。