代码之家  ›  专栏  ›  技术社区  ›  Giant Cloud

点云数据的K均值聚类

  •  0
  • Giant Cloud  · 技术社区  · 7 年前

    我试图在点云上实现K均值聚类算法。然而,我不确定如何导入数据作为pcl的k-means成员的输入。事实证明,这些文档有点令人困惑。 到目前为止,我已经将pcd导入点云并将其转换为向量,但我不知道如何从这里开始直接初始化Kmeans。

    int main (int argc, char** argv)
    { 
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);
    
        std::vector<pcl::PointXYZ> cloud;
        pcl::io::loadPCDFile ("Scene02 - Cloud.pcd", *cloud_in);
    
        for (int i = 0; i < cloud_in->size(); i++)
        {
            cloud[i] = cloud_in->points[i];
        }
    
        pcl::Kmeans real(300000, 3); 
        real.setInputData(cloud);
    
    
    
     }
    

    我意识到语法是错误的,但我也不确定正确的语法是什么。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sneaky Polar Bear    7 年前

    与pcl通常的工作方式(以自定义点类型为中心)相比,此函数非常奇怪。基本上,奇怪的是,您必须通过指定的维度向量而不是自定义点类型输入点。下面是经过测试的功能示例代码:(显然,您需要提供自己的文件名,并且您可能需要调整集群大小)

    int main(int argc, char** argv) {
    
        std::string filePath = "../PointCloudFiles/beaconJR.pcd";
        pcl::PointCloud<pcl::PointXYZ>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZ>);
        if (pcl::io::loadPCDFile(filePath, *tempCloud) == -1) //* load the file
        {printf("failed file load!\n");}
        else
        {
            pcl::Kmeans real(static_cast<int> (tempCloud->points.size()), 3);
            real.setClusterSize(3); //it is important that you set this term appropriately for your application
            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;
                real.addDataPoint(data);
            }
    
            real.kMeans();
            // get the cluster centroids 
            pcl::Kmeans::Centroids centroids = real.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 << i << "_cent output: x: " << centroids[i][0] << " ,";
                std::cout << "y: " << centroids[i][1] << " ,";
                std::cout << "z: " << centroids[i][2] << std::endl;
            }
        }
    
        std::cin.get();
        std::cin.get();
    }
    

    --编辑