Rigid#

The rigid registration page groups the main rigid object with its supporting side and visualization classes.

class medicalimageanalysis.structure.rigid.Rigid(reference_name, moving_name, rigid_name=None, parent_name=None, reference_indices=None, moving_indices=None, matrix=None, local_uid=None, rigid_id=None)[source]#

Bases: object

Rigid registration between the frames of reference of a reference and a moving image.

Parameters:
  • reference_name (str or int) – Images in Data.image (name or id) used to compute the registration. Their frames define what it applies to. Only the ids are kept.

  • moving_name (str or int) – Images in Data.image (name or id) used to compute the registration. Their frames define what it applies to. Only the ids are kept.

  • rigid_name (str, optional) – User name in Data.rigid. None builds one from the current image names (a plain label: it is not updated when an image is renamed).

  • parent_name (str or int, optional) – Registration (name or id) this one is built on; it must connect the same two frames. Its result is the base of this one and is followed live while it exists; the last known base is kept (and saved) so this registration survives the parent being deleted.

  • reference_indices (list of int, optional) – z-slices used on each side (e.g. ultrasound frames). None = all slices.

  • moving_indices (list of int, optional) – z-slices used on each side (e.g. ultrasound frames). None = all slices.

  • matrix ((4, 4) array_like, optional) – This registration’s own step (reference -> moving). Defaults to identity.

  • local_uid (str, optional) – Existing uid when loading (from_json()). None generates a new one.

  • rigid_id (int, optional) – Existing id in Data.rigid when loading. None (or an id already in use) gets a new one.

id#

Permanent id in Data.rigid.

Type:

int

rigid_name#

User name, mirrored from Data.rigid. Change it with Data.rename_rigid.

Type:

str

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Rigid('CT 01', 'MR 01')
>>> rigid = mia.Rigid(1, 2)                                   # same, by image id
>>> rigid = mia.Rigid('CT 01', 'US 01', moving_indices=[10, 11, 12])
>>> left = mia.Rigid('CT 01', 'CBCT 01', rigid_name='left hip', parent_name='right hip')
SCHEMA_VERSION = 2#
static find_rigids(image_a, image_b)[source]#

Names of every registration in Data.rigid that connects the frames of two images (name or id, either direction). Use Data.rigid.get_id on a result for its id.

Several can match, e.g. ‘right hip’ and ‘left hip’ between the same CT and CBCT.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Rigid.find_rigids('Dose 01', 'MR 01')
['CT 01_MR 01']
add_rigid(rigid_name, rigid_id=None)[source]#

Store this registration in Data.rigid and return its name; None builds a unique name from the current image names. A name already in use is replaced and its id taken over.

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Rigid('CT 01', 'MR 01')
>>> rigid.rigid_name, rigid.id
('CT 01_MR 01', 1)
compute_array(plane, position, base_name=None, image_name=None, spacing=None, extend=True)[source]#

image_name resliced onto base_name’s view: Display.compute_array’s result plus ‘scale’ and ‘offset’.

Parameters:
  • plane (str) – View plane, e.g. ‘Axial’.

  • position (array_like) – The widget’s position (base image’s view).

  • base_name (str, optional) – Image whose view is shown. Any image in either frame. None = the registered reference image.

  • image_name (str, optional) – Image resliced onto the view. Any image in either frame. None = the registered moving image. An image on the same side as the base is in the same world, so it is sampled with no registration applied.

  • spacing (None, 'native', float or (sx, sy), optional) – None = base image grid, ‘native’ = moved image spacing, or any spacing. Use the same value in compute_mesh_slice() and compute_aspect().

  • extend (bool, default True) – Grow the grid past the base image so the whole of image_name is shown (fusion with a larger moving image). Place the result at ‘offset’ instead of the base image’s top-left pixel.

Returns:

Display.compute_array’s result plus ‘scale’ (sample spacing / base spacing) and ‘pixel_offset’ (col, row) of the result’s top-left pixel in the base view’s pixel coords; (0, 0) unless extend=True.

Return type:

dict

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Data.rigid['CT 01_MR 01']
>>> res = rigid.compute_array('Axial', position)
>>> res = rigid.compute_array('Sagittal', position, extend=True)    # whole MR, even past the CT's z range
>>> res = rigid.compute_array('Axial', position, base_name='MR 01', image_name='Dose 01')
compute_aspect(plane, base_name=None, image_name=None, spacing=None)[source]#

Pixel aspect (sx / sy) of the view, matching compute_array with the same images and spacing.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].compute_aspect('Sagittal')
compute_mesh_slice(roi_name, plane, position, base_name=None, image_name=None, offset=0, return_pixel=True, use_cache=True, spacing=None)[source]#

