///'#include //n#include <pcl/io/ply_io.h>//n#include <pcl/point_types.h>//n#include <pcl/search/kdtree.h>//n#include <pcl/segmentation/extract_clusters.h>//n#include <pcl/visualization/pcl_visualizer.h>//n//nint main()//n{//n pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ);//n pcl::io::loadPLYFile(/'input.ply/', *cloud);//n//n // 定义搜索树对象//n pcl::search::KdTreepcl::PointXYZ::Ptr kdtree(new pcl::search::KdTreepcl::PointXYZ);//n kdtree->setInputCloud(cloud);//n//n // 定义聚类对象//n pcl::EuclideanClusterExtractionpcl::PointXYZ ec;//n ec.setClusterTolerance(0.02); // 设置聚类的最大距离阈值//n ec.setMinClusterSize(50); // 设置聚类的最小点数//n ec.setMaxClusterSize(5000); // 设置聚类的最大点数//n ec.setSearchMethod(kdtree);//n ec.setInputCloud(cloud);//n//n // 寻找聚类//n std::vectorpcl::PointIndices cluster_indices;//n ec.extract(cluster_indices);//n//n // 创建可视化对象//n pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(/'Cluster Viewer/'));//n//n // 设置背景颜色为黑色//n viewer->setBackgroundColor(0, 0, 0);//n//n // 遍历所有聚类//n for (std::vectorpcl::PointIndices::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)//n {//n // 创建点云对象//n pcl::PointCloudpcl::PointXYZ::Ptr cluster(new pcl::PointCloudpcl::PointXYZ);//n for (const auto& index : it->indices)//n {//n cluster->points.push_back((*cloud)[index]);//n }//n cluster->width = cluster->points.size();//n cluster->height = 1;//n cluster->is_dense = true;//n//n // 生成随机颜色//n unsigned char r = rand() % 256;//n unsigned char g = rand() % 256;//n unsigned char b = rand() % 256;//n//n // 将聚类添加到可视化对象中//n pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZ color_handler(cluster, r, g, b);//n viewer->addPointCloud(cluster, color_handler, /'cluster/' + std::to_string(it - cluster_indices.begin()));//n viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, /'cluster/' + std::to_string(it - cluster_indices.begin()));//n }//n//n // 显示可视化结果//n while (!viewer->wasStopped())//n {//n viewer->spinOnce();//n }//n//n return 0;//n}//n/

基于PCL库实现点云聚类可视化:从PLY文件输入到聚类结果展示

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

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