• 欧几里得聚类提取


    本教程学习使用pcl::EuclideanClusterExtraction类进行欧几里得聚类提取。

    欧几里得聚类原理:

    聚类方法需要将一个无组织的点云模型p划分为更小的部分,从而大大减少了p的整体处理时间。

    一种简单的欧几里德意义上的数据聚类方法可以通过使用固定宽度的盒子或一般的八叉树数据结构来实现。

    然而,在更一般的意义上,我们可以利用最近邻并实现本质上类似于洪水填充算法的聚类技术。

    假设有一个点云,有一张桌子和上面的物体。我们希望找到并分割位于平面上的单个物体点簇。假设我们使用 Kd 树结构来寻找最近的邻居,那么算法步骤将是:

    1、为输入点云数据集p创建一个 Kd 树表示;

    2、建立一个空的集群列表c,以及一个需要检查q的点的队列;

    3、然后,对于p中的每一点,执行以下步骤:

    • 将点添加到当前队列q;
    • 对于每一个q中的点:
      • 寻找半径为r球面的点邻域集合;
      • 对于每个邻居,检查该点是否已被处理,如果未处理,则将其添加到q;
    • 处理完q中的所有点的列表后,把q添加到群集列表c中,并重置q为空列表

    4、当p中的所有点被处理完后并且都已经属于点集群c的一部分,算法停止。

    源码:

    创建 cluster_extraction.cpp 文件

    1. 1#include <pcl/ModelCoefficients.h>
    2. 2#include <pcl/point_types.h>
    3. 3#include <pcl/io/pcd_io.h>
    4. 4#include <pcl/filters/extract_indices.h>
    5. 5#include <pcl/filters/voxel_grid.h>
    6. 6#include <pcl/features/normal_3d.h>
    7. 7#include <pcl/search/kdtree.h>
    8. 8#include <pcl/sample_consensus/method_types.h>
    9. 9#include <pcl/sample_consensus/model_types.h>
    10. 10#include <pcl/segmentation/sac_segmentation.h>
    11. 11#include <pcl/segmentation/extract_clusters.h>
    12. 12
    13. 13
    14. 14int
    15. 15main ()
    16. 16{
    17. 17 // Read in the cloud data
    18. 18 pcl::PCDReader reader;
    19. 19 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
    20. 20 reader.read ("table_scene_lms400.pcd", *cloud);
    21. 21 std::cout << "PointCloud before filtering has: " << cloud->size () << " data points." << std::endl; //*
    22. 22
    23. 23 // Create the filtering object: downsample the dataset using a leaf size of 1cm
    24. 24 pcl::VoxelGrid<pcl::PointXYZ> vg;
    25. 25 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
    26. 26 vg.setInputCloud (cloud);
    27. 27 vg.setLeafSize (0.01f, 0.01f, 0.01f);
    28. 28 vg.filter (*cloud_filtered);
    29. 29 std::cout << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl; //*
    30. 30
    31. 31 // Create the segmentation object for the planar model and set all the parameters
    32. 32 pcl::SACSegmentation<pcl::PointXYZ> seg;
    33. 33 pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    34. 34 pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    35. 35 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
    36. 36 pcl::PCDWriter writer;
    37. 37 seg.setOptimizeCoefficients (true);
    38. 38 seg.setModelType (pcl::SACMODEL_PLANE);
    39. 39 seg.setMethodType (pcl::SAC_RANSAC);
    40. 40 seg.setMaxIterations (100);
    41. 41 seg.setDistanceThreshold (0.02);
    42. 42
    43. 43 int nr_points = (int) cloud_filtered->size ();
    44. 44 while (cloud_filtered->size () > 0.3 * nr_points)
    45. 45 {
    46. 46 // Segment the largest planar component from the remaining cloud
    47. 47 seg.setInputCloud (cloud_filtered);
    48. 48 seg.segment (*inliers, *coefficients);
    49. 49 if (inliers->indices.size () == 0)
    50. 50 {
    51. 51 std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
    52. 52 break;
    53. 53 }
    54. 54
    55. 55 // Extract the planar inliers from the input cloud
    56. 56 pcl::ExtractIndices<pcl::PointXYZ> extract;
    57. 57 extract.setInputCloud (cloud_filtered);
    58. 58 extract.setIndices (inliers);
    59. 59 extract.setNegative (false);
    60. 60
    61. 61 // Get the points associated with the planar surface
    62. 62 extract.filter (*cloud_plane);
    63. 63 std::cout << "PointCloud representing the planar component: " << cloud_plane->size () << " data points." << std::endl;
    64. 64
    65. 65 // Remove the planar inliers, extract the rest
    66. 66 extract.setNegative (true);
    67. 67 extract.filter (*cloud_f);
    68. 68 *cloud_filtered = *cloud_f;
    69. 69 }
    70. 70
    71. 71 // Creating the KdTree object for the search method of the extraction
    72. 72 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
    73. 73 tree->setInputCloud (cloud_filtered);
    74. 74
    75. 75 std::vector<pcl::PointIndices> cluster_indices;
    76. 76 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    77. 77 ec.setClusterTolerance (0.02); // 2cm
    78. 78 ec.setMinClusterSize (100);
    79. 79 ec.setMaxClusterSize (25000);
    80. 80 ec.setSearchMethod (tree);
    81. 81 ec.setInputCloud (cloud_filtered);
    82. 82 ec.extract (cluster_indices);
    83. 83
    84. 84 int j = 0;
    85. 85 for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
    86. 86 {
    87. 87 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
    88. 88 for (const auto& idx : it->indices)
    89. 89 cloud_cluster->push_back ((*cloud_filtered)[idx]); //*
    90. 90 cloud_cluster->width = cloud_cluster->size ();
    91. 91 cloud_cluster->height = 1;
    92. 92 cloud_cluster->is_dense = true;
    93. 93
    94. 94 std::cout << "PointCloud representing the Cluster: " << cloud_cluster->size () << " data points." << std::endl;
    95. 95 std::stringstream ss;
    96. 96 ss << "cloud_cluster_" << j << ".pcd";
    97. 97 writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
    98. 98 j++;
    99. 99 }
    100. 100
    101. 101 return (0);
    102. 102}

    说明:

    1、相关头文件

    1. 1#include <pcl/ModelCoefficients.h>
    2. 2#include <pcl/point_types.h>
    3. 3#include <pcl/io/pcd_io.h>
    4. 4#include <pcl/filters/extract_indices.h>
    5. 5#include <pcl/filters/voxel_grid.h>
    6. 6#include <pcl/features/normal_3d.h>
    7. 7#include <pcl/search/kdtree.h>
    8. 8#include <pcl/sample_consensus/method_types.h>
    9. 9#include <pcl/sample_consensus/model_types.h>
    10. 10#include <pcl/segmentation/sac_segmentation.h>
    11. 11#include <pcl/segmentation/extract_clusters.h>

    2、创建读取对象,读取点云数据,输出点云信息

    1. 17 // Read in the cloud data
    2. 18 pcl::PCDReader reader;
    3. 19 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
    4. 20 reader.read ("table_scene_lms400.pcd", *cloud);
    5. 21 std::cout << "PointCloud before filtering has: " << cloud->size () << " data points." << std::endl; //*

    3、创建体素滤波对象,对点云数据进行下采样

    1. 23 // Create the filtering object: downsample the dataset using a leaf size of 1cm
    2. 24 pcl::VoxelGrid<pcl::PointXYZ> vg;
    3. 25 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
    4. 26 vg.setInputCloud (cloud);
    5. 27 vg.setLeafSize (0.01f, 0.01f, 0.01f);
    6. 28 vg.filter (*cloud_filtered);
    7. 29 std::cout << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl; //*

    4、为平面模型创建分割对象并设置所有参数

    1. 31 // Create the segmentation object for the planar model and set all the parameters
    2. 32 pcl::SACSegmentation<pcl::PointXYZ> seg;
    3. 33 pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    4. 34 pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    5. 35 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
    6. 36 pcl::PCDWriter writer;
    7. 37 seg.setOptimizeCoefficients (true);
    8. 38 seg.setModelType (pcl::SACMODEL_PLANE);
    9. 39 seg.setMethodType (pcl::SAC_RANSAC);
    10. 40 seg.setMaxIterations (100);
    11. 41 seg.setDistanceThreshold (0.02);

    5、获取平面分割点云

    1. 43 int nr_points = (int) cloud_filtered->size ();
    2. 44 while (cloud_filtered->size () > 0.3 * nr_points)
    3. 45 {
    4. 46 // Segment the largest planar component from the remaining cloud
    5. 47 seg.setInputCloud (cloud_filtered);
    6. 48 seg.segment (*inliers, *coefficients);
    7. 49 if (inliers->indices.size () == 0)
    8. 50 {
    9. 51 std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
    10. 52 break;
    11. 53 }
    12. 54
    13. 55 // Extract the planar inliers from the input cloud
    14. 56 pcl::ExtractIndices<pcl::PointXYZ> extract;
    15. 57 extract.setInputCloud (cloud_filtered);
    16. 58 extract.setIndices (inliers);
    17. 59 extract.setNegative (false);
    18. 60
    19. 61 // Get the points associated with the planar surface
    20. 62 extract.filter (*cloud_plane);
    21. 63 std::cout << "PointCloud representing the planar component: " << cloud_plane->size () << " data points." << std::endl;
    22. 64
    23. 65 // Remove the planar inliers, extract the rest
    24. 66 extract.setNegative (true);
    25. 67 extract.filter (*cloud_f);
    26. 68 *cloud_filtered = *cloud_f;
    27. 69 }

    6、在这里,我们为提取算法的搜索方法创建了一个 KdTree 对象。

    1. 71 // Creating the KdTree object for the search method of the extraction
    2. 72 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
    3. 73 tree->setInputCloud (cloud_filtered);

    7、在这里,我们正在创建一个PointIndices 数组,其中包含vector<int>中的实际索引信息。每个检测到的集群的索引都保存在这里 - 请注意cluster_indices是一个数组,其中包含每个检测到的集群的一个 PointIndices 实例。所以 cluster_indices[0]包含我们点云中第一个簇的所有索引。

      std::vector<pcl::PointIndices> cluster_indices;

    8、因为我们的点云属于 PointXYZ 类型,我们创建了一个点类型为 PointXYZ 的 EuclideanClusterExtraction 对象。我们还设置了提取的参数和变量。小心为setClusterTolerance()设置正确的值。如果您采用非常小的值,则可能会将实际对象视为多个集群。另一方面,如果您将值设置得太高,则可能会发生多个对象 被视为一个集群的情况。因此,我们的建议是测试并尝试哪个值适合您的数据集。

    我们要求找到的集群必须至少有setMinClusterSize()点和最大setMaxClusterSize()点。

    1. pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    2. ec.setClusterTolerance (0.02); // 2cm
    3. ec.setMinClusterSize (100);
    4. ec.setMaxClusterSize (25000);
    5. ec.setSearchMethod (tree);
    6. ec.setInputCloud (cloud_filtered);
    7. ec.extract (cluster_indices);

    9、现在,我们从点云中提取聚类,并将索引保存在 cluster _ index 中。为了将每个聚类从vector< PointIndices > 中分离出来,我们必须遍历 cluster _ index,为每个条目创建一个新的 PointCloud,并在 PointCloud 中写入当前集群的所有点。

    1. int j = 0;
    2. for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
    3. {
    4. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
    5. for (const auto& idx : it->indices)
    6. cloud_cluster->push_back ((*cloud_filtered)[idx]); //*
    7. cloud_cluster->width = cloud_cluster->size ();
    8. cloud_cluster->height = 1;
    9. cloud_cluster->is_dense = true;

    编译并且运行:

    1、将以下行添加到 CMakeLists.txt

    1. 1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    2. 2
    3. 3project(cluster_extraction)
    4. 4
    5. 5find_package(PCL 1.2 REQUIRED)
    6. 6
    7. 7include_directories(${PCL_INCLUDE_DIRS})
    8. 8link_directories(${PCL_LIBRARY_DIRS})
    9. 9add_definitions(${PCL_DEFINITIONS})
    10. 10
    11. 11add_executable (cluster_extraction cluster_extraction.cpp)
    12. 12target_link_libraries (cluster_extraction ${PCL_LIBRARIES})

    2、终端运行

    $ ./cluster_extraction

    3、输出

    1. PointCloud before filtering has: 460400 data points.
    2. PointCloud after filtering has: 41049 data points.
    3. [SACSegmentation::initSACModel] Using a model of type: SACMODEL_PLANE
    4. [SACSegmentation::initSAC] Using a method of type: SAC_RANSAC with a model threshold of 0.020000
    5. [SACSegmentation::initSAC] Setting the maximum number of iterations to 100
    6. PointCloud representing the planar component: 20522 data points.
    7. [SACSegmentation::initSACModel] Using a model of type: SACMODEL_PLANE
    8. [SACSegmentation::initSAC] Using a method of type: SAC_RANSAC with a model threshold of 0.020000
    9. [SACSegmentation::initSAC] Setting the maximum number of iterations to 100
    10. PointCloud representing the planar component: 12429 data points.
    11. PointCloud representing the Cluster: 4883 data points.
    12. PointCloud representing the Cluster: 1386 data points.
    13. PointCloud representing the Cluster: 320 data points.
    14. PointCloud representing the Cluster: 290 data points.
    15. PointCloud representing the Cluster: 120 data points.

     

     

     

  • 相关阅读:
    新手如何第一次编写 “Hello World“ Windows 驱动程序 (KMDF)
    痞子衡嵌入式:MCUXpresso IDE下高度灵活的FreeMarker链接文件模板机制
    30岁的程序员,技术一般,去考公还是努力提升技术?
    抖音店铺提供优质服务|成都瀚网科技
    javaIO流04:FileOutputStream详解
    一元多项式的乘法与加法运算
    RK3568的CAN驱动适配
    SpringCloud与SpringBoot的版本对应
    【C++】类型转换
    【排序 + 优先队列 】数对组数问题
  • 原文地址:https://blog.csdn.net/m0_50046535/article/details/125491337