[点云分割] 使用 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);
}
相关推荐
深蓝学院18 天前
李飞飞团队2026开年首篇工作:一张RGB-D图像让机器人野外零样本全能操作
机器人·点云·视觉·机器人操作·具身智能·世界模型
水龙吟啸1 个月前
SFM逆向扫描工程:重建3D物体
点云·特征匹配·最小二乘法·深度相机·角点检测·3d重建·icp算法
深紫色的三北六号1 个月前
基于 ContextCapture SDK 的 Python 自动化三维建模
python·点云·三维建模·contextcapture·las
老黄编程2 个月前
点云NARF关键点原理、算法描述及参数详细描述
算法·点云·narf特征点
老黄编程2 个月前
点云生成深度图的原理及算法步骤和参数详细说明
数学·算法·点云·深度图
放羊郎3 个月前
基于三维点云图的路径规划
人工智能·动态规划·slam·点云·路径规划·激光slam
fanstering4 个月前
腾讯混元P3-SAM: Native 3D Part Segmentation
笔记·学习·3d·点云
老歌老听老掉牙4 个月前
OpenCASCADE 点云拟合曲线与曲面:从零实现到工业级应用
c++·点云·opencascade
网易独家音乐人Mike Zhou5 个月前
【Python】圆柱体内部3D点云仿真及ply文件生成,圆形3D点云检测及拟合算法
stm32·单片机·mcu·物联网·算法·点云·iot
元让_vincent6 个月前
论文Review 激光动态物体剔除 Dynablox | RAL2023 ETH MIT出品!
人工智能·计算机视觉·目标跟踪·机器人·自动驾驶·点云·动态物体剔除