按照官网的例子使用聚类,发现结果是全黑的。
经过多次测试发现 eps=3.3, min_points=1这里是关键
min_points必须等于1否则无效果
import time
import open3d as o3d;
import numpy as np;
import matplotlib.pyplot as plt#坐标
mesh_coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=2.225, origin=[0, 0, 0])
#mesh_coord_frame = mesh_coord_frame.translate((0.16, 0.15, 0)) #加载点云数据
ply = o3d.io.read_point_cloud("source/Foam1.ply") # downply = ply.voxel_down_sample(voxel_size=0.103)
# o3d.visualization.draw_geometries([ downply],window_name="downply")
#去除无效部分
plane_model, inliers = ply.segment_plane(distance_threshold=1.6,ransac_n=3,num_iterations=1000)[a, b, c, d] = plane_model
print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0")inlier_cloud = ply.select_by_index(inliers) pcd = ply.select_by_index(inliers, invert=True)
o3d.visualization.draw_geometries([ inlier_cloud],window_name="3D海绵点云无效数据")
o3d.visualization.draw_geometries([ pcd],window_name="3D海绵点云有效数据") # 使用聚类算法
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:labels = np.array(pcd.cluster_dbscan(eps=3.3, min_points=1, print_progress=True))print(labels)
# 求点云的聚类数量
max_label = labels.max()
print(f"point cloud has {max_label + 1} clusters")
# 可视化
colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
colors[labels < 0] = 0
pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
o3d.visualization.draw_geometries([ pcd,mesh_coord_frame],window_name="3D海绵聚类")
点云图
使用聚类后效果图