PCL系列7——半径滤波(离群点剔除)

本文介绍了PCL库中的点云半径滤波方法,用于去除点云数据中的噪声和离群点。通过检查点云中每个点在设定半径内的邻接点数量,实现对异常点的有效剔除。文章详细解析了滤波原理、源码,并提供了示例代码及运行结果。
摘要由CSDN通过智能技术生成
1.原理介绍

点云半径滤波也叫基于连通分析的点云滤波,该方法的基本思想是假定原始点云中每个激光点在指定的半径邻域中至少包含一定数量的近邻点。原始点云中符合假设条件的激光点被视为正常点进行保留,反之,则视为噪声点并进行去除。该方法对原始激光点云中存在的一些悬空的孤立点或无效点具有很好的去除效果。

2.源码剖析
template <typename PointT> void
pcl::RadiusOutlierRemoval<PointT>::applyFilterIndices (std::vector<int> &indices)
{
  if (search_radius_ == 0.0)
  {
    PCL_ERROR ("[pcl::%s::applyFilter] No radius defined!\n", getClassName ().c_str ());
    indices.clear ();
    removed_indices_->clear ();
    return;
  }

  // Initialize the search class
  if (!searcher_)
  {
    if (input_->isOrganized ())
      searcher_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
    else
      searcher_.reset (new pcl::search::KdTree<PointT> (false));
  }
  searcher_->setInputCloud (input_);

  // The arrays to be used
  std::vector<int> nn_indices (indices_->size ());
  std::vector<float> nn_dists (indices_->size ());
  indices.resize (indices_->size ());
  removed_indices_->resize (indices_->size ());
  int oii = 0, rii = 0;  // oii = output indices iterator, rii = removed indices iterator

  // If the data is dense => use nearest-k search
  if (input_->is_dense)
  {
    // Note: k includes the query point, so is always at least 1
    int mean_k = min_pts_radius_ + 1;
    double nn_dists_max = search_radius_ * search_radius_;

    for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
    {
      // Perform the nearest-k search
      int k = searcher_->nearestKSearch (*it, mean_k, nn_indices, nn_dists);

      // Check the number of neighbors
      // Note: nn_dists is sorted, so check the last item
      bool chk_neighbors = true;
      if (k == mean_k)
      {
        if (negative_)
        {
          chk_neighbors = false;
          if (nn_dists_max < nn_dists[k-1])
          {
            chk_neighbors = true;
          }
        }
        else
        {
          chk_neighbors = true;
          if (nn_dists_max < nn_dists[k-1])
          {
            chk_neighbors = false;
          }
        }
      }
      else
      {
        if (negative_)
          chk_neighbors = true;
        else
          chk_neighbors = false;
      }

      // Points having too few neighbors are outliers and are passed to removed indices
      // Unless negative was set, then it's the opposite condition
      if (!chk_neighbors)
      {
        if (extract_removed_indices_)
          (*removed_indices_)[rii++] = *it;
        continue;
      }

      // Otherwise it was a normal point for output (inlier)
      indices[oii++] = *it;
    }
  }
  // NaN or Inf values could exist => use radius search
  else
  {
    for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
    {
      // Perform the radius search
      // Note: k includes the query point, so is always at least 1
      int k = searcher_->radiusSearch (*it, search_radius_, nn_indices, nn_dists);

      // Points having too few neighbors are outliers and are passed to removed indices
      // Unless negative was set, then it's the opposite condition
      if ((!negative_ && k <= min_pts_radius_) || (negative_ && k > min_pts_radius_))
      {
        if (extract_removed_indices_)
          (*removed_indices_)[rii++] = *it;
        continue;
      }

      // Otherwise it was a normal point for output (inlier)
      indices[oii++] = *it;
    }
  }

  // Resize the output arrays
  indices.resize (oii);
  removed_indices_->resize (rii);
}

3.示例代码
#include <pcl/io/pcd_io.h>  //文件输入输出
#include <pcl/point_types.h>  //点类型相关定义
#include <pcl/visualization/cloud_viewer.h>  //点云可视化相关定义
#include <pcl/filters/radius_outlier_removal.h>  //滤波相关
#include <pcl/common/common.h>  
#include <iostream>

using namespace std;

int main()
{
	//1.读取点云
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PCDReader r;
	r.read<pcl::PointXYZ>("data\\table_scene_lms400.pcd", *cloud);
	cout << "there are " << cloud->points.size() << " points before filtering." << endl;

	//2.半径滤波
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filter(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::RadiusOutlierRemoval<pcl::PointXYZ> sor;
	sor.setInputCloud(cloud);
	sor.setRadiusSearch(0.02);
	sor.setMinNeighborsInRadius(15);
	sor.setNegative(false); 
	sor.filter(*cloud_filter);  

	//3.滤波结果保存
	pcl::PCDWriter w;
	w.writeASCII<pcl::PointXYZ>("data\\table_scene_lms400_Radius_filter.pcd", *cloud_filter);
	cout << "there are " << cloud_filter->points.size() << " points after filtering." << endl;

	system("pause");
	return 0;
}

4.示例代码结果

半径滤波前
半径滤波后
PCL官网示例

欢迎关注我的公众号。
在这里插入图片描述

  • 2
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
PCL(Point Cloud Library)是一个用于处理点云数据的开源库。半径滤波PCL中常用的点云滤波方法之一,用于去除离群和平滑点云数据。 半径滤波的过程如下: 1. 定义一个搜索半径(radius)来确定每个的邻域范围。 2. 对于点云中的每个,搜索其邻域内的所有。 3. 计算邻域内所有的平均值或中值,并将该值作为当前的新坐标。 4. 重复步骤2和步骤3,直到处理完所有的。 5. 可选地,可以使用统计学方法(例如标准差)来进一步排除离群。 使用PCL进行半径滤波的示例代码如下: ```cpp #include <pcl/point_cloud.h> #include <pcl/filters/radius_outlier_removal.h> int main() { // 创建输入点云 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 读取或生成点云数据 // 创建半径滤波对象 pcl::RadiusOutlierRemoval<pcl::PointXYZ> radius_filter; radius_filter.setInputCloud(cloud); radius_filter.setRadiusSearch(0.1); // 设置半径搜索范围 radius_filter.setMinNeighborsInRadius(10); // 设置邻居的最小数量 // 执行半径滤波 pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<pcl::PointXYZ>); radius_filter.filter(*filtered_cloud); return 0; } ``` 上述代码中,我们首先创建了一个输入点云对象`cloud`,然后创建了`RadiusOutlierRemoval`对象`radius_filter`来进行半径滤波。设置半径搜索范围和邻居的最小数量后,调用`filter`方法执行滤波操作,并将结果保存在`filtered_cloud`中。 需要注意的是,半径滤波只能去除离群和平滑点云数据,对于边缘保持和细节保持较差。在实际应用中,可以根据具体需求调整半径和邻居的数量来获取理想的滤波效果。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遥感与地理信息

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值