#include #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/io/ply_io.h> #include <pcl/kdtree/kdtree.h> #include <pcl/segmentation/extract_clusters.h> #include <pcl/kdtree/kdtree_flann.h> #include <pcl/visualization/cloud_viewer.h>

int main() { // 读取点云数据 pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ); pcl::PointCloudpcl::PointXYZ::Ptr clouds(new pcl::PointCloudpcl::PointXYZ); pcl::io::loadPLYFilepcl::PointXYZ("E:\dianyun\09 - Cloud.ply", *cloud); //将骨架点都添加到原始点云内 for (const auto& point : clouds->points) { cloud->push_back(point); } //pcl::io::savePLYFileBinary("merged_cloud.ply", *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.005); // 设置聚类的容差
ec.setMinClusterSize(100); // 设置聚类的最小点数
ec.setMaxClusterSize(5000); // 设置聚类的最大点数
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;
}

// 可视化聚类结果
pcl::visualization::CloudViewer viewer("Cluster Viewer");
viewer.showCloud(cloud);
while (!viewer.wasStopped())
{
}

return 0;

}

改写下面代码使得到的聚类结果可视化的c++代码#include iostream#include pclpoint_typesh#include pclfeaturesnormal_3dh#include pclioply_ioh#include pclkdtreekdtreeh#include pclsegmentationextract_clustersh#include pclkdtreek

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

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