完善后的代码如下所示:

for (int j = 0; j < clouds[i]->size(); j++)
{
    // 以点j为中心进行聚类(xj, yj, zj),设置半径为r,进行欧式聚类,这里会得到骨架点个数的簇,所以也要创建指针容器
    pcl::PointCloud<pcl::PointXYZ>::Ptr yq = yenn[j];
    pcl::PointXYZ centroid1;
    centroid1.x = clouds[i]->points[j].x;
    centroid1.y = clouds[i]->points[j].y;
    centroid1.z = clouds[i]->points[j].z;

    pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    ec.setClusterTolerance(r); // 设置聚类的半径
    ec.setMinClusterSize(1); // 设置聚类的最小点数
    ec.setMaxClusterSize(clouds[i]->size()); // 设置聚类的最大点数
    ec.setSearchMethod(tree);
    ec.setInputCloud(clouds[i]);
    ec.setIndices(yq);
    std::vector<pcl::PointIndices> clusterIndices;
    ec.extract(clusterIndices);

    // Output the indices of the clusters
    for (const auto& indices : clusterIndices) // 遍历储存的所有类的索引
    {
        pcl::PointCloud<pcl::PointXYZ>::Ptr cluster7(new pcl::PointCloud<pcl::PointXYZ>);
        for (const auto& index : indices.indices) // 循环遍历一个类里所有点的索引将其加入点云指针
        {
            cluster7->push_back(clouds[i]->points[index]); // 将索引对应的点加入到这个类的指针中
        }
        yemm.push_back(cluster7); // 得到的一个类通过尾插法将其指针加入指针容器yenn,所以一片叶子的多个指针表示为clusters[i]
    }
}

其中,pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;用于进行欧式聚类,ec.setClusterTolerance(r);设置聚类的半径,ec.setMinClusterSize(1);设置聚类的最小点数,ec.setMaxClusterSize(clouds[i]->size());设置聚类的最大点数,ec.setSearchMethod(tree);设置搜索方法,ec.setInputCloud(clouds[i]);设置输入点云,ec.setIndices(yq);设置起点为centroid1。聚类结果存储在std::vector<pcl::PointIndices> clusterIndices;中。

完善下面代码使得对点云ply文件进行欧式聚类时的起点为centroid1并将聚类索引结果保存到 clusterIndices内的c++代码for int j = 0; j cloudsi-size; j++以点j为中心进行聚类xj yj zj设置半径为r进行欧式聚类这里会得到骨架点个数的簇所以也要创建指针容器	pclPointCloudpclPointXYZPtr yq = yennj;	pcl

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

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