PCL 1.8.1 2-Means 聚类实现:点云数据处理与可视化
#include
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud
// Function to calculate the distance between two points double calculateDistance(const PointT& p1, const PointT& p2) { return std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z)); }
// Function to perform 2-means clustering
void twoMeansClustering(PointCloudT::Ptr cloud, PointCloudT::Ptr cluster1, PointCloudT::Ptr cluster2) {
// Create KD-Tree
pcl::KdTreeFLANN
// Randomly choose two initial centers
int centerIdx1 = rand() % cloud->size();
int centerIdx2 = rand() % cloud->size();
PointT center1 = cloud->at(centerIdx1);
PointT center2 = cloud->at(centerIdx2);
while (true) {
// Assign each point to the closest center
std::vector<pcl::PointXYZ> cluster1Points, cluster2Points;
for (int i = 0; i < cloud->size(); i++) {
double distance1 = calculateDistance(cloud->at(i), center1);
double distance2 = calculateDistance(cloud->at(i), center2);
if (distance1 < distance2) {
cluster1Points.push_back(cloud->at(i));
}
else {
cluster2Points.push_back(cloud->at(i));
}
}
// Update the centers
PointT newCenter1, newCenter2;
for (const auto& point : cluster1Points) {
newCenter1.x += point.x;
newCenter1.y += point.y;
newCenter1.z += point.z;
}
for (const auto& point : cluster2Points) {
newCenter2.x += point.x;
newCenter2.y += point.y;
newCenter2.z += point.z;
}
newCenter1.x /= cluster1Points.size();
newCenter1.y /= cluster1Points.size();
newCenter1.z /= cluster1Points.size();
newCenter2.x /= cluster2Points.size();
newCenter2.y /= cluster2Points.size();
newCenter2.z /= cluster2Points.size();
// Check if the centers have converged
double centerDistance = calculateDistance(newCenter1, center1) + calculateDistance(newCenter2, center2);
if (centerDistance < 0.001)
break;
center1 = newCenter1;
center2 = newCenter2;
}
// Extract the points for each cluster
pcl::copyPointCloud(*cluster1Points.makeShared(), *cluster1);
pcl::copyPointCloud(*cluster2Points.makeShared(), *cluster2);
}
int main() { std::string inputPath = "D:\DIANYUNWENJIANJIA\kruskal_ply.ply"; std::string outputPath = "D:\DIANYUNWENJIANJIA\2MEANS_ply.ply";
// Load the input point cloud
PointCloudT::Ptr cloud(new PointCloudT);
pcl::io::loadPLYFile<PointT>(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<PointT>(cloud, "cloud");
viewer.addPointCloud<PointT>(cluster1, pcl::visualization::PointCloudColorHandlerCustom<PointT>(cluster1, 255, 0, 0), "cluster1_handler");
viewer.addPointCloud<PointT>(cluster2, pcl::visualization::PointCloudColorHandlerCustom<PointT>(cluster2, 0, 255, 0), "cluster2_handler");
viewer.spin();
// Save the clustered point cloud
pcl::io::savePLYFileASCII(outputPath, *cloud);
return 0;
}
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pz01 著作权归作者所有。请勿转载和采集!