• PCL——(7)点云滤波


    @

    一、简介

    在这里插入图片描述
    PCL中总结了几种需要进行点云滤波处理的情况,这几种情况如下:

    • (1)点云数据密度不规则需要平滑。
    • (2)因为遮挡等问题造成离群点需要去除。
    • (3)大量数据需要进行下采样( Downsample)。
    • (4)噪音数据需要去除。

    对应的方法如下:

    • (1)按具体给定的规则限制过滤去除点。
    • (2)通过常用滤波算法修改点的部分属性。
    • (3)对数据进行下采样,

    二、PCL中的滤波器

    2.1 直通滤波器

    去除掉在用户指定某一维度上的指定范围内(或外)部的点。

    #include <iostream>
    #include <pcl/point_types.h>
    #include <pcl/filters/passthrough.h>
    
    int
     main (int argc, char** argv)
    {
      // 过滤前点云
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
      // 保存过滤后点云
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
    
      // Fill in the cloud data
      cloud->width  = 5;
      cloud->height = 1;
      cloud->points.resize (cloud->width * cloud->height);
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
      {
        cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
      }
    
      std::cerr << "Cloud before filtering: " << std::endl;
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
        std::cerr << "    " << cloud->points[i].x << " " 
                            << cloud->points[i].y << " " 
                            << cloud->points[i].z << std::endl;
    
      // Create the filtering object
      pcl::PassThrough<pcl::PointXYZ> pass;
      pass.setInputCloud (cloud);
      pass.setFilterFieldName ("z");// 滤波字段设置为z轴方向
      pass.setFilterLimits (0.0, 1.0);
      //pass.setFilterLimitsNegative (true); //设置保留范围内还是过滤掉范围内, 默认为flase,
      										//可以注释掉。true为过滤掉范围内的,flase为过滤掉范围外的
      pass.filter (*cloud_filtered);
    
      std::cerr << "Cloud after filtering: " << std::endl;
      for (std::size_t i = 0; i < cloud_filtered->points.size (); ++i)
        std::cerr << "    " << cloud_filtered->points[i].x << " " 
                            << cloud_filtered->points[i].y << " " 
                            << cloud_filtered->points[i].z << std::endl;
    
      return (0);
    }
    

    2.2 使用VoxelGrid滤波器对点云进行下采样

    使用体素化网格方法实现下采样,即减少点的数量 减少点云数据,并同时保存点云的形状特征,在提高配准,曲面重建,形状识别等算法速度中非常实用,PCL是实现的VoxelGrid类通过输入的点云数据创建一个三维体素栅格,容纳后每个体素内用体素中所有点的重心来近似显示体素中其他点,这样该体素内所有点都用一个重心点最终表示,对于所有体素处理后得到的过滤后的点云,这种方法比用体素中心逼近的方法更慢,但是对于采样点对应曲面的表示更为准确。

    #include <iostream>
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    #include <pcl/filters/voxel_grid.h>
    
    int
    main (int argc, char** argv)
    {
      pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2 ());
      pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());
    
      // Fill in the cloud data
      pcl::PCDReader reader;
      // Replace the path below with the path where you saved your file
      reader.read ("table_scene_lms400.pcd", *cloud); // Remember to download the file first!
    
      std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height 
           << " data points (" << pcl::getFieldsList (*cloud) << ")." << std::endl;
    
      // Create the filtering object
      pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
      sor.setInputCloud (cloud);
      sor.setLeafSize (0.01f, 0.01f, 0.01f);//设置滤波时创建的体素体积为1cm的立方体
      sor.filter (*cloud_filtered);//执行滤波处理,存储输出
    
      std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height 
           << " data points (" << pcl::getFieldsList (*cloud_filtered) << ")." << std::endl;
    
      pcl::PCDWriter writer;
      writer.write ("table_scene_lms400_downsampled.pcd", *cloud_filtered, 
             Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), false);
    
      return (0);
    }
    

    2.3 statisticalOutlierRemoval滤波器移除离群点

    使用统计分析技术,从一个点云数据中集中移除测量噪声点(也就是离群点)比如:激光扫描通常会产生密度不均匀的点云数据集,另外测量中的误差也会产生稀疏的离群点,使效果不好,估计局部点云特征(例如采样点处法向量或曲率变化率)的运算复杂,这会导致错误的数值,反过来就会导致点云配准等后期的处理失败。

    解决办法:每个点的邻域进行一个统计分析,并修剪掉一些不符合一定标准的点,稀疏离群点移除方法基于在输入数据中对点到临近点的距离分布的计算,对每一个点,计算它到它的所有临近点的平均距离,,假设得到的结果是一个高斯分布,其形状是由均值和标准差决定,平均距离在标准范围之外的点,可以被定义为离群点并可从数据中去除。
    在这里插入图片描述

    #include <iostream>
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    #include <pcl/filters/statistical_outlier_removal.h>
    
    int
    main (int argc, char** argv)
    {
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
    
      // Fill in the cloud data
      pcl::PCDReader reader;
      // Replace the path below with the path where you saved your file
      reader.read<pcl::PointXYZ> ("table_scene_lms400.pcd", *cloud);
    
      std::cerr << "Cloud before filtering: " << std::endl;
      std::cerr << *cloud << std::endl;
    
      // Create the filtering object
      // 创建滤波器,对每个点分析的临近点的个数设置为50 ,并将标准差的倍数设置为1  这意味着如果一
       //个点的距离超出了平均距离一个标准差以上,则该点被标记为离群点,并将它移除,存储起来
      pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
      sor.setInputCloud (cloud);
      sor.setMeanK (50);  //设置在进行统计时考虑查询点临近点数
      sor.setStddevMulThresh (1.0);//设置判断是否为离群点的阀值
      sor.filter (*cloud_filtered);
    
      std::cerr << "Cloud after filtering: " << std::endl;
      std::cerr << *cloud_filtered << std::endl;
    
      pcl::PCDWriter writer;
      writer.write<pcl::PointXYZ> ("table_scene_lms400_inliers.pcd", *cloud_filtered, false);
      
    // 然后,使用同样的参数再次调用该滤波器,但是利用函数setNegative设置使输出取外点,以获取离群点数据(也就是原本滤除掉的点)。
      sor.setNegative (true);
      sor.filter (*cloud_filtered);
      writer.write<pcl::PointXYZ> ("table_scene_lms400_outliers.pcd", *cloud_filtered, false);
    
      return (0);
    }
    

    2.4 使用参数化模型投影点云

    将点投影到一个参数化模型上(平面或者球体等),参数化模型通过一组参数来设定,对于平面来说使用其等式形式:(ax+by+cz+d=0).在PCL中有特意存储常见模型系数的数据结构.

    #include <iostream>
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    #include <pcl/ModelCoefficients.h> //模型系数头文件
    #include <pcl/filters/project_inliers.h> //投影滤波类头文件
    
    int
     main (int argc, char** argv)
    {
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);
    
      // Fill in the cloud data
      cloud->width  = 5;
      cloud->height = 1;
      cloud->points.resize (cloud->width * cloud->height);
    
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
      {
        cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
      }
    
      std::cerr << "Cloud before projection: " << std::endl;
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
        std::cerr << "    " << cloud->points[i].x << " " 
                            << cloud->points[i].y << " " 
                            << cloud->points[i].z << std::endl;
    
      // Create a set of planar coefficients with X=Y=0,Z=1
      // 填充ModelCoefficients的值,使用ax+by+cz+d=0平面模型,其中 a=b=d=0,c=1 也就是X——Y平面
      //定义模型系数对象,并填充对应的数据
      pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients ());
      coefficients->values.resize (4);
      coefficients->values[0] = coefficients->values[1] = 0;
      coefficients->values[2] = 1.0;
      coefficients->values[3] = 0;
    
      // 创建ProjectInliers对象,使用ModelCoefficients作为投影对象的模型参数
      pcl::ProjectInliers<pcl::PointXYZ> proj;     //创建投影滤波对象
      proj.setModelType (pcl::SACMODEL_PLANE);      //设置对象对应的投影模型
      proj.setInputCloud (cloud);                   //设置输入点云
      proj.setModelCoefficients (coefficients);       //设置模型对应的系数
      proj.filter (*cloud_projected);                 //投影结果存储
    
      std::cerr << "Cloud after projection: " << std::endl;
      for (std::size_t i = 0; i < cloud_projected->points.size (); ++i)
        std::cerr << "    " << cloud_projected->points[i].x << " " 
                            << cloud_projected->points[i].y << " " 
                            << cloud_projected->points[i].z << std::endl;
    
      return (0);
    }
    

    编译运行的结果如下
    在这里插入图片描述
    实验结果可以看出投影前的Z轴都不为0 ,都是随机产生的值,投影之后,打印的结果表明,xy的值都没有改变,z都变为0

    所以该投影滤波类就是输入点云和投影模型,输出为投影到模型上之后的点云。

    2.5 从一个点云中提取索引(子集)

    基于某一分割算法提取点云中的一个子集。这里以平面分割算法为例:

    #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 (int argc, char** argv)
    {
     /**********************************************************************************************************
       从输入的.PCD 文件载入数据后,创建一个VOxelGrid滤波器对数据进行下采样,在这里进行下才样是为了加速处理过程,
       越少的点意味着分割循环中处理起来越快
       **********************************************************************************************************/
      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->points.size ();
      // While 30% of the original cloud is still there
      while (cloud_filtered->points.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);
    }
    

    在这里插入图片描述

    2.6 使用ConditionalRemoval 或RadiusOutlinerRemoval移除离群点

    如何在滤波模块使用几种不同的方法移除离群点,对于ConditionalRemoval滤波器,可以一次删除满足对输入的点云设定的一个或多个条件指标的所有的数据点,RadiusOutlinerRemoval滤波器,它可以删除在输入点云一定范围内没有至少达到足够多近邻的所有数据点。

    2.6.1 RadiusOutlierRemoval 原理

    在点云数据中,用户指定每个点的一定范围内周围至少要有足够多的近邻。例如,如果指定至少要有1个邻居,只有黄色的点会被删除,如果指定至少要有2个邻居,黄色和绿色的点都将被删除。
    在这里插入图片描述
    关于RadiusOutlinerRemoval的理解,在点云数据中,设定每个点一定范围内周围至少有足够多的近邻,不满足就会被删除

    2.6.2 ConditionalRemoval 原理

    这个滤波器删除点云中不符合用户指定的一个或者多个条件的数据点

    #include <iostream>
    #include <pcl/point_types.h>
    #include <pcl/filters/radius_outlier_removal.h>
    #include <pcl/filters/conditional_removal.h>
    
    int
     main (int argc, char** argv)
    {
      if (argc != 2)
      {
        std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
        exit(0);
      }
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
    
      // Fill in the cloud data
      cloud->width  = 5;
      cloud->height = 1;
      cloud->points.resize (cloud->width * cloud->height);
    
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
      {
        cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
      }
    
      if (strcmp(argv[1], "-r") == 0){
        pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
        // build the filter
        outrem.setInputCloud(cloud);
        outrem.setRadiusSearch(0.8);//设置半径为0.8的范围内找临近点
        outrem.setMinNeighborsInRadius (2);//设置查询点的邻域点集数小于2的删除
        // apply filter
        outrem.filter (*cloud_filtered);  //执行条件滤波   在半径为0.8 在此半径内必须要有两个邻居点,此点才会保存
      }
      else if (strcmp(argv[1], "-c") == 0){
        // build the condition
        pcl::ConditionAnd<pcl::PointXYZ>::Ptr range_cond (new
          pcl::ConditionAnd<pcl::PointXYZ> ()); //创建条件定义对象
          //为条件定义对象添加比较算子
        range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new
          pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::GT, 0.0)));//添加在Z字段上大于0的比较算子
        range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new
          pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::LT, 0.8)));/添加在Z字段上小于0.8的比较算子
        // build the filter
         // 创建滤波器并用条件定义对象初始化
        pcl::ConditionalRemoval<pcl::PointXYZ> condrem;
        condrem.setCondition (range_cond);
        condrem.setInputCloud (cloud);
        condrem.setKeepOrganized(true); //设置保持点云的结构
        // apply filter
        condrem.filter (*cloud_filtered);   // 执行滤波
      }
      else{
        std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
        exit(0);
      }
      std::cerr << "Cloud before filtering: " << std::endl;
      for (std::size_t i = 0; i < cloud->points.size (); ++i)
        std::cerr << "    " << cloud->points[i].x << " "
                            << cloud->points[i].y << " "
                            << cloud->points[i].z << std::endl;
      // display pointcloud after filtering
      std::cerr << "Cloud after filtering: " << std::endl;
      for (std::size_t i = 0; i < cloud_filtered->points.size (); ++i)
        std::cerr << "    " << cloud_filtered->points[i].x << " "
                            << cloud_filtered->points[i].y << " "
                            << cloud_filtered->points[i].z << std::endl;
      return (0);
    }
    

    2.7 双边滤波算法

    双边滤波算法,是通过取邻近采样点的加权平均来修正当前采样点的位置,从而达到滤波效果。同时也会有选择地剔除部分与当前采样点“差异”太大的相邻采样点,从而达到保持原特征的目的。

    2.8 CropHull任意多边形内部点云提取

    2.9 均匀采样

    pcl::UniformSampling<pcl::PointXYZ> unisam;
    //设置滤波时创建的体素体积为1cm的立方体
    //vox.setLeafSize (0.01f, 0.01f, 0.01f) ;
    //设置滤波时创建的半径球体
    unisam.setRadiusSearch(0.01f) ;
    

    在这里插入图片描述

    打赏

    码字不易,如果对您有帮助,就打赏一下吧O(∩_∩)O

    支付宝

    微信

  • 相关阅读:
    Python高效编程技巧
    Python使用Flask框架,结合Highchart,自定义图表样式主题
    Python使用Flask框架,结合Highchart,自定义基本上算是最全的导出菜单了
    Python使用Flask框架,结合Highchart,自定义导出菜单项目及顺序
    Python使用Flask框架,结合Highchart,搭配数据功能模块,加载 HTML 表格数据
    Python使用Flask框架,结合Highchart,搭配数据功能模块处理csv数据
    Python使用Flask框架,结合Highchart处理jsonl数据
    Python使用Flask框架,结合Highchart处理xml数据
    Python使用Flask框架,结合Highchart处理csv数据(引申-从文件获取数据--从数据库获取数据)
    使用Python的Flask框架,结合Highchart,动态渲染图表(Ajax 请求数据接口)
  • 原文地址:https://www.cnblogs.com/long5683/p/13278040.html
Copyright © 2020-2023  润新知