以下是一个基于1.8.1pcl库的C++算法示例,将欧式聚类改写为以已知点p为聚类起点,并将聚类结果可视化。输入文件为ply文件。

#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/normal_3d.h>
#include <pcl/segmentation/region_growing.h>

int main(int argc, char** argv)
{
    // 加载点云数据
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PLYReader reader;
    reader.read(argv[1], *cloud);

    // 创建法线估计对象
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
    ne.setInputCloud(cloud);

    // 创建KdTree对象
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
    ne.setSearchMethod(tree);

    // 计算法线
    pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
    ne.setKSearch(20);
    ne.compute(*cloud_normals);

    // 创建区域增长对象
    pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
    reg.setMinClusterSize(50);
    reg.setMaxClusterSize(1000000);
    reg.setSearchMethod(tree);
    reg.setNumberOfNeighbours(30);
    reg.setInputCloud(cloud);
    reg.setInputNormals(cloud_normals);

    // 设置聚类起点
    pcl::PointXYZ p;
    p.x = 0.0;  // 设置起点x坐标
    p.y = 0.0;  // 设置起点y坐标
    p.z = 0.0;  // 设置起点z坐标
    std::vector<int> indices;
    indices.push_back(cloud->size() - 1);  // 将起点索引添加到索引向量中
    reg.setIndices(indices);

    // 执行聚类
    std::vector<pcl::PointIndices> clusters;
    reg.extract(clusters);

    // 可视化聚类结果
    pcl::visualization::PCLVisualizer viewer("Cluster viewer");
    viewer.setBackgroundColor(0, 0, 0);
    viewer.addPointCloud<pcl::PointXYZ>(cloud, "cloud");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");

    int cluster_id = 0;
    for (pcl::PointIndices cluster : clusters)
    {
        pcl::PointCloud<pcl::PointXYZ>::Ptr cluster_cloud(new pcl::PointCloud<pcl::PointXYZ>);
        for (int index : cluster.indices)
        {
            cluster_cloud->push_back((*cloud)[index]);
        }

        std::string cluster_name = "cluster_" + std::to_string(cluster_id);
        viewer.addPointCloud<pcl::PointXYZ>(cluster_cloud, cluster_name);
        viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, rand() % 256 / 255.0, rand() % 256 / 255.0, rand() % 256 / 255.0, cluster_name);
        cluster_id++;
    }

    viewer.spin();

    return 0;
}

请确保已安装pcl库,并将输入ply文件的路径作为命令行参数传递给程序。程序将在可视化窗口中显示聚类结果。

基于181pcl库将欧式聚类改写为以已知点p为聚类起点并将聚类结果可视化的c++算法输入文件为ply文件

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

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