Slice one of image_name’s ROIs on the same grid compute_array samples, so image and contour line up.

roi_name is an ROI name or id. On the registered images it can be the paired ROI from either image. Pass use_cache=False while dragging or animating.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].compute_mesh_slice('Liver', 'Axial', position)
compute_registration(method='icp_point', reference_rois=None, moving_rois=None, reference_mesh=None, moving_mesh=None, reference_mask=None, moving_mask=None, reference_points=None, moving_points=None, use_current=True, **kwargs)[source]#

Run a rigid registration on the registered images and store the result in self.matrix (the parent’s base is factored out).

Parameters:
  • method ({'itk', 'itk_mask', 'landmark', 'icp_point', 'icp_plane', 'icp_vtk'}, default 'itk') –

    • ‘itk’ : intensity registration, RigidITK.rigid. Masks and slice subsets restrict the metric.

    • ’itk_mask’ : binary mask shape registration, RigidITK.rigid_mask.

    • ’landmark’ : paired-point fit, RigidITK.landmark.

    • ’icp_point’ / ‘icp_plane’ : Open3D ICP, ICP.compute_o3d.

    • ’icp_vtk’ : VTK ICP, ICP.compute_vtk.

  • reference_rois (str or list of str, optional) – ROI names paired by position (moving None = same names). Merged into one surface for ICP. They become this registration’s ROI pairs and pivot (see pair_rois()).

  • moving_rois (str or list of str, optional) – ROI names paired by position (moving None = same names). Merged into one surface for ICP. They become this registration’s ROI pairs and pivot (see pair_rois()).

  • reference_mesh (pyvista.PolyData, optional) – ICP only; override the ROIs.

  • moving_mesh (pyvista.PolyData, optional) – ICP only; override the ROIs.

  • reference_mask (numpy.ndarray, optional) – ITK only; binary (z, y, x). Required for ‘itk_mask’.

  • moving_mask (numpy.ndarray, optional) – ITK only; binary (z, y, x). Required for ‘itk_mask’.

  • reference_points ((N, 3) array_like, optional) – ‘landmark’ only; paired world points.

  • moving_points ((N, 3) array_like, optional) – ‘landmark’ only; paired world points.

  • use_current (bool, default True) – Start from the current combined matrix when it is not identity.

  • **kwargs – Passed straight to the backend.

Returns:

Backend diagnostics.

Return type:

dict

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Rigid('CT 01', 'MR 01')
>>> rigid.compute_registration(metric='MI')
>>> rigid.compute_registration('icp_plane', reference_rois='Liver', distance=5)
>>> rigid.compute_registration('icp_point', reference_rois=['Liver', 'Kidney_R'],
...                            moving_rois=['Liver_2', 'R Kidney'])
create_image(base_name=None, image_name=None)[source]#

Resample image_name onto base_name’s world (same convention as the display). Color keeps channels; outside image_name is its background (0 for color).

Examples

>>> import medicalimageanalysis as mia
>>> vtk_image = mia.Data.rigid['CT 01_MR 01'].create_image()
get_base_matrix()[source]#

reference -> moving result of the parent (identity without a parent).

Follows the parent live while it exists and keeps the last result, so this registration survives the parent being deleted. The steps don’t have to be run back to back.

get_center(base_name=None, image_name=None, plane=None, position=None)[source]#

image_name’s center as displayed on base_name, projected onto the active slice plane when given.

get_combined_matrix()[source]#

reference world -> moving world, including the parent’s result.

get_display_key(base_name=None, image_name=None)[source]#

Short hash of the display matrix for cache keys. Also changes when the parent changes.

get_display_matrix(base_name=None, image_name=None)[source]#

image_name native world -> base_name world, including the step animation. Pass this as rigid_matrix.

Identity when both images are on the same side (same frame of reference).

get_indices(image_name)[source]#

z-indices registered for this image (e.g. ultrasound frames), or None for all slices / other images.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Rigid('CT 01', 'US 01', moving_indices=[40, 41, 42]).get_indices('US 01')
[40, 41, 42]
get_motion_matrix(base_name=None, image_name=None, fraction=1.0)[source]#

This step’s motion M in base_name’s world (full display = M @ base display), interpolated by fraction.

The pivot moves in a straight line while the rotation angle scales linearly, so the structure slides and turns in place instead of swinging around the world origin.

get_view_center(plane, position, base_name=None, image_name=None)[source]#

Center in the base widget’s view coords (pixel centers at integers, matching the -0.5 placement).

get_vtk_matrix(base_name=None, image_name=None)[source]#

For actor.SetUserMatrix on image_name’s ROI actors in base_name’s 3D view.

pair_rois(reference_rois, moving_rois=None)[source]#

