[点云分割] 使用 ModelOutlierRemoving 过滤点云

使用已知系数的几何模型,例如平面或球体,对一个点云进行滤波操作。

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);
}
相关推荐
新手小白勇闯新世界10 天前
论文阅读(一种基于球面投影和特征提取的岩石点云快速配准算法)
论文阅读·点云·配准·icp·特征提取
Deepcong16 天前
3D点云与2D图像的相互转换:2D图像对应像素的坐标 转为3D空间的对应坐标
c++·目标跟踪·点云·2d转3d
PHP代码16 天前
entwine 和 conda环境下 使用和踩坑 详细步骤! 已解决
服务器·conda·点云
空名_Noname1 个月前
Open3D实现点云数据的序列化与网络传输
c++·点云·open3d
LiDAR点云4 个月前
任意空间平面点云旋转投影至水平面—罗德里格旋转公式
点云·点云水平投影·罗德里格旋转
点云-激光雷达-Slam-三维牙齿4 个月前
单目测距 单目相机测距 图片像素坐标转实际坐标的一种转换方案
人工智能·python·算法·点云
点云-激光雷达-Slam-三维牙齿5 个月前
Open3d 点云投影到 xoy yoz 平面最简单的方式(附python 代码)
python·算法·平面·点云
coco_1998_25 个月前
Ubuntu22.04 搭建 PCL 环境(VTK源码安装),PCL测试代码
linux·vtk·点云·pcl
LiDAR点云6 个月前
PCL平面多边形可视化
点云·pcl·多边形可视化
信必诺6 个月前
VTK —— 二、教程七 - 对点云进行操作(按下r键切换选取或观察模式)(附完整源码)
c++·vtk·点云