#include #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/io/pcd_io.h> #include <pcl/kdtree/kdtree.h> #include <pcl/segmentation/extract_clusters.h>

int main() { // 读取点云数据 pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ); pcl::io::loadPCDFilepcl::PointXYZ('input_cloud.pcd', *cloud);

// 创建kdtree对象
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud);

// 设置已知点p为聚类起点
int p_index = 100; // 假设已知点p的索引为100

// 定义聚类对象
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
ec.setClusterTolerance(0.02); // 设置聚类的容差
ec.setMinClusterSize(100); // 设置聚类的最小点数
ec.setMaxClusterSize(25000); // 设置聚类的最大点数
ec.setSearchMethod(tree);
ec.setInputCloud(cloud);

// 设置聚类的种子点
pcl::PointIndices::Ptr seed_indices(new pcl::PointIndices);
seed_indices->indices.push_back(p_index);
ec.setIndices(seed_indices);

// 执行聚类
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;

}

请注意,此代码假设已知点p的索引为100,你需要根据实际情况进行调整。此外,你需要将点云数据保存为名为'input_cloud.pcd'的PCD文件,并确保已安装并正确配置了PCL库。

PCL 1.8.1 K均值聚类算法 C++ 代码示例:以已知点为聚类起点

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

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