[点云分割] 条件欧氏聚类分割

介绍

条件欧氏聚类分割是一种基于欧氏距离和条件限制的点云分割方法。它通过计算点云中点与点之间的欧氏距离,并结合一定的条件限制来将点云分割成不同的区域或聚类。

在条件欧氏聚类分割中,通常会定义以下两个条件来判断两个点是否属于同一个聚类:

  1. 距离条件:两个点之间的欧氏距离是否小于设定的阈值。如果两个点之间的距离小于阈值,则认为它们是相邻的,属于同一个聚类。

  2. 条件限制:除了距离条件外,还可以根据其他的条件来限制聚类的形成。例如,可以限制点的法线方向、颜色、强度等属性的相似性,只有当这些属性满足一定的条件时,两个点才被认为是相邻的,属于同一个聚类。

条件欧氏聚类分割的步骤通常包括以下几个步骤:

  1. 初始化:设置距离阈值和其他条件限制的参数。

  2. 遍历点云:对于点云中的每个点,依次进行以下操作:

    • 计算当前点与其周围点之间的欧氏距离。

    • 根据距离条件和其他条件限制,判断当前点是否与周围点属于同一个聚类。如果是,则将它们标记为同一个聚类。

    • 继续遍历其他未被标记的点,重复上述操作,直到所有点都被遍历完。

  3. 输出聚类结果:将同一个聚类的点标记为一组,形成不同的聚类簇。

效果

代码

cpp 复制代码
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/console/time.h>

#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/segmentation/conditional_euclidean_clustering.h>

typedef pcl::PointXYZI PointTypeIO;
typedef pcl::PointXYZINormal PointTypeFull;

bool enforceIntensitySimilarity (const PointTypeFull& point_a, const PointTypeFull& point_b, float /*squared_distance*/)
    {
      if (std::abs (point_a.intensity - point_b.intensity) < 5.0f)
            return (true);
      else
           return (false);
    }

bool enforceNormalOrIntensitySimilarity (const PointTypeFull& point_a, const PointTypeFull& point_b, float /*squared_distance*/)
{
  // 将点云的法线信息转换未Eigen库的Eigen:vector3f类型
  Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap (), point_b_normal = point_b.getNormalVector3fMap ();

  // 判断点云A的点云B的强度差是否小于5.0
  if (std::abs (point_a.intensity - point_b.intensity) < 5.0f)
        return (true);

  // 判断点云A和点云B的法线夹角的余弦值是否大于30°对应的余弦值,即判断法线相似性
  if (std::abs (point_a_normal.dot (point_b_normal)) > std::cos (30.0f / 180.0f * static_cast<float> (M_PI)))
        return (true);
  return (false);
}

bool customRegionGrowing (const PointTypeFull& point_a, const PointTypeFull& point_b, float squared_distance)
{
  Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap (), point_b_normal = point_b.getNormalVector3fMap ();

  // 根据平方距离的大小,判断生长条件
  if (squared_distance < 10000)
      {
       if (std::abs (point_a.intensity - point_b.intensity) < 8.0f)
              return (true);
       if (std::abs (point_a_normal.dot (point_b_normal)) > std::cos (30.0f / 180.0f * static_cast<float> (M_PI)))
              return (true);
      }
  else
      {
        if (std::abs (point_a.intensity - point_b.intensity) < 3.0f)
              return (true);
      }
  return (false);
}

