|
| 1 | +""" |
| 2 | +Stitch point clouds from a continuously rotating object without pre-alignment using Local Point Cloud Registration and apply Voxel Downsample. |
| 3 | +
|
| 4 | +It is assumed that the object is rotating around its own axis and the camera is stationary. |
| 5 | +The camera settings should have defined a region of interest box that removes unnecessary points, keeping only the object to be stitched. |
| 6 | +
|
| 7 | +Note: This example uses experimental SDK features, which may be modified, moved, or deleted in the future without notice. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import time |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import zivid |
| 17 | +from zivid.experimental.point_cloud_export import export_unorganized_point_cloud |
| 18 | +from zivid.experimental.point_cloud_export.file_format import PLY |
| 19 | +from zivid.experimental.toolbox.point_cloud_registration import ( |
| 20 | + LocalPointCloudRegistrationParameters, |
| 21 | + local_point_cloud_registration, |
| 22 | +) |
| 23 | +from zividsamples.display import display_pointcloud |
| 24 | + |
| 25 | + |
| 26 | +def _options() -> argparse.Namespace: |
| 27 | + """Function to read user arguments. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Arguments from user |
| 31 | +
|
| 32 | + """ |
| 33 | + parser = argparse.ArgumentParser(description=__doc__) |
| 34 | + |
| 35 | + parser.add_argument( |
| 36 | + "--settings-path", |
| 37 | + required=True, |
| 38 | + type=Path, |
| 39 | + help="Path to the camera settings YML file", |
| 40 | + ) |
| 41 | + |
| 42 | + return parser.parse_args() |
| 43 | + |
| 44 | + |
| 45 | +def _main() -> None: |
| 46 | + user_options = _options() |
| 47 | + |
| 48 | + app = zivid.Application() |
| 49 | + |
| 50 | + print("Connecting to camera") |
| 51 | + camera = app.connect_camera() |
| 52 | + |
| 53 | + settings_file = Path(user_options.settings_path) |
| 54 | + print(f"Loading settings from file: {settings_file}") |
| 55 | + settings = zivid.Settings.load(settings_file) |
| 56 | + |
| 57 | + previous_to_current_point_cloud_transform = np.eye(4) |
| 58 | + unorganized_stitched_point_cloud = zivid.UnorganizedPointCloud() |
| 59 | + registration_params = LocalPointCloudRegistrationParameters() |
| 60 | + |
| 61 | + for number_of_captures in range(20): |
| 62 | + time.sleep(1) |
| 63 | + frame = camera.capture_2d_3d(settings) |
| 64 | + unorganized_point_cloud = ( |
| 65 | + frame.point_cloud().to_unorganized_point_cloud().voxel_downsampled(voxel_size=1.0, min_points_per_voxel=2) |
| 66 | + ) |
| 67 | + |
| 68 | + if number_of_captures != 0: |
| 69 | + local_point_cloud_registration_result = local_point_cloud_registration( |
| 70 | + target=unorganized_stitched_point_cloud, |
| 71 | + source=unorganized_point_cloud, |
| 72 | + parameters=registration_params, |
| 73 | + initial_transform=previous_to_current_point_cloud_transform, |
| 74 | + ) |
| 75 | + if not local_point_cloud_registration_result.converged(): |
| 76 | + print("Registration did not converge...") |
| 77 | + continue |
| 78 | + previous_to_current_point_cloud_transform = local_point_cloud_registration_result.transform().to_matrix() |
| 79 | + |
| 80 | + unorganized_stitched_point_cloud.transform(np.linalg.inv(previous_to_current_point_cloud_transform)) |
| 81 | + unorganized_stitched_point_cloud.extend(unorganized_point_cloud) |
| 82 | + |
| 83 | + print(f"Captures done: {number_of_captures}") |
| 84 | + |
| 85 | + print("Voxel-downsampling the stitched point cloud") |
| 86 | + unorganized_stitched_point_cloud = unorganized_stitched_point_cloud.voxel_downsampled( |
| 87 | + voxel_size=0.75, min_points_per_voxel=2 |
| 88 | + ) |
| 89 | + |
| 90 | + display_pointcloud( |
| 91 | + xyz=unorganized_stitched_point_cloud.copy_data("xyz"), |
| 92 | + rgb=unorganized_stitched_point_cloud.copy_data("rgba")[:, 0:3], |
| 93 | + ) |
| 94 | + |
| 95 | + file_name = Path(__file__).parent / "StitchedPointCloudOfRotatingObject.ply" |
| 96 | + export_unorganized_point_cloud(unorganized_stitched_point_cloud, PLY(str(file_name), layout=PLY.Layout.unordered)) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + _main() |
0 commit comments