PCL model_outlier_removal模型滤波

一、概述

  使用矩阵对点云进行旋转平移。

二、代码

model_outlier_removal.cpp

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/model_outlier_removal.h>

int
main ()
{
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_sphere_filtered (new pcl::PointCloud<pcl::PointXYZ>);

  // 1. Generate cloud data
  std::size_t noise_size = 5;
  std::size_t sphere_data_size = 10;
  cloud->width = noise_size + sphere_data_size;
  cloud->height = 1;
  cloud->points.resize (cloud->width * cloud->height);
  // 1.1 Add noise
  for (std::size_t i = 0; i < noise_size; ++i)
  {
    (*cloud)[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
    (*cloud)[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
    (*cloud)[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
  }
  // 1.2 Add sphere:
  double rand_x1 = 1;
  double rand_x2 = 1;
  for (std::size_t i = noise_size; i < (noise_size + sphere_data_size); ++i)
  {
    // See: http://mathworld.wolfram.com/SpherePointPicking.html
    while (pow (rand_x1, 2) + pow (rand_x2, 2) >= 1)
    {
      rand_x1 = (rand () % 100) / (50.0f) - 1;
      rand_x2 = (rand () % 100) / (50.0f) - 1;
    }
    double pre_calc = sqrt (1 - pow (rand_x1, 2) - pow (rand_x2, 2));
    (*cloud)[i].x = 2 * rand_x1 * pre_calc;
    (*cloud)[i].y = 2 * rand_x2 * pre_calc;
    (*cloud)[i].z = 1 - 2 * (pow (rand_x1, 2) + pow (rand_x2, 2));
    rand_x1 = 1;
    rand_x2 = 1;
  }

  std::cerr << "Cloud before filtering: " << std::endl;
  for (const auto& point: *cloud)
    std::cout << "    " << point.x << " " << point.y << " " << point.z << std::endl;

  // 2. filter sphere:
  // 2.1 generate model:
  // modelparameter for this sphere:
  // position.x: 0, position.y: 0, position.z:0, radius: 1
  pcl::ModelCoefficients sphere_coeff;
  sphere_coeff.values.resize (4);
  sphere_coeff.values[0] = 0;
  sphere_coeff.values[1] = 0;
  sphere_coeff.values[2] = 0;
  sphere_coeff.values[3] = 1;

  pcl::ModelOutlierRemoval<pcl::PointXYZ> sphere_filter;
  sphere_filter.setModelCoefficients (sphere_coeff);
  sphere_filter.setThreshold (0.05);
  sphere_filter.setModelType (pcl::SACMODEL_SPHERE);
  sphere_filter.setInputCloud (cloud);
  sphere_filter.filter (*cloud_sphere_filtered);

  std::cerr << "Sphere after filtering: " << std::endl;
  for (const auto& point: *cloud_sphere_filtered)
    std::cout << "    " << point.x << " " << point.y << " " << point.z << std::endl;

  return (0);
}

三、结果

Cloud before filtering:
    1.28125 577.094 197.938
    828.125 599.031 491.375
    358.688 917.438 842.562
    764.5 178.281 879.531
    727.531 525.844 311.281
    -0.279483 -0.803513 0.5256
    -0.279552 0.818687 -0.5016
    0.064711 0.905955 -0.4184
    0.6451 -0.584622 -0.492
    -0.486568 -0.457947 -0.744
    0.777133 -0.0518089 -0.6272
    -0.739387 0.646964 0.1864
    -0.428788 0.678914 0.596
    -0.841921 0.376649 -0.3864
    -0.435684 -0.312799 -0.844
Sphere after filtering:
    -0.279483 -0.803513 0.5256
    -0.279552 0.818687 -0.5016
    0.064711 0.905955 -0.4184
    0.6451 -0.584622 -0.492
    -0.486568 -0.457947 -0.744
    0.777133 -0.0518089 -0.6272
    -0.739387 0.646964 0.1864
    -0.428788 0.678914 0.596
    -0.841921 0.376649 -0.3864
    -0.435684 -0.312799 -0.844
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's an example code snippet for using the `StatisticalOutlierRemoval` filter in PCL: ```cpp #include <iostream> #include <pcl/io/pcd_io.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>); if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) == -1) { PCL_ERROR("Couldn't read file\n"); return (-1); } pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; sor.setInputCloud(cloud); sor.setMeanK(50); // number of points to use for mean distance calculation sor.setStddevMulThresh(1.0); // standard deviation multiplier threshold sor.filter(*cloud); pcl::io::savePCDFileASCII("output.pcd", *cloud); std::cout << "Filtered cloud saved as output.pcd\n"; return 0; } ``` In this example, we first load a point cloud from a PCD file using `pcl::io::loadPCDFile`. Then we create a `StatisticalOutlierRemoval` object and set the input cloud using `setInputCloud`. We also set two parameters: `setMeanK` sets the number of nearest neighbors to use for mean distance calculation, and `setStddevMulThresh` sets the threshold for removing outliers based on standard deviation. Finally, we call `filter` to apply the filter to the input cloud and save the filtered cloud to a new PCD file using `pcl::io::savePCDFileASCII`. Note that `StatisticalOutlierRemoval` works by computing the mean distance of each point to its k nearest neighbors, and then removing any points whose distance is greater than a certain number of standard deviations from the mean. By adjusting the values of `setMeanK` and `setStddevMulThresh`, you can control the sensitivity of the filter to outliers.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值