PCL 2-Means 聚类:代码解析及常见错误解决方案
{ "title": "PCL 2-Means 聚类:代码解析及常见错误解决方案", "description": "本文详细介绍了使用 PCL 库进行 2-Means 聚类算法的代码示例,并分析了常见错误“将一个无效参数传递给了将无效参数视为严重错误的函数”的解决方法。", "keywords": "PCL, 2-Means 聚类, 点云处理, 代码示例, 错误解决, 文件路径, 权限", "content": "#include "iostream" #include "pcl/io/ply_io.h" #include "pcl/point_types.h" #include "pcl/common/distances.h" #include "pcl/kdtree/kdtree_flann.h" #include "pcl/segmentation/extract_clusters.h" #include "pcl/visualization/pcl_visualizer.h"
tydef pcl::PointXYZ PointT;
tydef pcl::PointCloud
// Function to calculate distance between two points double calculateDistance(const PointT& p1, const PointT& p2) { return pcl::euclideanDistance(p1, p2); }
// Function to perform 2-means clustering
void twoMeansClustering(PointCloudT::Ptr cloud, PointCloudT::Ptr cluster1, PointCloudT::Ptr cluster2) {
pcl::KdTreeFLANN
std::vector<int> cluster1Indices, cluster2Indices;
std::vector<float> cluster1Distances, cluster2Distances;
// Find the nearest neighbor for each point and assign it to the closest cluster
for (int i = 0; i < cloud->size(); i++) {
std::vector<int> nearestIndices(1);
std::vector<float> nearestDistances(1);
kdtree.nearestKSearch(cloud->at(i), 1, nearestIndices, nearestDistances);
if (cluster1Indices.empty() || nearestDistances[0] < nearestDistances[1]) {
cluster1Indices.push_back(i);
cluster1Distances.push_back(nearestDistances[0]);
}
else {
cluster2Indices.push_back(i);
cluster2Distances.push_back(nearestDistances[1]);
}
}
// Extract the points for each cluster
pcl::copyPointCloud(*cloud, cluster1Indices, *cluster1);
pcl::copyPointCloud(*cloud, cluster2Indices, *cluster2);
}
int main() { std::string inputPath = "D:\DIANYUNWENJIANJIA\kruskal_ply.ply"; std::string outputPath = "D:\DIANYUNWENJIANJIA\2MEANS_ply.ply";
// Load input point cloud
PointCloudT::Ptr cloud(new PointCloudT);
pcl::PLYReader reader;
reader.read(inputPath, *cloud);
// Perform 2-means clustering
PointCloudT::Ptr cluster1(new PointCloudT);
PointCloudT::Ptr cluster2(new PointCloudT);
twoMeansClustering(cloud, cluster1, cluster2);
// Visualization
pcl::visualization::PCLVisualizer viewer("2-Means Clustering");
viewer.setBackgroundColor(0, 0, 0);
viewer.addPointCloud<pcl::PointXYZ>(cloud, "cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer.addPointCloud<pcl::PointXYZ>(cluster1, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cluster1, 255, 0, 0), "cluster1");
viewer.addPointCloud<pcl::PointXYZ>(cluster2, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cluster2, 0, 255, 0), "cluster2");
viewer.spin();
// Keep the visualization window open
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
// Save output point cloud
pcl::PLYWriter writer;
writer.write(outputPath, *cloud);
return 0;
}
这个错误通常是由于文件路径错误导致的。请确保输入和输出的文件路径是正确的,并且文件存在。另外,还要确保你有权限读取和写入这些文件。
解决方法:
- 仔细检查输入和输出文件路径,确保它们是正确的,并且文件存在。
- 确保你有权限读取和写入这些文件。如果文件位于系统目录,你可能需要以管理员身份运行代码。
- 在代码中添加一些调试信息,例如打印输入和输出文件路径,以帮助你检查文件路径是否正确。
如果以上方法都无法解决问题,请提供以下信息以帮助我们进一步排查问题:
- 操作系统版本
- PCL 库版本
- 你的代码
- 错误消息的完整内容
原文地址: https://www.cveoy.top/t/topic/pzTN 著作权归作者所有。请勿转载和采集!