| 1 | import logging |
| 2 | import numpy as np |
| 3 | import open3d as o3d |
| 4 | |
| 5 | log = logging.getLogger(__name__) |
| 6 | |
| 7 | |
| 8 | # Load a point cloud from a file |
| 9 | def load_o3d_pcd(file_path: str): |
| 10 | return o3d.io.read_point_cloud(file_path) |
| 11 | |
| 12 | |
| 13 | # Get points and colors from a Open3D point cloud |
| 14 | def get_points_and_colors(pcd: o3d.geometry.PointCloud): |
| 15 | points = np.asarray(pcd.points) |
| 16 | colors = np.zeros_like(points, dtype=np.uint8) |
| 17 | if pcd.has_colors(): |
| 18 | colors = np.asarray(pcd.colors) |
| 19 | if colors.shape[1] == 4: |
| 20 | colors = colors[:, :3] |
| 21 | if colors.max() < 1.1: |
| 22 | colors = (colors * 255).astype(np.uint8) |
| 23 | return points, colors |
| 24 | |
| 25 | |
| 26 | # Preprocess a point cloud |
| 27 | def cleanup_pcd( |
| 28 | pcd: o3d.geometry.PointCloud, |
| 29 | voxel_size: float, |
| 30 | num_nb: int = 15, |
| 31 | std_ratio: float = 2.0, |
| 32 | ): |
| 33 | # voxelize the point cloud |
| 34 | pcd = pcd.voxel_down_sample(voxel_size) |
| 35 | # remove outliers |
| 36 | pcd, _ = pcd.remove_statistical_outlier(num_nb, std_ratio=std_ratio) |
| 37 | return pcd |