PCL点云聚类及可视化C++代码详解
PCL点云聚类及可视化C++代码详解
这篇博客将介绍如何使用PCL库对点云数据进行聚类分析,并通过代码示例演示如何可视化聚类结果。
代码示例cpp#include #include <pcl/point_types.h>#include <pcl/features/normal_3d.h>#include <pcl/io/ply_io.h>#include <pcl/kdtree/kdtree.h>#include <pcl/segmentation/extract_clusters.h>#include <pcl/kdtree/kdtree_flann.h>#include <pcl/visualization/cloud_viewer.h>
using namespace pcl;
int main(){ // 读取点云数据 pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ); pcl::PointCloudpcl::PointXYZ::Ptr clouds(new pcl::PointCloudpcl::PointXYZ); pcl::io::loadPLYFilepcl::PointXYZ('E:\dianyun\09.ply', *cloud); //将骨架点都添加到原始点云内 for (const auto& point : clouds->points) { cloud->push_back(point); } //pcl::io::savePLYFileBinary('merged_cloud.ply', *cloud);
// 创建kdtree对象 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>); tree->setInputCloud(cloud);
// 设置已知点p为聚类起点 int p_index = 640; // 假设已知点p的索引为100
// 定义聚类对象 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec; ec.setClusterTolerance(0.008); // 设置聚类的容差 ec.setMinClusterSize(100); // 设置聚类的最小点数 ec.setMaxClusterSize(5000); // 设置聚类的最大点数 ec.setSearchMethod(tree); ec.setInputCloud(cloud);
// 设置聚类的种子点 pcl::PointIndices::Ptr seed_indices(new pcl::PointIndices);
seed_indices->indices.push_back(p_index); ec.setIndices(seed_indices);
// 执行聚类 std::vector<pcl::PointIndices> cluster_indices; ec.extract(cluster_indices);
// 输出聚类结果 for (const auto &indices : cluster_indices) { std::cout << 'Cluster with ' << indices.indices.size() << ' points:' << std::endl; for (const auto &index : indices.indices) { std::cout << ' (' << cloud->points[index].x << ', ' << cloud->points[index].y << ', ' << cloud->points[index].z << ')' << std::endl; } std::cout << std::endl; } // 可视化聚类结果 pcl::visualization::CloudViewer viewer('Cluster Viewer'); viewer.showCloud(cloud); while (!viewer.wasStopped()) {
}
return 0
原文地址: https://www.cveoy.top/t/topic/fDSl 著作权归作者所有。请勿转载和采集!