import open3d as o3d
def display_inlier_outlier(cloud, ind):
inlier_cloud = cloud.select_by_index(ind)
outlier_cloud = cloud.select_by_index(ind, invert=True)
print("Showing outliers (red) and inliers (gray): ")
outlier_cloud.paint_uniform_color([1, 0, 0])
inlier_cloud.paint_uniform_color([0.8, 0.8, 0.8])
o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud], window_name='Open3D Removal Outlier', width=1920,
height=1080, left=50, top=50, point_show_normal=False, mesh_show_wireframe=False,
mesh_show_back_face=False)
pcd = o3d.io.read_point_cloud("D:/pcd/milk_cartoon_all_small_clorox.pcd")
print(pcd)
o3d.visualization.draw_geometries([pcd], window_name='Open3D Origin', width=1920, height=1080, left=50, top=50,
point_show_normal=False, mesh_show_wireframe=False, mesh_show_back_face=False)
print("Downsample the point cloud with a voxel of 0.003")
downpcd = pcd.voxel_down_sample(voxel_size=0.003)
print(downpcd)
o3d.visualization.draw_geometries([downpcd], window_name='Open3D downSample', width=1920, height=1080, left=50, top=50,
point_show_normal=False, mesh_show_wireframe=False, mesh_show_back_face=False)
print("Recompute the normal of the downsampled point cloud")
downpcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
o3d.visualization.draw_geometries([downpcd], window_name='Open3D downSample Normals', width=1920, height=1080, left=50,
top=50, point_show_normal=True, mesh_show_wireframe=False, mesh_show_back_face=False)
print("Statistical oulier removal")
cl, ind = downpcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0)
display_inlier_outlier(downpcd, ind)
downpcd_inlier_cloud = downpcd.select_by_index(ind)
print(downpcd_inlier_cloud)
plane_model, inliers = downpcd_inlier_cloud.segment_plane(distance_threshold=0.01,
ransac_n=5,
num_iterations=10000)
[a, b, c, d] = plane_model
print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0")
inlier_cloud = downpcd_inlier_cloud.select_by_index(inliers)
print('----inlier_cloud: ', inlier_cloud.points)
inlier_cloud.paint_uniform_color([1.0, 0, 0])
outlier_cloud = downpcd_inlier_cloud.select_by_index(inliers, invert=True)
print('----outlier_cloud: ', outlier_cloud.points)
o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud], window_name='Open3D Plane Model', width=1920,
height=1080, left=50, top=50, point_show_normal=False, mesh_show_wireframe=False,
mesh_show_back_face=False)
o3d.io.write_point_cloud("D:/pcd/1001140020191217_las2pcd_cx_g.pcd", inlier_cloud)