Set which reference ROI corresponds to which moving ROI (paired by position, replaces earlier pairs).

Names or ids; the ids are stored, so renaming an ROI keeps the pair.

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Rigid('CT 01', 'MR 01')
>>> rigid.pair_rois(['Liver', 'Kidney_R'], ['Liver_2', 'R Kidney'])
>>> rigid.compute_mesh_slice('Liver', 'Axial', position)      # slices 'Liver_2' on the MR
retrieve_angles(order='ZXY')[source]#

Euler angles (degrees) of this registration’s own step.

retrieve_motion(base_name=None, image_name=None, order='ZXY')[source]#

This step’s rotation (degrees) and the displacement of the pivot, in base_name’s world.

Returns:

  • angles ((3,) ndarray)

  • translation ((3,) ndarray)

retrieve_translation()[source]#

Translation of this registration’s own step.

align_origins()[source]#

Set this step so the two registered images’ origins coincide (a rough start before compute_registration).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].align_origins()
update_rotation(base_name=None, image_name=None, center=None, r_x=0, r_y=0, r_z=0, plane=None, position=None, axis=None, angle=None)[source]#

Rotate image_name as seen on base_name, about center (default: its center on the current slice), by Euler r_x/r_y/r_z (world axes, degrees) or axis + angle (degrees).

The registration itself changes, so every view using it follows.

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Data.rigid['CT 01_MR 01']
>>> rigid.update_rotation(r_z=2, plane='Axial', position=position)
>>> rigid.update_rotation('MR 01', 'CT 01', axis=[0, 0.7, 0.7], angle=1.5, plane='Axial', position=position)
update_translation(base_name=None, image_name=None, t_x=0, t_y=0, t_z=0)[source]#

Translate image_name as seen on base_name, in base_name’s world coordinates.

Examples

>>> import medicalimageanalysis as mia
>>> rigid = mia.Data.rigid['CT 01_MR 01']
>>> rigid.update_translation(t_x=1.5)
>>> rigid.update_translation('MR 01', 'CT 01', t_x=1.5)
classmethod from_json(text)[source]#

Rebuild a registration from to_json() output and register it in Data.rigid.

The saved frames and ids are kept, so it works before its images are loaded (the image ids must be the ones the images are restored with). The parent is found again by uid when it is loaded too; otherwise the saved base matrix is used.

Schema 1 records (saved with image and ROI names) are read too: the names are looked up, so their images and ROIs must be loaded first.

Examples

>>> import medicalimageanalysis as mia
>>> with open('C:/Data/ct_mr.json') as f:
...     rigid = mia.Rigid.from_json(f.read())
export_image(path, base_name=None, image_name=None)[source]#

Write the resampled image (create_image) as an mhd/raw MetaImage pair.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].export_image('C:/Data/aligned.mhd')
save(path)[source]#

Write the JSON state to a file. Written to a temporary name and swapped in, so a crash never leaves a half-written file.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].save('C:/Data/ct_mr.json')
to_json()[source]#

Compact JSON of the registration state (no images, meshes or display settings), typically under 1 KB.

connects(image_a, image_b)[source]#

True when the two images are on opposite sides of this registration (by name or frame of reference).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.rigid['CT 01_MR 01'].connects('Dose 01', 'MR 01')
True
class medicalimageanalysis.structure.rigid.RigidSide(image_id, frame, indices=None, rois=<factory>)[source]#

Bases: object

One side (reference or moving) of a registration. Built by Rigid, not created directly.

Parameters:
image_id#

Permanent id of the image this side was registered with (Data.image[image_id]).

Type:

int

frame#

Frame of Reference UID shared by every image on this side; ‘volume:<id>’ when the image has none.

Type:

str

indices#

z-indices of the image used for computation. None = all slices. The matching SOP Instance UIDs are Data.image[image_id].sops[i].

Type:

list of int or None

rois#

ROI ids, paired by position with the other side’s rois.

Type:

list of int

image_id: int#
frame: str#
indices: list | None = None#
rois: list#
class medicalimageanalysis.structure.rigid.RigidVisual(reference=None, moving=None, opacity=0.5, multicolor=None, display_fraction=1.0)[source]#

Bases: object

Display settings for a registration. Application state: never saved.

Parameters:
reference, moving

Per-image display settings (e.g. colormap names).

Type:

object

opacity#

Blend opacity of the moved image.

Type:

float

multicolor#

Multicolor overlay setting.

Type:

object

display_fraction#

Step animation: 0 shows only the parent’s result (structure unaligned), 1 the full registration.

Type:

float

reference: object = None#
moving: object = None#
opacity: float = 0.5#
multicolor: object = None#
display_fraction: float = 1.0#