{"title": "#include \n#include <pcl/io/ply_io.h>\n#include <pcl/point_types.h>\n#include <pcl/common/distances.h>\n#include <pcl/kdtree/kdtree_flann.h>\n#include <pcl/segmentation/extract_clusters.h>\n#include <pcl/visualization/pcl_visualizer.h>\n\ntypedef pcl::PointXYZ PointT;\ntypedef pcl::PointCloud PointCloudT;\n\n// Function to calculate distance between two points\ndouble calculateDistance(const PointT& p1, const PointT& p2) {\n return pcl::euclideanDistance(p1, p2);\n}\n\n// Function to perform 2-means clustering\nvoid twoMeansClustering(PointCloudT::Ptr cloud, PointCloudT::Ptr cluster1, PointCloudT::Ptr cluster2) {\n pcl::KdTreeFLANN kdtree;\n kdtree.setInputCloud(cloud);\n\n std::vector cluster1Indices, cluster2Indices;\n std::vector cluster1Distances, cluster2Distances;\n\n // Find the nearest neighbor for each point and assign it to the closest cluster\n for (int i = 0; i < cloud->size(); i++) {\n std::vector nearestIndices(1);\n std::vector nearestDistances(1);\n kdtree.nearestKSearch(cloud->at(i), 1, nearestIndices, nearestDistances);\n\n if (cluster1Indices.empty() || nearestDistances[0] < nearestDistances[1]) {\n cluster1Indices.push_back(i);\n cluster1Distances.push_back(nearestDistances[0]);\n } else {\n cluster2Indices.push_back(i);\n cluster2Distances.push_back(nearestDistances[1]);\n }\n }\n\n // Extract the points for each cluster\n pcl::copyPointCloud(*cloud, cluster1Indices, *cluster1);\n pcl::copyPointCloud(*cloud, cluster2Indices, *cluster2);\n}\n\nint main() {\n std::string inputPath = "D:\DIANYUNWENJIANJIA\kruskal_ply.ply";\n std::string outputPath = "D:\DIANYUNWENJIANJIA\2MEANS_ply.ply";\n\n // Load input point cloud\n PointCloudT::Ptr cloud(new PointCloudT);\n pcl::PLYReader reader;\n reader.read(inputPath, *cloud);\n\n // Perform 2-means clustering\n PointCloudT::Ptr cluster1(new PointCloudT);\n PointCloudT::Ptr cluster2(new PointCloudT);\n twoMeansClustering(cloud, cluster1, cluster2);\n\n // Visualization\n pcl::visualization::PCLVisualizer viewer("2-Means Clustering");\n viewer.setBackgroundColor(0, 0, 0);\n viewer.addPointCloudpcl::PointXYZ(cloud, "cloud");\n viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");\n viewer.addPointCloudpcl::PointXYZ(cluster1, pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZ(cluster1, 255, 0, 0), "cluster1");\n viewer.addPointCloudpcl::PointXYZ(cluster2, pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZ(cluster2, 0, 255, 0), "cluster2");\n viewer.spin();\n\n // Keep the visualization window open\n while (!viewer.wasStopped())\n {\n viewer.spinOnce();\n }\n\n // Save output point cloud\n pcl::PLYWriter writer;\n writer.write(outputPath, *cloud);\n\n return 0;\n}\n\n这段代码在运行时会出现\n出现报错\n\t_STL_VERIFY(_Pos < size(), "vector subscript out of range");已触发了一个断点\n该如何解决\n内容:这个错误通常表示访问了一个超出向量范围的索引,导致程序崩溃。\n\n在你的代码中,出现这个错误的原因可能是在拷贝点云数据到两个聚类点云(cluster1和cluster2)时,使用了错误的索引。你可以尝试使用以下代码来修复问题:\n\ncpp\n// Extract the points for each cluster\npcl::copyPointCloud(*cloud, cluster1Indices, *cluster1);\npcl::copyPointCloud(*cloud, cluster2Indices, *cluster2);\n\n\n你还可以添加一些调试信息来确定错误的原因,例如在出错的地方打印索引值,或使用断点调试器逐步执行代码。\n\n另外,确保你的输入点云文件路径是正确的,并且你有读取和写入该文件的权限。\n\n希望这些信息能帮助你解决问题。如果问题仍然存在,请提供更多错误信息或调试信息以供进一步分析。\n

PCL 2-Means 聚类算法实现:代码详解和错误解决

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

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