#include #include <pcl/io/ply_io.h> #include <pcl/point_types.h> #include <pcl/kdtree/kdtree_flann.h> #include <pcl/visualization/cloud_viewer.h> #include <pcl/point_cloud.h> #include <pcl/common/common.h>

typedef pcl::PointXYZ PointT; typedef pcl::PointCloud PointCloudT;

// 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 kdtree; kdtree.setInputCloud(cloud);

// 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;

}

PCL 1.8.1 2-Means 聚类实现:点云数据处理与可视化