{ "title": "#include \n#include <pcl/io/ply_io.h>\n#include <pcl/point_types.h>\n#include <pcl/kdtree/kdtree_flann.h>\n#include <pcl/visualization/cloud_viewer.h>\n#include <pcl/point_cloud.h>\n#include <pcl/common/common.h>\n\ntypedef pcl::PointXYZ PointT;\ntypedef pcl::PointCloud PointCloudT;\n\n// Function to calculate the distance between two points\ndouble calculateDistance(const PointT& p1, const PointT& p2) {\n\treturn std::sqrt((p1.x - p2.x) * (p1.x - p2.x) +\n\t\t(p1.y - p2.y) * (p1.y - p2.y) +\n\t\t(p1.z - p2.z) * (p1.z - p2.z));\n}\n\n// Function to perform 2-means clustering\nvoid twoMeansClustering(PointCloudT::Ptr cloud, PointCloudT::Ptr cluster1, PointCloudT::Ptr cluster2) {\n\t// Create KD-Tree\n\tpcl::KdTreeFLANN kdtree;\n\tkdtree.setInputCloud(cloud);\n\n\t// Randomly choose two initial centers\n\tint centerIdx1 = rand() % cloud->size();\n\tint centerIdx2 = rand() % cloud->size();\n\tPointT center1 = cloud->at(centerIdx1);\n\tPointT center2 = cloud->at(centerIdx2);\n\n\twhile (true) {\n\t\t// Assign each point to the closest center\n\t\tstd::vector cluster1Indices, cluster2Indices;\n\t\tfor (int i = 0; i < cloud->size(); i++) {\n\t\t\tdouble distance1 = calculateDistance(cloud->at(i), center1);\n\t\t\tdouble distance2 = calculateDistance(cloud->at(i), center2);\n\t\t\tif (distance1 < distance2) {\n\t\t\t\tcluster1Indices.push_back(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcluster2Indices.push_back(i);\n\t\t\t}\n\t\t}\n\n\t\t// Update the centers\n\t\tPointT newCenter1, newCenter2;\n\t\tfor (const auto& idx : cluster1Indices) {\n\t\t\tnewCenter1.x += cloud->at(idx).x;\n\t\t\tnewCenter1.y += cloud->at(idx).y;\n\t\t\tnewCenter1.z += cloud->at(idx).z;\n\t\t}\n\t\tfor (const auto& idx : cluster2Indices) {\n\t\t\tnewCenter2.x += cloud->at(idx).x;\n\t\t\tnewCenter2.y += cloud->at(idx).y;\n\t\t\tnewCenter2.z += cloud->at(idx).z;\n\t\t}\n\t\tnewCenter1.x /= cluster1Indices.size();\n\t\tnewCenter1.y /= cluster1Indices.size();\n\t\tnewCenter1.z /= cluster1Indices.size();\n\t\tnewCenter2.x /= cluster2Indices.size();\n\t\tnewCenter2.y /= cluster2Indices.size();\n\t\tnewCenter2.z /= cluster2Indices.size();\n\n\t\t// Check if the centers have converged\n\t\tdouble centerDistance = calculateDistance(newCenter1, center1) + calculateDistance(newCenter2, center2);\n\t\tif (centerDistance < 0.001)\n\t\t\tbreak;\n\n\t\tcenter1 = newCenter1;\n\t\tcenter2 = newCenter2;\n\t}\n\n\t// Extract the points for each cluster\n\tpcl::copyPointCloud(*cloud, pcl::Indices(cluster1Indices), *cluster1);\n\tpcl::copyPointCloud(*cloud, pcl::Indices(cluster2Indices), *cluster2);\n}\n\nint main() {\n\tstd::string inputPath = "D:\DIANYUNWENJIANJIA\kruskal_ply.ply";\n\tstd::string outputPath = "D:\DIANYUNWENJIANJIA\2MEANS_ply.ply";\n\n\t// Load the input point cloud\n\tPointCloudT::Ptr cloud(new PointCloudT);\n\tpcl::io::loadPLYFile(inputPath, *cloud);\n\n\t// Perform 2-means clustering\n\tPointCloudT::Ptr cluster1(new PointCloudT);\n\tPointCloudT::Ptr cluster2(new PointCloudT);\n\ttwoMeansClustering(cloud, cluster1, cluster2);\n\n\t// Visualization\n\tpcl::visualization::PCLVisualizer viewer("2-Means Clustering");\n\tviewer.setBackgroundColor(0, 0, 0);\n\tviewer.addPointCloud(cloud, "cloud");\n\tviewer.addPointCloud(cluster1, pcl::visualization::PointCloudColorHandlerCustom(cluster1, 255, 0, 0), "cluster1");\n\tviewer.addPointCloud(cluster2, pcl::visualization::PointCloudColorHandlerCustom(cluster2, 0, 255, 0), "cluster2");\n\tviewer.spin();\n\n\t// Save the clustered point cloud\n\tpcl::PLYWriter writer;\n\twriter.write(outputPath, *cloud);\n\n\treturn 0;\n}\n

PCL 1.8.1 2-Means 聚类:解决

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

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