| 1 |
""" |
| 2 |
.. _icp_registration_example: |
| 3 |
|
| 4 |
Register a Surface with ICP |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Recover the rigid transform between two surfaces with the iterative closest |
| 8 |
point implementation behind :func:`pyvista.DataSetFilters.align`. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# sphinx_gallery_thumbnail_number = 2 |
| 16 |
|
| 17 |
# %% |
| 18 |
# Load a reference surface |
| 19 |
# ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 20 |
# The :func:`~pyvista.examples.downloads.download_action_figure` scan is an |
| 21 |
# asymmetric reference mesh. |
| 22 |
|
| 23 |
target = examples.download_action_figure() |
| 24 |
target |
| 25 |
|
| 26 |
# %% |
| 27 |
# A camera position that frames the figure clearly. |
| 28 |
|
| 29 |
cpos = pv.CameraPosition( |
| 30 |
position=(150, -250, 150), |
| 31 |
focal_point=(0, 0, 35), |
| 32 |
viewup=(0, 0, 1), |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
# %% |
| 37 |
# Transform a copy away from the reference |
| 38 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 39 |
# The transformed copy stands in for an incoming scan that needs to be |
| 40 |
# registered back onto the reference. |
| 41 |
|
| 42 |
offset = np.array(target.length) * 0.4 |
| 43 |
transform = pv.Transform().rotate_x(25).rotate_z(-35).translate((offset, -offset, offset)) |
| 44 |
source = target.transform(transform, inplace=False) |
| 45 |
|
| 46 |
pl = pv.Plotter() |
| 47 |
pl.add_mesh(target, color='lightgray', opacity=0.6) |
| 48 |
pl.add_mesh(source, color='tomato', opacity=0.8) |
| 49 |
pl.camera_position = cpos |
| 50 |
pl.show() |
| 51 |
|
| 52 |
|
| 53 |
# %% |
| 54 |
# Recover the rigid transform |
| 55 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 56 |
# :func:`pyvista.DataSetFilters.align` runs ICP and returns both the aligned |
| 57 |
# mesh and the recovered transform matrix. |
| 58 |
|
| 59 |
aligned, matrix = source.align(target, return_matrix=True) |
| 60 |
|
| 61 |
_, closest_points = target.find_closest_cell(aligned.points, return_closest_point=True) |
| 62 |
aligned['distance_to_target'] = np.linalg.norm(aligned.points - closest_points, axis=1) |
| 63 |
|
| 64 |
pl = pv.Plotter(shape=(1, 2)) |
| 65 |
pl.subplot(0, 0) |
| 66 |
pl.add_mesh(target, color='lightgray', opacity=0.6) |
| 67 |
pl.add_mesh(source, color='tomato', opacity=0.8) |
| 68 |
pl.camera_position = cpos |
| 69 |
pl.subplot(0, 1) |
| 70 |
pl.add_mesh(target, color='lightgray', opacity=0.4) |
| 71 |
pl.add_mesh(aligned, scalars='distance_to_target', cmap='viridis') |
| 72 |
pl.camera_position = cpos |
| 73 |
pl.link_views() |
| 74 |
pl.show() |
| 75 |
|
| 76 |
|
| 77 |
# %% |
| 78 |
# Inspect the recovered transform |
| 79 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 80 |
# The returned matrix maps the transformed copy back onto the reference. |
| 81 |
|
| 82 |
np.round(matrix, 3) |
| 83 |
|
| 84 |
|
| 85 |
# %% |
| 86 |
# Measure the residual distances |
| 87 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 |
# A successful registration leaves a small point-to-surface residual. |
| 89 |
|
| 90 |
aligned['distance_to_target'].mean(), aligned['distance_to_target'].max() |
| 91 |
# %% |
| 92 |
# .. tags:: filter |