PCL 库实现基于初始点 (xj, yj, zj) 的 KMeans 聚类 C++ 代码
以下是使用 PCL 库实现基于初始点 (xj, yj, zj) 的 KMeans 聚类 C++ 代码示例:
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/segmentation/extract_clusters.h>
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>('input.pcd', *cloud);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud);
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod(tree);
normal_estimator.setInputCloud(cloud);
normal_estimator.setKSearch(10);
normal_estimator.compute(*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(normals);
reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI);
reg.setCurvatureThreshold(1.0);
std::vector<pcl::PointIndices> clusters;
reg.extract(clusters);
for (int i = 0; i < clusters.size(); ++i)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cluster_cloud(new pcl::PointCloud<pcl::PointXYZ>);
for (int j = 0; j < clusters[i].indices.size(); ++j)
{
cluster_cloud->push_back(cloud->points[clusters[i].indices[j]]);
}
std::cout << 'Cluster ' << i + 1 << ': ' << cluster_cloud->size() << ' points' << std::endl;
}
return 0;
}
请注意,上述代码假设输入点云数据保存在名为'input.pcd' 的 PCD 文件中。您需要根据实际情况修改输入文件的路径和名称。
原文地址: http://www.cveoy.top/t/topic/fCE5 著作权归作者所有。请勿转载和采集!