您的位置:首页 > 科技 > 能源 > 直通滤波-使用 PassThrough 过滤器过滤 PointCloud

直通滤波-使用 PassThrough 过滤器过滤 PointCloud

2024/10/6 12:26:01 来源:https://blog.csdn.net/m0_71087087/article/details/141949885  浏览:    关键词:直通滤波-使用 PassThrough 过滤器过滤 PointCloud

代码:创建一个文件,比如说,在你的收藏夹中 编辑器,并将以下内容放入其中:passthrough.cpp

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>intmain ()
{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 (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;// Create the filtering objectpcl::PassThrough<pcl::PointXYZ> pass;pass.setInputCloud (cloud);pass.setFilterFieldName ("z");pass.setFilterLimits (0.0, 1.0);//pass.setNegative (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);
}

解释

现在,让我们逐个分解代码。

在以下几行中,我们定义 Point Clouds 结构,填写 input cloud,并将其内容显示到屏幕。

 // Fill in the cloud datacloud->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 过滤器对象,并设置其参数。这 筛选条件字段名称设置为 Z 坐标,并且接受的间隔值 设置为 (0.0;1.0).

  pcl::PassThrough<pcl::PointXYZ> pass;pass.setInputCloud (cloud);pass.setFilterFieldName ("z");pass.setFilterLimits (0.0, 1.0);//pass.setNegative (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;

 编译和运行程序:

将以下行添加到您的 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

您将看到类似于以下内容的内容:

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

过滤过程的图形显示如下所示。

请注意,坐标轴表示为红色 (x)、绿色 (y) 和蓝色 (z). 这五个点用绿色表示,作为 filtering 和 red 作为过滤器已删除的点。

作为练习,请尝试取消注释此行

  //pass.setNegative (true);

并再次运行该程序。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com