int main ()
{
    // Data containers used
    pcl::PointCloud<PointTypeIO>::Ptr cloud_in (new pcl::PointCloud<PointTypeIO>), cloud_out (new pcl::PointCloud<PointTypeIO>);
    pcl::PointCloud<PointTypeFull>::Ptr cloud_with_normals (new pcl::PointCloud<PointTypeFull>);
    pcl::IndicesClustersPtr clusters (new pcl::IndicesClusters), small_clusters (new pcl::IndicesClusters), large_clusters (new pcl::IndicesClusters);
    pcl::search::KdTree<PointTypeIO>::Ptr search_tree (new pcl::search::KdTree<PointTypeIO>);
    pcl::console::TicToc tt;

     // Load the input point cloud
    std::cerr << "Loading...\n", tt.tic ();
    pcl::io::loadPCDFile ("Statues_4.pcd", *cloud_in);
    std::cerr << ">> Done: " << tt.toc () << " ms, " << cloud_in->size () << " points\n";

    // Downsample the cloud using a Voxel Grid class
    std::cerr << "Downsampling...\n", tt.tic ();
    pcl::VoxelGrid<PointTypeIO> vg;
    vg.setInputCloud (cloud_in);
    vg.setLeafSize (80.0, 80.0, 80.0);
    vg.setDownsampleAllData (true);
    vg.filter (*cloud_out);
    std::cerr << ">> Done: " << tt.toc () << " ms, " << cloud_out->size () << " points\n";

    // Set up a Normal Estimation class and merge data in cloud_with_normals
    std::cerr << "Computing normals...\n", tt.tic ();
    pcl::copyPointCloud (*cloud_out, *cloud_with_normals);
    pcl::NormalEstimation<PointTypeIO, PointTypeFull> ne;
    ne.setInputCloud (cloud_out);
    ne.setSearchMethod (search_tree);
    ne.setRadiusSearch (300.0);
    ne.compute (*cloud_with_normals);
    std::cerr << ">> Done: " << tt.toc () << " ms\n";

    // Set up a Conditional Euclidean Clustering class
    std::cerr << "Segmenting to clusters...\n", tt.tic ();
    pcl::ConditionalEuclideanClustering<PointTypeFull> cec (true);
    cec.setInputCloud (cloud_with_normals);
    cec.setConditionFunction (&customRegionGrowing);
    cec.setClusterTolerance (500.0);
    cec.setMinClusterSize (cloud_with_normals->size () / 1000);
    cec.setMaxClusterSize (cloud_with_normals->size () / 5);
    cec.segment (*clusters);
    cec.getRemovedClusters (small_clusters, large_clusters);
    std::cerr << ">> Done: " << tt.toc () << " ms\n";

    // Using the intensity channel for lazy visualization of the output
    for (const auto& small_cluster : (*small_clusters))
    for (const auto& j : small_cluster.indices)
        (*cloud_out)[j].intensity = -2.0;
    for (const auto& large_cluster : (*large_clusters))
        for (const auto& j : large_cluster.indices)
            (*cloud_out)[j].intensity = +10.0;
    for (const auto& cluster : (*clusters))
        {
            int label = rand () % 8;
            for (const auto& j : cluster.indices)
                  (*cloud_out)[j].intensity = label;
        }

    // Save the output point cloud
        std::cerr << "Saving...\n", tt.tic ();
    pcl::io::savePCDFile ("output.pcd", *cloud_out);
    std::cerr << ">> Done: " << tt.toc () << " ms\n";

    return (0);
}
相关推荐
haluhalu.3 分钟前
Agent = LLM + 上下文 + 工具(含观察 / 动作空间、ReAct 循环)
前端·javascript·人工智能·python·react.js·机器学习·个人开发
YOLO数据集集合2 小时前
小麦穗检测数据集 | 小麦穗检测 植物表型 密集目标检测 农业AI9182期
人工智能·yolo·目标检测·机器学习·目标跟踪·小麦穗
Rocky Ding*9 小时前
MOSS-VL技术原理深度解析:让视觉流持续进入生成过程,实时 Agent 才有架构基础
论文阅读·人工智能·深度学习·机器学习·aigc·多模态·ai-native
2601_962780919 小时前
2027 届秋招|数学背景求职管理咨询,商业认知 + 数据分析能力构建方案
数据挖掘·数据分析
田里的水稻9 小时前
EP_ROS2的相机视频流传递说明
数码相机·机器学习
加强洁西卡9 小时前
【AI】机器学习常见的指标及其解释
人工智能·机器学习
面包狗AI4S10 小时前
【AI4S】生化环材高可信技术与产业周报(2026-10-03—2026-10-09)
人工智能·深度学习·机器学习·ai
DongQiShanRen10 小时前
玄龙(上):TICK 主循环——意识心跳怎么跳
linux·jvm·数据库·人工智能·数据挖掘·rust
田里的水稻11 小时前
EP_ROS框架---相机视频流交互二
人工智能·深度学习·数码相机·机器学习·迁移学习
Dfreedom.11 小时前
光谱定性分析中PCA、SVM与LDA方法的对比研究
机器学习·支持向量机·lda·pca·svm·化学计量学·光谱数据处理