平面分割(基于RANSAC)
使用RANSAC算法从点云中拟合平面;
接口:segment_plane
测试:Plane-segmentation
import open3d as o3d
pcd_point_cloud = o3d.data.PCDPointCloud()
pcd = o3d.io.read_point_cloud(pcd_point_cloud.path)
plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
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 = pcd.select_by_index(inliers)
inlier_cloud.paint_uniform_color([1.0, 0, 0])
outlier_cloud = pcd.select_by_index(inliers, invert=True)
o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud],
zoom=0.8,
front=[-0.4999, -0.1659, -0.8499],
lookat=[2.1813, 2.0619, 2.0999],
up=[0.1204, -0.9852, 0.1215])
基于聚类的分割(基于DBSCAN)
基于密度的聚类算法,根据点周围的密度,将点进行聚类划分;
接口:
cluster_dbscan
测试:
DBSCAN-clustering
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
ply_point_cloud = o3d.data.PLYPointCloud()
pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
with o3d.utility.VerbosityContextManager(
o3d.utility.VerbosityLevel.Debug) as cm:
labels = np.array(
pcd.cluster_dbscan(eps=0.02, min_points=10, print_progress=True))
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],
zoom=0.455,
front=[-0.4999, -0.1659, -0.8499],
lookat=[2.1813, 2.0619, 2.0999],
up=[0.1204, -0.9852, 0.1215])
# #分离出每个聚类的点云
# min_label = labels.min()
# pcList =[]
# for i in range(min_label,max_label+1):
# label_index=np.where(labels == i)
# temp_pc=pcd.select_by_index(np.array(label_index)[0])
# pcList.append(temp_pc)
# #o3d.visualization.draw_geometries([temp_pc])
# o3d.io.write_point_cloud(str(i)+".pcd",temp_pc)