使用PCL库进行点云聚类并输出cluster_indices

本文介绍如何使用PCL库对点云数据进行聚类,并将聚类结果存储在std::vector<pcl::PointIndices>类型的变量cluster_indices中。

以下是一个示例代码:

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/segmentation/extract_clusters.h>

int main()
{
    // 读取点云数据
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPCDFile<pcl::PointXYZ>('input_cloud.pcd', *cloud) == -1)
    {
        std::cerr << 'Failed to read input cloud file.' << std::endl;
        return -1;
    }

    // 创建分割对象
    pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    ec.setClusterTolerance(0.02); // 设置聚类的最大距离
    ec.setMinClusterSize(100);    // 设置聚类的最小点数
    ec.setMaxClusterSize(25000);  // 设置聚类的最大点数
    ec.setInputCloud(cloud);

    // 执行聚类分割
    std::vector<pcl::PointIndices> cluster_indices;
    ec.extract(cluster_indices);

    // 输出聚类结果
    for (const auto& indices : cluster_indices)
    {
        std::cout << 'Cluster with ' << indices.indices.size() << ' points:' << std::endl;
        for (const auto& index : indices.indices)
        {
            std::cout << '  (' << cloud->points[index].x << ', '
                      << cloud->points[index].y << ', '
                      << cloud->points[index].z << ')' << std::endl;
        }
        std::cout << std::endl;
    }

    return 0;
}

代码解释:

  1. 加载点云数据:
    • 代码首先加载名为'input_cloud.pcd'的点云文件。
  2. 创建分割对象:
    • 创建一个pcl::EuclideanClusterExtraction对象,用于执行基于欧几里得距离的聚类分割。
  3. 设置聚类参数:
    • setClusterTolerance(): 设置聚类的最大距离,即两个点之间的最大距离,以便将它们视为同一聚类的成员。
    • setMinClusterSize(): 设置聚类的最小点数。
    • setMaxClusterSize(): 设置聚类的最大点数。
  4. 执行聚类分割:
    • 调用extract()方法执行聚类分割,并将结果存储在cluster_indices变量中。
  5. 输出聚类结果:
    • 遍历cluster_indices,输出每个聚类的信息,包括点数和每个点的坐标。

注意:

  • 该代码假设你已经安装了PCL库。
  • 你需要将'input_cloud.pcd'替换为你的点云文件的名称。

希望这篇博客文章能帮助你理解如何使用PCL库进行点云聚类并将结果存储在std::vector<pcl::PointIndices>类型的变量中。

使用PCL库进行点云聚类并输出cluster_indices

原文地址: https://www.cveoy.top/t/topic/fMSc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录