ICP#

class medicalimageanalysis.utils.rigid.icp.ICP(source, target, matrix=None)[source]

Bases: object

Performs Iterative Closest Point (ICP) registration between a source and a target mesh or point cloud. Supports both VTK-based and Open3D-based ICP implementations.

Parameters:
  • source (pyvista.PolyData) – The moving/source mesh to align.

  • target (pyvista.PolyData) – The reference/target mesh.

  • matrix ((4, 4) ndarray, optional) – Initial source -> target transform. None means it is estimated in the compute call.

Examples

>>> import pyvista as pv
>>> source = pv.ParametricEllipsoid(30, 20, 10)
>>> icp = ICP(source, source.translate((2, 0, 0)))
>>> icp.compute_vtk()
>>> round(float(icp.get_matrix()[0, 3]), 1)
2.0
compute_com()[source]

Computes an initial translation by matching the vertex means of the source and target meshes.

The result is stored in self.matrix and is used as the starting transform by compute_o3d() and compute_vtk().

Returns:

Sets self.matrix ((4, 4) ndarray, translation only).

Return type:

None

Examples

>>> import pyvista as pv
>>> icp = ICP(pv.Sphere(), pv.Sphere(center=(2, 0, 0)))
>>> icp.compute_com()
>>> round(float(icp.get_matrix()[0, 3]), 3)
2.0
compute_o3d(distance=10, iterations=50, rmse=1e-06, fitness=1e-06, method='point', com_matching=True, inverse=False, src_frac=0.1, tgt_frac=1.0, min_pts=1000, max_pts=20000, seed=0)[source]

Performs rigid ICP using Open3D on subsampled source/target points.

Parameters:
  • distance (float) – Maximum correspondence distance (mesh units). Pairs further apart are rejected as outliers, so the initial offset must be within this distance.

  • iterations (int) – Maximum number of ICP iterations.

  • rmse (float) – Convergence threshold for relative change in RMSE.

  • fitness (float) – Convergence threshold for relative change in fitness.

  • method ({'point', 'plane'}) – ‘point’ for point-to-point ICP, ‘plane’ for point-to-plane ICP (uses the target point normals).

  • com_matching (bool) – If True and no matrix is stored, initializes with the translation between vertex means.

  • inverse (bool) – If True, stores the inverse (target -> source) of the resulting transformation matrix.

  • src_frac (float) – Fraction of source points used, clamped to [min_pts, max_pts].

  • tgt_frac (float) – Fraction of target points used, clamped below by min_pts (no upper cap).

  • min_pts (int) – Minimum number of points kept per cloud.

  • max_pts (int or None) – Maximum number of source points kept. None means no cap.

  • seed (int) – Random seed for subsampling.

Returns:

Sets self.icp (open3d RegistrationResult), self.source_idx / self.target_idx (subsample indices) and self.matrix ((4, 4) ndarray, source -> target, or its inverse if inverse=True).

Return type:

None

Raises:

ValueError – If method is not ‘point’ or ‘plane’.

Examples

>>> import numpy as np
>>> import pyvista as pv
>>> source = pv.ParametricEllipsoid(30, 20, 10)
>>> icp = ICP(source, source.rotate_z(5).translate((2, 1, 0)))
>>> icp.compute_o3d(method='plane')
>>> np.round(icp.get_matrix()[:3, 3], 1).tolist()
[2.0, 1.0, 0.0]
>>> icp.get_metrics()['fitness']
1.0
compute_vtk(distance=0.001, iterations=100, src_frac=0.1, min_pts=1000, max_pts=20000, com_matching=True, inverse=False)[source]

Performs rigid ICP using VTK, with landmark count set as a fraction of source points.

VTK matches each landmark to the closest point on the target surface and has no outlier rejection, so it is best suited to meshes that overlap fully. vtkIterativeClosestPointTransform has no initial-transform input, so a stored matrix is applied to a copy of the source first and composed with the ICP result.

Parameters:
  • distance (float) – Absolute RMS distance threshold for convergence (mesh units). Note this is a convergence tolerance, not the correspondence radius used by compute_o3d().

  • iterations (int) – Maximum number of ICP iterations.

  • src_frac (float) – Fraction of source points used as landmarks, clamped to [min_pts, max_pts]. VTK picks landmarks with a regular stride through the point list, not randomly.

  • min_pts (int) – Minimum number of landmarks.

  • max_pts (int or None) – Maximum number of landmarks. None means no cap.

  • com_matching (bool) – If True and no matrix is stored, starts by matching the source and target vertex means.

  • inverse (bool) – If True, stores the inverse (target -> source) of the resulting transformation matrix.

Returns:

Sets self.icp (vtkIterativeClosestPointTransform) and self.matrix ((4, 4) ndarray, source -> target, or its inverse if inverse=True).

Return type:

None

Examples

>>> import numpy as np
>>> import pyvista as pv
>>> source = pv.ParametricEllipsoid(30, 20, 10)
>>> icp = ICP(source, source.rotate_z(5).translate((2, 1, 0)))
>>> icp.compute_vtk()
>>> np.round(icp.get_matrix()[:3, 3], 1).tolist()
[2.0, 1.0, 0.0]
get_correspondence_set(original=True)[source]

Returns the set of corresponding point indices from the last compute_o3d() call.

Parameters:

original (bool, default True) – If True, indices refer to the original source/target mesh points. If False, they refer to the subsampled point clouds used inside the ICP.

Returns:

Columns are (source index, target index). None if the last run was not Open3D.

Return type:

(M, 2) ndarray or None

Examples

>>> import pyvista as pv
>>> source = pv.ParametricEllipsoid(30, 20, 10)
>>> icp = ICP(source, source.translate((2, 0, 0)))
>>> icp.compute_o3d()
>>> pairs = icp.get_correspondence_set()
>>> pairs.shape[1], bool(pairs[:, 0].max() < source.n_points)
(2, True)
get_matrix()[source]

Returns the resulting transformation matrix.

Returns:

4x4 transformation matrix, or None if nothing has been set or computed.

Return type:

np.ndarray or None

Examples

>>> import pyvista as pv
>>> icp = ICP(pv.Sphere(), pv.Sphere(center=(2, 0, 0)))
>>> icp.get_matrix() is None
True
>>> icp.compute_com()
>>> icp.get_matrix().shape
(4, 4)
get_metrics()[source]

Returns quality metrics from the last ICP run, for QA and failure detection.

Returns:

Open3D: {'fitness', 'inlier_rmse', 'correspondences'} (fitness is the inlier fraction of source points). VTK: {'mean_distance', 'iterations'}. None if no ICP has been run.

Return type:

dict or None

Examples

>>> import pyvista as pv
>>> source = pv.ParametricEllipsoid(30, 20, 10)
>>> icp = ICP(source, source.translate((2, 0, 0)))
>>> icp.compute_vtk()
>>> sorted(icp.get_metrics())
['iterations', 'mean_distance']