feat: initial commit - massive-cloud point cloud processing system
This commit is contained in:
47
alg/point_porj/archive/relative_code/Eucilid.py
Normal file
47
alg/point_porj/archive/relative_code/Eucilid.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import open3d as o3d
|
||||
import numpy as np
|
||||
|
||||
|
||||
def euclidean_clustering_open3d(pcd, radius=0.1, min_cluster_size=50, max_cluster_size=10000):
|
||||
"""
|
||||
使用 Open3D 进行欧几里得聚类
|
||||
Args:
|
||||
pcd: Open3D 点云对象
|
||||
radius: 聚类半径
|
||||
min_cluster_size: 最小簇大小
|
||||
max_cluster_size: 最大簇大小
|
||||
Returns:
|
||||
labels: 每个点的簇标签(-1 表示噪声/无效簇)
|
||||
clusters: 各簇的点索引列表
|
||||
"""
|
||||
# 欧几里得聚类
|
||||
labels = np.array(pcd.cluster_dbscan(eps=radius, min_points=min_cluster_size))
|
||||
|
||||
# 统计簇信息
|
||||
unique_labels = np.unique(labels)
|
||||
clusters = []
|
||||
|
||||
for label in unique_labels:
|
||||
if label == -1: # -1 表示噪声点
|
||||
continue
|
||||
cluster_indices = np.where(labels == label)[0]
|
||||
if min_cluster_size <= len(cluster_indices) <= max_cluster_size:
|
||||
clusters.append(cluster_indices)
|
||||
|
||||
return labels, clusters
|
||||
|
||||
|
||||
# 使用示例
|
||||
pcd = o3d.io.read_point_cloud("E:\point_distance\data\part_car_left_side.ply")
|
||||
labels, clusters = euclidean_clustering_open3d(pcd, radius=4, min_cluster_size=100)
|
||||
|
||||
print(f"找到 {len(clusters)} 个聚类")
|
||||
|
||||
# 为每个聚类着色
|
||||
colors = np.random.rand(len(clusters), 3)
|
||||
colored_points = np.zeros((len(pcd.points), 3))
|
||||
for i, cluster in enumerate(clusters):
|
||||
colored_points[cluster] = colors[i]
|
||||
|
||||
pcd.colors = o3d.utility.Vector3dVector(colored_points)
|
||||
o3d.visualization.draw_geometries([pcd])
|
||||
Reference in New Issue
Block a user