PCL 点云直通滤波

目录

一、概述

1.1原理

1.2实现步骤

1.3应用场景

二、代码实现

2.1关键函数

2.1.1 直通滤波实现

2.1.2 可视化函数

2.2完整代码

三、实现效果


PCL点云算法汇总及实战案例汇总的目录地址链接:

PCL点云算法与项目实战案例汇总(长期更新)


一、概述

        直通滤波(PassThrough Filter) 是一种常用的点云滤波方法,用于在特定范围内选择和保留点云数据中的点。通过指定要保留的点的坐标范围,可以有效地去除不必要的点云数据,从而简化点云并提高后续处理的效率。

1.1原理

        直通滤波的原理是根据用户定义的坐标轴(如 X、Y 或 Z)范围,过滤掉不在该范围内的点。此方法适用于对点云进行区域选择,以便专注于特定区域或特征。

1.2实现步骤

  1. 读取点云数据:使用 PCL 的 I/O 函数读取点云文件。
  2. 设置直通滤波器参数:包括要过滤的坐标轴和范围。
  3. 应用滤波器:生成过滤后的点云。
  4. 可视化结果:将原始点云和过滤后的点云进行可视化,对比效果。原始点云为红色,过滤后的点云为绿色。

1.3应用场景

  1. 区域选择:在进行特征提取或对象识别时,仅保留感兴趣区域的点。
  2. 噪声去除:通过过滤掉不在指定范围内的点,去除可能的噪声。
  3. 数据简化:降低点云数据的复杂性,以提高计算效率。

二、代码实现

2.1关键函数

2.1.1 直通滤波实现

        通过设置坐标轴和范围,应用直通滤波器。

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>

// 直通滤波函数
pcl::PointCloud<pcl::PointXYZ>::Ptr passThroughFilter(
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,  // 输入点云
    const std::string& axis,                    // 要过滤的坐标轴
    float min_limit,                            // 最小范围
    float max_limit                             // 最大范围
)
{
    pcl::PassThrough<pcl::PointXYZ> pass;  // 创建直通滤波器对象
    pass.setInputCloud(cloud);             // 设置输入点云
    pass.setFilterFieldName(axis);         // 设置要过滤的坐标轴("x"、"y" 或 "z")
    pass.setFilterLimits(min_limit, max_limit);  // 设置过滤范围
    pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pass.filter(*filtered_cloud);          // 应用滤波器
    return filtered_cloud;                 // 返回过滤后的点云
}

2.1.2 可视化函数

        使用 PCL 可视化库展示原始点云和过滤后的点云,并设置颜色:原始点云为红色,过滤后的点云为绿色。

#include <pcl/visualization/pcl_visualizer.h>

// 可视化原始点云和过滤后的点云
void visualizePointClouds(
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,          // 原始点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud  // 过滤后的点云
)
{
    // 创建可视化器
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("PassThrough Filter Viewer"));

    // 设置视口1,显示原始点云
    int vp_1;
    viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1);          // 创建左侧窗口
    viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_1);           // 设置白色背景
    viewer->addText("Raw Point Clouds", 10, 10, "v1_text", vp_1);  // 添加标题

    // 设置原始点云的颜色为红色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler(cloud, 255, 0, 0);  // 红色
    viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color_handler, "original_cloud", vp_1);  // 添加原始点云

    // 设置视口2,显示过滤后的点云
    int vp_2;
    viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2);          // 创建右侧窗口
    viewer->setBackgroundColor(0.98, 0.98, 0.98, vp_2);        // 设置浅灰色背景
    viewer->addText("Filtered Point Clouds", 10, 10, "v2_text", vp_2);  // 添加标题

    // 设置过滤后的点云的颜色为绿色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> filtered_cloud_color_handler(filtered_cloud, 0, 255, 0);  // 绿色
    viewer->addPointCloud<pcl::PointXYZ>(filtered_cloud, filtered_cloud_color_handler, "filtered_cloud", vp_2);  // 添加过滤后的点云

    // 设置点的大小(可选)
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud", vp_1);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "filtered_cloud", vp_2);

    // 启动可视化循环
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);  // 刷新可视化器
    }
}

2.2完整代码

// C++头文件
#include <iostream>
// PCL头文件
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>      // 直通滤波
#include <pcl/visualization/pcl_visualizer.h>

// 直通滤波函数
pcl::PointCloud<pcl::PointXYZ>::Ptr passThroughFilter(
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,  // 输入点云
    const std::string& axis,                    // 要过滤的坐标轴
    float min_limit,                            // 最小范围
    float max_limit                             // 最大范围
)
{
    pcl::PassThrough<pcl::PointXYZ> pass;  // 创建直通滤波器对象
    pass.setInputCloud(cloud);             // 设置输入点云
    pass.setFilterFieldName(axis);         // 设置要过滤的坐标轴("x"、"y" 或 "z")
    pass.setFilterLimits(min_limit, max_limit);  // 设置过滤范围
    pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pass.filter(*filtered_cloud);          // 应用滤波器
    return filtered_cloud;                 // 返回过滤后的点云
}

// 可视化原始点云和过滤后的点云
void visualizePointClouds(
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,          // 原始点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud  // 过滤后的点云
)
{
    // 创建可视化器
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("PassThrough Filter Viewer"));

    // 设置视口1,显示原始点云
    int vp_1;
    viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1);          // 创建左侧窗口
    viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_1);           // 设置白色背景
    viewer->addText("Raw Point Clouds", 10, 10, "v1_text", vp_1);  // 添加标题

    // 设置原始点云的颜色为红色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler(cloud, 255, 0, 0);  // 红色
    viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color_handler, "original_cloud", vp_1);  // 添加原始点云

    // 设置视口2,显示过滤后的点云
    int vp_2;
    viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2);          // 创建右侧窗口
    viewer->setBackgroundColor(0.98, 0.98, 0.98, vp_2);        // 设置浅灰色背景
    viewer->addText("Filtered Point Clouds", 10, 10, "v2_text", vp_2);  // 添加标题

    // 设置过滤后的点云的颜色为绿色
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> filtered_cloud_color_handler(filtered_cloud, 0, 255, 0);  // 绿色
    viewer->addPointCloud<pcl::PointXYZ>(filtered_cloud, filtered_cloud_color_handler, "filtered_cloud", vp_2);  // 添加过滤后的点云

    // 设置点的大小(可选)
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud", vp_1);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "filtered_cloud", vp_2);

    // 启动可视化循环
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);  // 刷新可视化器
    }
}

int main(int argc, char** argv)
{
    // ------------------------------读取点云数据---------------------------------
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPCDFile("bunny.pcd", *cloud) < 0)
    {
        PCL_ERROR("Could not read file\n");
        return (-1);  // 返回错误
    }

    // -------------------------------直通滤波---------------------------------
    float min_limit = -0.1;  // 设置过滤范围下限
    float max_limit =0.1;   // 设置过滤范围上限
    std::string axis = "y";  // 设置要过滤的坐标轴("x"、"y" 或 "z")
    pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud = passThroughFilter(cloud, axis, min_limit, max_limit);  // 进行直通滤波

    // ------------------------------可视化原始点云和过滤后的点云---------------------------------
    visualizePointClouds(cloud, filtered_cloud);  // 调用可视化函数

    return 0;
}

三、实现效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值