PCL-直通滤波

本篇内容:

  • 讲解直通滤波的作用
  • 通过pcl实现直通滤波
    效果:
    在这里插入图片描述

1 主要原理

点云数据通常包含x、y、z三个维度的数据,用户指定维度、范围后,直通滤波过滤或保留该范围内的所有点云
假设我指定维度’y’,范围(0.0,0.1),运行直通滤波后,则过滤或保留y坐标为(0.0,0.1)范围内的所有点云

2 直通滤波主要流程

初始化直通滤波器:

pcl::PassThrough<PointType> pt_filter;

设置输入点云:

pt_filter.setInputCloud(cloud);

设置过滤维度:

pt_filter.setFilterFieldName("y");

设置过滤范围:

pt_filter.setFilterLimits(0.0, 0.1);

设置过滤或保留:

pt_filter.setNegative(true);	// 设置为true,表示保留在过滤范围外的点,为false,表示保留在过滤范围内的点

执行过滤:

pt_filter.filter(*cloud_filter);

3 完整代码

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

// 前置点云类型,方便以后更改
using PointType = pcl::PointXYZ;
using PointCloud = pcl::PointCloud<PointType>;
using PointCloud_Ptr = PointCloud::Ptr;

int main(int argc, char **argv) {
  if (argc < 3) {
    std::cout<<"Usage: ./read_pcd <pcd_file_path> <pcd_save_path>\n";
    return -1;
  }

  std::string pcd_file_path(argv[1]);
  std::string pcd_save_path(argv[2]);

  // 声明变量,用于保存点云数据
  PointCloud_Ptr cloud(new PointCloud);
  PointCloud_Ptr cloud_filter(new PointCloud);

  // 读取pcd点云文件
  if (pcl::io::loadPCDFile<PointType>(pcd_file_path, *cloud) == -1) {
    std::cerr<<"check pcd path\n";
    return -1;
  }

  // 初始化过滤器
  pcl::PassThrough<PointType> pt_filter;
  // 设置输入点云
  pt_filter.setInputCloud(cloud);
  // 设置过滤维度
  pt_filter.setFilterFieldName("y");
  // 设置过滤范围
  pt_filter.setFilterLimits(0.0, 0.1);

  // 设置为true,表示保留在过滤范围外的点,为false,表示保留在过滤范围内的点
  pt_filter.setNegative(true);
  // 执行过滤
  pt_filter.filter(*cloud_filter);

  // 保存点云文件
  pcl::io::savePCDFileASCII(pcd_save_path, *cloud_filter);

  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值