当前位置:网站首页>PCL学习之滤波Filtering

PCL学习之滤波Filtering

2022-08-09 18:53:00 风轻云淡_Cauchy

1. Filtering a PointCloud using a PassThrough filter–简单区间滤波方法

a simple filtering along a specified dimension – that is, cut off values that are either inside or outside a given user range. [a, b]之间的留下,之外的被滤掉,简单的滤波。

// Create the filtering object
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.0, 1.0);
//pass.setFilterLimitsNegative (true);
pass.filter (*cloud_filtered);

2. Downsampling a PointCloud using a VoxelGrid filter–体素网格

downsample – that is, reduce the number of points – a point cloud dataset, using a voxelized grid approach. 体素化网格方法。
1)VoxelGrid 类在输入点云数据上创建一个 3D 体素网格(将体素网格想象为空间中的一组微小的 3D 框)。
2)在每个体素(即 3D 框)中,所有存在的点都将以其质心近似(即下采样)。

// Create the filtering object
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud (cloud);
sor.setLeafSize (0.01f, 0.01f, 0.01f);
sor.filter (*cloud_filtered);

2.1 示例结果

原始:
在这里插入图片描述
以0.01立方体降采样的结果:
在这里插入图片描述
以0.02立方体降采样结果:
在这里插入图片描述

3. Removing outliers using a StatisticalOutlierRemoval filter

remove noisy measurements, e.g. outliers, from a point cloud dataset using statistical analysis techniques.

  • 场景:激光扫描通常会生成不同点密度的点云数据集。
  • 缺点:一般会产生稀疏异常值。
  • 结果:使局部点云特征(例如表面法线或曲率变化)的估计复杂化,从而导致错误的值,进而可能导致点云配准失败。
  • 解决方法:稀疏异常值去除基于输入数据集中点到邻居距离分布的计算;即,对于每个点,计算从它到所有邻居的平均距离。通过假设结果分布是具有均值和标准差的高斯分布,所有平均距离在由全局距离均值和标准差定义的区间之外的点都可以被视为异常值并从数据集中进行修剪。
    在这里插入图片描述
// Create the filtering object
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (cloud);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
sor.filter (*cloud_filtered);
// sor.setNegative(true) // to obtain the outliers,as follows:3.1 outliers 结果

3.1 示例结果

原始数据:
在这里插入图片描述
inliers结果:
在这里插入图片描述
outliers结果:
在这里插入图片描述

4. Projecting points using a parametric model

project points onto a parametric model (e.g., plane, sphere, etc). The parametric model is given through a set of coefficients – in the case of a plane, through its equation: ax + by + cz + d = 0. 投影到一个平面上

// Create a set of planar coefficients with X=Y=0,Z=1
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
coefficients->values.resize (4);
coefficients->values[0] = coefficients->values[1] = 0;
coefficients->values[2] = 1.0;
36coefficients->values[3] = 0;

// Create the filtering object
pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType (pcl::SACMODEL_PLANE);
proj.setInputCloud (cloud);
proj.setModelCoefficients (coefficients);
proj.filter (*cloud_projected);

结果:
在这里插入图片描述

在这里插入图片描述
red (x), green (y), and blue (z);red as the points before projection and green as the points after projection。

5. Extracting indices from a PointCloud

use an ExtractIndices filter to extract a subset of points from a point cloud based on the indices output by a segmentation algorithm。

  • 这块儿有点儿不明白,后面有机会再深入学习

代码先粘到这儿:

#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>

int main()
{
    
  pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), cloud_p (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);

  // Fill in the cloud data
  pcl::PCDReader reader;
  reader.read ("table_scene_lms400.pcd", *cloud_blob);

  std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;

  // Create the filtering object: downsample the dataset using a leaf size of 1cm
  pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
  sor.setInputCloud (cloud_blob);
  sor.setLeafSize (0.01f, 0.01f, 0.01f);
  sor.filter (*cloud_filtered_blob);

  // Convert to the templated PointCloud
  pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered);

  std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;

  // Write the downsampled version to disk
  pcl::PCDWriter writer;
  writer.write<pcl::PointXYZ> ("table_scene_lms400_downsampled.pcd", *cloud_filtered, false);

  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
  pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
  // Create the segmentation object
  pcl::SACSegmentation<pcl::PointXYZ> seg;
  // Optional
  seg.setOptimizeCoefficients (true);
  // Mandatory
  seg.setModelType (pcl::SACMODEL_PLANE);
  seg.setMethodType (pcl::SAC_RANSAC);
  seg.setMaxIterations (1000);
  seg.setDistanceThreshold (0.01);

  // Create the filtering object
  pcl::ExtractIndices<pcl::PointXYZ> extract;

  int i = 0, nr_points = (int) cloud_filtered->size ();
  // While 30% of the original cloud is still there
  while (cloud_filtered->size () > 0.3 * nr_points)
  {
    
    // Segment the largest planar component from the remaining cloud
    seg.setInputCloud (cloud_filtered);
    seg.segment (*inliers, *coefficients);
    if (inliers->indices.size () == 0)
    {
    
      std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
      break;
    }

    // Extract the inliers
    extract.setInputCloud (cloud_filtered);
    extract.setIndices (inliers);
    extract.setNegative (false);
    extract.filter (*cloud_p);
    std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;

    std::stringstream ss;
    ss << "table_scene_lms400_plane_" << i << ".pcd";
    writer.write<pcl::PointXYZ> (ss.str (), *cloud_p, false);

    // Create the filtering object
    extract.setNegative (true);
    extract.filter (*cloud_f);
    cloud_filtered.swap (cloud_f);
    i++;
  }

  return (0);

}

5.1 示例结果

先通过体素网格降采样,减少点云,为后面的Extract indices工作节省运算量。
在这里插入图片描述
Extract indices结果:
在这里插入图片描述
在这里插入图片描述

6. Removing outliers using a Conditional or RadiusOutlier removal

ConditionalRemoval filter which removes all indices in the given input cloud that do not satisfy one or more given conditions;
RadiusOutlierRemoval filter which removes all indices in its input cloud that don’t have at least some number of neighbors within a certain range。

原网站

版权声明
本文为[风轻云淡_Cauchy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/duanyuwangyuyan/article/details/125822241