点云数据上的K均值聚类如何获取每个点所属的聚类号

时间:2018-07-27 10:27:12

标签: c++ k-means point-cloud-library

我对点云数据进行kmeans分析很感兴趣。我想知道如何根据kmeans分析获得每个点的聚类数。我已经阅读了类似的主题为“基于点云数据的K-means聚类”的线程,但是到目前为止,我无法使用“ pcl :: Kmeans :: ClustersToPoints”函数来确定如何获取聚类数数据。

std::string filePath = "/path/to/pointcloud/Fruit_bunch2.pcd";

pcl::PointCloud<pcl::PointXYZ>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZ>);

std::cout << "Reading file" << std::endl;

    if (pcl::io::loadPCDFile(filePath, *tempCloud) == -1) //* load the file
    {
        printf("failed file load!\n");}
    else
    {

        std::cout << "Calculating Kmeans" << std::endl;

        pcl::Kmeans mykmeans (tempCloud->points.size(), 3);
        mykmeans.setClusterSize(3);
            for (size_t i = 0; i < tempCloud->points.size(); i++)
            {
                std::vector<float> data(3);
                data[0] = tempCloud->points[i].x;
                data[1] = tempCloud->points[i].y;
                data[2] = tempCloud->points[i].z;
                mykmeans.addDataPoint(data);
            }

        mykmeans.kMeans();

        // Get the cluster centroids
        pcl::Kmeans::Centroids centroids = mykmeans.get_centroids();
        std::cout << "Points in total Cloud : " << tempCloud->points.size() << std::endl;
        std::cout << "Centroid count: " << centroids.size() << std::endl;

            for (int i = 0; i < centroids.size(); i++)
            {
                std::cout << " Centroid " << i << " output: x: " << centroids[i][0] << " ,";
                std::cout << "y: " << centroids[i][1] << " ,";
                std::cout << "z: " << centroids[i][2] << std::endl;
            }

        pcl::Kmeans::ClustersToPoints myClusters;
        //How do I get the cluster that each point belongs to ?

    }

0 个答案:

没有答案