• PCL学习之滤波Filtering


    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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 结果
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

    在这里插入图片描述
    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 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    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。

  • 相关阅读:
    Mac OS 使用ScreenCaptureKit进行窗口抓取和系统声音抓取
    Redis-shake 数据迁移工具
    leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination
    【操作系统】保姆级教程(VMware)Ubuntu+qemu+xv6安装调试
    Linux应用开发 - 多线程编程
    机器学习模型2——决策树
    matlab数据处理: cell table array+datetime
    【知识图谱论文】AnyBURL:用于知识图完成的随时自下而上的规则学习
    基于springboot的高校学生实践成长检测系统
    Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
  • 原文地址:https://blog.csdn.net/duanyuwangyuyan/article/details/125822241