Image#

The image structures page groups the main image container and its display helper together.

class medicalimageanalysis.structure.image.Image(image)[source]#

Bases: object

One image volume with its DICOM tags, geometry, ROIs, POIs and display.

Parameters:

image (object) – Reader output (e.g. Read3D, ReadUS, ReadXRay) providing image_set, array, image_name, modality, filepaths, sops, plane, spacing, dimensions, orientation, origin, image_matrix, skipped_slice and rgb.

background#

Class attribute: value shown outside the volume when it is resliced (-3001). Subclasses override it.

Type:

float

id#

Permanent id, written by Data.image when the image is registered. None until then.

Type:

int | None

image_name#

User name, mirrored from Data.image. Change it with Data.rename_image.

Type:

str

roi, poi

ROIs / POIs by permanent id or name. Their ids come from Data.new_roi_id / Data.new_poi_id, so an ROI has the same id on every image. Rename them with Data.rename_roi / Data.rename_poi, not on one image.

Type:

Registry

moi, planeoi

Markups / planes of interest by permanent id or name. Ids come from Data.new_moi_id / Data.new_planeoi_id (unique across every image); names are unique on this image only.

Type:

Registry

model#

Models (clustered / tetrahedral meshes derived from this image’s ROIs) by permanent id or name. Ids come from Data.new_model_id (unique across every image); names are unique on this image only.

Type:

Registry

roi_group, poi_group, moi_group, planeoi_group, model_group

Groups of each kind (Group) by permanent id or path. The same groups, under the same ids, are on every image (Data.match_groups).

Type:

Registry

Examples

>>> import medicalimageanalysis as mia
>>> mia.read_mhd(file='C:/Data/scan.mhd', modality='CT')
>>> image = mia.Data.image[mia.Data.image_list()[0]]
>>> image.origin, image.spacing, image.array.shape

ROIs by name or id

>>> roi_id = mia.Data.get_roi_id('Liver')
>>> image.roi['Liver'] is image.roi[roi_id]
True
background = -3001.0#
add_model(roi_names, kind='tet', name=None, cavities=None, cluster_points=None, edge_length_fac=0.05, edge_length_abs=None, region_edge_lengths=None, epsilon=0.001, optimize=True, simplify=True, coarsen=False, stop_energy=10.0, num_opt_iter=80, color=None, visible=True, build=False, group=None)[source]#

Add a model (clustered surface or tetrahedral mesh) built from ROIs of this image. Nothing is synced.

The recipe is stored on the model; it is only run when build=True or on model.build(), since pytetwild takes seconds.

Parameters:
  • roi_names (str, int or list of them) – Source ROIs (names or ids), in region order.

  • kind ({'tet', 'surface'}, default 'tet')

  • name (str, optional) – Name on this image. None = ‘Model <n>’.

  • cavities

  • cluster_points

  • edge_length_fac

  • edge_length_abs

  • region_edge_lengths

  • epsilon

  • optimize

  • simplify

:param : :param coarsen: The recipe; see Model. :param stop_energy: The recipe; see Model. :param num_opt_iter: The recipe; see Model. :param color: RGB, 0-255. Random if None. :type color: list of int, optional :param visible: :type visible: bool, default True :param build: Run the recipe now. :type build: bool, default False :param group: Model group (path or id) to put the model in; a path is created when missing. None = ungrouped. :type group: str or int, optional

Return type:

Model

Raises:

KeyError – If name is already a model on this image.

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Data.image['CT 01']
>>> fe = image.add_model(['Liver', 'GTV'], name='Liver FE', cluster_points=[2500, 800], edge_length_abs=4.0,
...                      region_edge_lengths=[None, 1.5], build=True)
>>> cpd = image.add_model('Liver', kind='surface', name='Liver CPD', cluster_points=2500, build=True)
add_moi(kind, points, view_plane='Axial', position=None, name=None, color=None, visible=True, text=None, group=None)[source]#

Add a markup of interest (line, arrow, angle, polyline, polygon, ellipse or text) drawn on one view.

The markup keeps that view’s plane (current rotation included) and is shown only on views lying in it. Nothing is synced to other images.

Parameters:
  • kind (str) – ‘line’, ‘arrow’, ‘angle’, ‘polyline’, ‘polygon’, ‘ellipse’ or ‘text’ (point counts: Moi.POINTS).

  • points ((N, 3) array_like) – World points (mm). From clicks: Moi.convert_pixel_to_position(pixels, grid).

  • view_plane (str, default 'Axial') – Plane of the view it is drawn on.

  • position ((3,) array_like, optional) – That view’s position. None = self.display.position[view_plane].

  • name (str, optional) – Name on this image. None = ‘<Kind> <n>’ (e.g. ‘Line 3’).

  • color (list of int, optional) – RGB, 0-255. Defaults to yellow.

  • visible (bool, default True) – Show in the views.

  • text (str, optional) – Label (the content for kind ‘text’).

  • group (str or int, optional) – Markup group (path or id) to put the markup in; a path is created when missing. None = ungrouped.

Return type:

Moi

Raises:

KeyError – If name is already a markup on this image.

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Data.image['CT 01']
>>> position = image.display.position['Axial']
>>> grid = image.display.get_grid('Axial', position)
>>> points = mia.Moi.convert_pixel_to_position([[200, 180], [260, 230]], grid)
>>> ruler = image.add_moi('line', points, view_plane='Axial', position=position)
>>> ruler.name, ruler.compute_measurement()
('Line 1', ('Length', 52.4, 'mm'))
>>> image.add_moi('text', points[:1], position=position, text='Recurrence?', color=[255, 0, 0])
add_planeoi(origin, normal, x_axis=None, name=None, extent=None, keep_side=None, group=None, color=None, visible=True)[source]#

Add a plane of interest (cut plane, reference plane, …). Nothing is synced to other images.

Parameters:
  • origin ((3,) array_like) – World point on the plane (mm); the center of its extent.

  • normal ((3,) array_like) – Plane normal.

  • x_axis ((3,) array_like, optional) – In-plane x direction. None picks one.

  • name (str, optional) – Name on this image. None = ‘Plane <n>’.

  • extent ((2,) array_like, optional) – (width, height) in mm. None = infinite.

  • keep_side ({1, -1, None}, optional) – Side kept when cutting (+1 = the side the normal points to).

  • group (str or int, optional) – Plane group (path or id) tying planes together (e.g. one resection); a path is created when missing. None = ungrouped.

  • color (list of int, optional) – RGB, 0-255. Random if None.

  • visible (bool, default True) – Show in the views.

Return type:

PlaneOI

Raises:

KeyError – If name is already a plane on this image.

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Data.image['CT 01']
>>> cut = image.add_planeoi(origin=[12.0, -80.0, 410.0], normal=[0.0, 0.3, 0.95], extent=(120, 90),
...                          keep_side=-1, group='Resection 1', name='Superior cut')

The plane of the view currently shown, through the crosshair:

>>> grid = image.display.get_grid('Sagittal', image.display.position['Sagittal'])
>>> image.add_planeoi(origin=image.display.crosshair, normal=grid['normal'], x_axis=grid['x_axis'])
add_poi(poi_name=None, color=None, visible=False, path=None, point=None, match=True, group=None)[source]#

Add a point of interest at a world position.

The POI gets the id poi_name already has on another image, or a new one (Data.new_poi_id).

Parameters:
  • poi_name (str, optional) – Name in self.poi.

  • color (list of int, optional) – RGB, 0-255.

  • visible (bool, default False) – Show in the views.

  • path (str, optional) – Source file.

  • point ((3,) array_like, optional) – World position (x, y, z).

  • match (bool, default True) – Run Data.match_pois afterwards. match_pois itself passes False.

  • group (str or int, optional) – POI group (path or id) to put the POI in, on every image; a path is created when missing. None keeps the group the POI already has (ungrouped for a new name).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].add_poi('Iso', color=[255, 0, 0], point=[0.0, -20.0, 15.5])
>>> mia.Data.image['CT 01'].poi['Iso'].id
add_roi(roi_name=None, color=None, visible=False, path=None, contour=None, plane='Axial', match=True, group=None)[source]#

Add an ROI from world-space contours.

The ROI gets the id roi_name already has on another image, or a new one (Data.new_roi_id).

Parameters:
  • roi_name (str, optional) – Name in self.roi.

  • color (list of int, optional) – RGB, 0-255.

  • visible (bool, default False) – Show in the views.

  • path (str, optional) – Source file.

  • contour (list of (N, 3) array_like, optional) – World-space contour points.

  • plane (str, default 'Axial') – Plane the contours were drawn on.

  • match (bool, default True) – Run Data.match_rois afterwards. match_rois itself passes False.

  • group (str or int, optional) – ROI group (path or id) to put the ROI in, on every image; a path is created when missing. None keeps the group the ROI already has (ungrouped for a new name).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].add_roi('Liver', color=[0, 255, 0], contour=contours)
>>> mia.Data.image['CT 01'].roi['Liver'].id == mia.Data.image['MR 01'].roi['Liver'].id
True
add_saved_model(model, group_map=None)[source]#

Add a model read with Model.load / Model.from_bytes, keeping its saved id unless another model on any loaded image already uses it; a name already on this image gets the next free ‘<name> <n>’. Its saved group goes through group_map (from load_groups() / groups_from_json()).

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Data.image['CT 01']
>>> image.add_saved_model(mia.Model.load(image, 'C:/Data/liver_fe.npz'))
groups_from_json(text)[source]#

Add the groups in groups_to_json() output to this image and return {saved id: id in this session}, for the loaders of this image’s ROIs, POIs, markups, planes and models.

A saved group is a group already in the session (same kind) when, in this order:

  1. it has the same id and Data.group_id_provider is set (ids then come from the database, so they are global; without a provider two unrelated files can both hold id 5),

  2. it is a composite of the same main ROI (one composite per main ROI), or

  3. it has the same path.

That covers a group renamed in the session since the file was saved. The saved group then takes the session group’s id, path and identity (name, parent, main, method) and keeps this image’s saved color, visibility and match record. Otherwise it is new and keeps its saved id unless another group uses it. The groups are then added to every other image (Data.match_groups). Main ROI ids are kept as saved.

Examples

>>> import medicalimageanalysis as mia
>>> with open('C:/Data/CT 01/groups.json') as f:
...     group_map = mia.Data.image['CT 01'].groups_from_json(f.read())
groups_to_json()[source]#

Every group of this image, of every kind, as one JSON string: {‘schema’, ‘roi’: […], ‘poi’: […], …}, parents before children, each entry that group’s own to_json string (including this image’s match record). Empty groups are saved too: like an ROI, a group exists on every image.

Examples

>>> import medicalimageanalysis as mia
>>> text = mia.Data.image['CT 01'].groups_to_json()
load_groups(groups_path, key)[source]#

Decrypt and add the groups written by save() (groups.enc) and return {saved id: id in this session} for the loaders that follow (see groups_from_json()). Call after load_metadata() and before loading this image’s ROIs, POIs, markups and models.

Examples

>>> import medicalimageanalysis as mia
>>> image.load_metadata('C:/Data/CT 01/metadata.enc', key)
>>> group_map = image.load_groups('C:/Data/CT 01/groups.enc', key)
>>> image.load_markups('C:/Data/CT 01/markups.enc', key, group_map=group_map)
>>> image.load_models('C:/Data/CT 01', key, group_map=group_map)
compute_corner_positions()[source]#

(8, 3) world positions of the corner voxel centers.

Corners 0-3 go around the first slice: index (0, 0), (x, 0), (x, y), (0, y). Corners 4-7 are the same on the last slice, so corner i + 4 sits above corner i.

Examples

>>> import medicalimageanalysis as mia
>>> corners = mia.Data.image['CT 01'].compute_corner_positions()
compute_initial_origin(plane)[source]#

World position (top-left corner) of the plane’s slice through the middle of the volume.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].compute_initial_origin('Sagittal')
compute_matrix_pixel_to_position()[source]#

(4, 4) matrix mapping a pixel index (x, y, z, 1) to a world position.

Examples

>>> import medicalimageanalysis as mia
>>> pixel_to_position = mia.Data.image['CT 01'].compute_matrix_pixel_to_position()
compute_matrix_position_to_pixel()[source]#

(4, 4) matrix mapping a world position (x, y, z, 1) to a fractional pixel index.

Examples

>>> import medicalimageanalysis as mia
>>> position_to_pixel = mia.Data.image['CT 01'].compute_matrix_position_to_pixel()
compute_pixel(position)[source]#

Fractional pixel index (x, y, z) of a world position. Round it for a voxel.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].compute_pixel([10.0, -42.5, 103.0])
compute_position(xyz)[source]#

World position of a pixel index (x, y, z), fractional allowed.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].compute_position([256, 256, 40])
create_external(name='External', color=None, visible=False, filepaths=None, threshold=-250)[source]#

Body outline ROI from an intensity threshold (contours + mesh).

Parameters:
  • name (str, default 'External') – ROI name; created when missing.

  • color (list of int, optional) – RGB, 0-255. Defaults to green.

  • visible (bool, default False) – Show in the views.

  • filepaths (str, optional) – Source file.

  • threshold (float, default -250) – Lowest value inside the body (HU for CT).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].create_external(threshold=-300)
create_roi(name=None, color=None, visible=False, filepath=None, group=None)[source]#

Add an empty ROI (to be drawn or filled later). group (path or id) puts it in an ROI group on every image; None keeps the group the ROI already has.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].create_roi('GTV', color=[255, 0, 0])
>>> mia.Data.image['CT 01'].create_roi('Hepatic artery', group='Liver/Vessels')
create_sitk_image(empty=False)[source]#

SimpleITK image with this image’s geometry. Color becomes a vector image.

Parameters:

empty (bool, default False) – True gives a zero UInt8 image of the same size (e.g. for a mask).

Examples

>>> import medicalimageanalysis as mia
>>> sitk_image = mia.Data.image['CT 01'].create_sitk_image()
>>> mask = mia.Data.image['CT 01'].create_sitk_image(empty=True)
get_aspect(slice_plane)[source]#

Pixel aspect (sx / sy) of a native slice, rounded to 2 decimals.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_aspect('Coronal')
get_bounds()[source]#

World bounding box of the voxel centers: [x_min, x_max, y_min, y_max, z_min, z_max].

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_bounds()
get_center(position=True, zyx=False)[source]#

Center of the volume as a world position or a pixel index.

Parameters:
  • position (bool, default True) – True gives the world position, False the pixel index.

  • zyx (bool, default False) – Return in (z, y, x) order instead of (x, y, z).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_center()
>>> mia.Data.image['CT 01'].get_center(position=False, zyx=True)
get_corner_sides()[source]#

Box surface (6 quads) through the corner voxel centers, for showing the image extent in 3D.

Examples

>>> import medicalimageanalysis as mia
>>> box = mia.Data.image['CT 01'].get_corner_sides()
get_specific_tag(tag)[source]#

A DICOM tag from the first file, or None.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_specific_tag('SliceThickness')
>>> mia.Data.image['CT 01'].get_specific_tag((0x0018, 0x0050))
get_specific_tag_on_all_files(tag)[source]#

A DICOM tag from every file (list), or None when the first file doesn’t have it.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_specific_tag_on_all_files('ImagePositionPatient')
get_window()[source]#

[lower, upper] display window from WindowCenter/WindowWidth, else the array’s min and max.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].get_window()
[-160, 240]
input_mhd(filename, roi_names, values, plane='Axial')[source]#

Add ROIs from a MetaImage (.mhd/.raw) label map: each value in values becomes the ROI of the same position.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].input_mhd('C:/Data/labels.mhd', ['Liver', 'Spleen'], [1, 2])
input_rtstruct(rtstruct)[source]#

Add the ROIs and POIs of a read RTSTRUCT. Existing ones are kept unless they have no contours. A replaced ROI/POI keeps its group.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].input_rtstruct(rtstruct)
load_array(array_path, key)[source]#

Decrypt and decompress the array written by save().

Examples

>>> import medicalimageanalysis as mia
>>> image.load_array('C:/Data/CT 01/array.enc', key)
load_markups(markups_path, key, group_map=None)[source]#

Decrypt and add the markups and planes written by save() (markups.enc). Call after load_metadata(), so the image is registered and ids are checked against every loaded image. group_map (from load_groups()) maps their saved groups.

Examples

>>> import medicalimageanalysis as mia
>>> image.load_metadata('C:/Data/CT 01/metadata.enc', key)
>>> image.load_array('C:/Data/CT 01/array.enc', key)
>>> group_map = image.load_groups('C:/Data/CT 01/groups.enc', key)
>>> image.load_markups('C:/Data/CT 01/markups.enc', key, group_map=group_map)
load_metadata(metadata_path, key)[source]#

Decrypt the metadata written by save() into this image and register it (Data.image, or Data.dose for a Dose).

Call load_array() afterwards for the pixels.

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Image.__new__(mia.Image)
>>> image.load_metadata('C:/Data/CT 01/metadata.enc', key)
>>> image.load_array('C:/Data/CT 01/array.enc', key)
load_models(folder_path, key, group_map=None)[source]#

Decrypt and add every model written by save() (model_<id>.enc files). Call after load_metadata(); group_map (from load_groups()) maps their saved groups.

Examples

>>> import medicalimageanalysis as mia
>>> image.load_metadata('C:/Data/CT 01/metadata.enc', key)
>>> image.load_array('C:/Data/CT 01/array.enc', key)
>>> image.load_models('C:/Data/CT 01', key)
load_unencrypted_image(image_path, rois=True, pois=True)[source]#

Load an image folder written by save_unencrypted(), optionally with its ROIs and POIs.

The image is registered in Data.image straight after its attributes are restored, under its saved id and name (a new id when the saved one is taken in this session), the same as load_metadata(). It does not need to be registered first; if it was, it is moved, not held twice. The groups (groups.json) are added next, so every item finds its group, and at the end every image gets its ROIs / POIs (Data.match_rois / match_pois).

Examples

>>> import medicalimageanalysis as mia
>>> image.load_unencrypted_image('C:/Data/CT 01')
load_unencrypted_pois(poi_path, group_map=None)[source]#

Load one POI folder, keeping its saved id when it doesn’t clash.

A name already on this image gets a _1, _2, … suffix and a new id. A name already on another image takes that image’s id, so one name never has two ids. The saved id is also dropped when another POI already uses it. Folders saved before ids existed get a new id.

A POI already on another image keeps that image’s group; otherwise the saved group (group_id.npy) is used, mapped through group_map (from groups_from_json(); load_unencrypted_image() passes it).

Examples

>>> import medicalimageanalysis as mia
>>> image.load_unencrypted_pois('C:/Data/CT 01/POIs/3')
load_unencrypted_rois(roi_path, group_map=None)[source]#

Load one ROI folder, keeping its saved id when it doesn’t clash.

A name already on this image gets a _1, _2, … suffix and a new id. A name already on another image takes that image’s id, so one name never has two ids. The saved id is also dropped when another ROI already uses it. Folders saved before ids existed get a new id.

An ROI already on another image keeps that image’s group; otherwise the saved group (group_id.npy) is used, mapped through group_map (from groups_from_json(); load_unencrypted_image() passes it).

Examples

>>> import medicalimageanalysis as mia
>>> image.load_unencrypted_rois('C:/Data/CT 01/ROIs/1')
markups_from_json(text, group_map=None)[source]#

Add the markups and planes in markups_to_json() output to this image.

A saved id is kept unless another markup (plane) on any loaded image already uses it; a name already on this image gets the next free ‘<name> <n>’. Saved groups go through group_map (from groups_from_json()); with None a saved group id is kept when this image has that group.

Examples

>>> import medicalimageanalysis as mia
>>> with open('C:/Data/CT 01/markups.json') as f:
...     mia.Data.image['CT 01'].markups_from_json(f.read())
markups_to_json()[source]#

Every markup and plane of this image as one JSON string: {‘schema’, ‘moi’: […], ‘planeoi’: […]}, where each entry is that item’s own to_json string (so it can also be stored one database row per item). Their groups are saved by groups_to_json().

Examples

>>> import medicalimageanalysis as mia
>>> text = mia.Data.image['CT 01'].markups_to_json()
save(folder_path, key, clevel=5)[source]#

Write encrypted metadata.enc (pickled, no array/ROIs/POIs), array.enc (blosc2 ZSTD), groups.enc (groups of every kind, JSON), markups.enc (markups and planes, JSON) and one model_<id>.enc per model (npz) into folder_path. Load the groups back first (load_groups()).

Parameters:
  • folder_path (str) – Existing folder.

  • key (bytes) – AES-GCM key (16, 24 or 32 bytes).

  • clevel (int, default 5) – Compression level, 0-9.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].save('C:/Data/CT 01', key)
save_unencrypted(path, rois=True, pois=True)[source]#

Write the image to a folder: info.p (attributes), tags.npy, array.npy, groups.json, markups.json (markups and planes), plus ROIs/, POIs/ and Models/ folders.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].save_unencrypted('C:/Data/CT 01')
save_unencrypted_pois(path, create_main_folder=False)[source]#

Write one folder per POI (id, name, visible, color, filepaths, point_position, group_id when grouped) under path.

Folders are named by the permanent POI id, so renaming a POI never moves its folder. create_main_folder=True puts them in a new ‘POIs’ subfolder.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].save_unencrypted_pois('C:/Data/CT 01', create_main_folder=True)
save_unencrypted_rois(path, create_main_folder=False)[source]#

Write one folder per ROI (id, name, visible, color, filepaths, contour_position, group_id when grouped) under path.

Folders are named by the permanent ROI id, so renaming an ROI never moves its folder. create_main_folder=True puts them in a new ‘ROIs’ subfolder.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].save_unencrypted_rois('C:/Data/CT 01', create_main_folder=True)
reset_display()[source]#

Clear the view rotation and put every plane back at its initial slice (see Display.reset()).

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].reset_display()
retrieve_angles(order='ZXY')[source]#

Euler angles (degrees) of the display’s view rotation.

The display builds its rotation from Euler ‘xyz’, so the angles differ from rotation_totals unless order=’xyz’.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].retrieve_angles()
class medicalimageanalysis.structure.image.Display(image)[source]#

Bases: object

Reslicing and view state for one image: slice positions, the shared view rotation and crosshair lines.

Built by Image (or a subclass such as Dose), not created directly. Each call names its own plane and position, so any number of widgets can share one Display.

Parameters:

image (Image) – The image this display reslices.

matrix#

Shared view rotation, rebuilt from rotation_totals (Euler ‘xyz’). Identity = native orientation.

Type:

(3, 3) ndarray

rotation_totals#

Total rotation about x, y, z in degrees.

Type:

(3,) ndarray

rotation_center#

World pivot every plane passes through while rotating.

Type:

(3,) ndarray

crosshair#

World point where the crosshair lines cross.

Type:

(3,) ndarray

position#

Plane name -> world position (top-left corner) of that plane’s current slice.

Type:

dict

oblique_shape#

Plane name -> (h, w) of the resliced output grid.

Type:

dict

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> res = display.compute_array('Axial', display.position['Axial'])
AXES = {'Axial': (0, 1, 2), 'Coronal': (0, 2, 1), 'Sagittal': (1, 2, 0)}#
static grid_with_spacing(grid, sx, sy)[source]#

Same corner, axes and physical extent as grid, with a different pixel size.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> grid = display.get_grid('Axial', display.position['Axial'])
>>> fine = mia.Display.grid_with_spacing(grid, 0.5, 0.5)
compute_array(plane, position, matrix_override=None, rigid_matrix=None, grid=None, as_array=True, as_vtk=False, copy_array=False, force_reslice=False, background=None)[source]#

2D slice of this image for one plane, axis-aligned when possible, otherwise resliced.

Parameters:
  • plane (str) – ‘Axial’, ‘Sagittal’ or ‘Coronal’.

  • position (array_like) – World position (top-left corner) of the slice.

  • matrix_override ((3, 3) array_like, optional) – View rotation to use instead of the shared self.matrix.

  • rigid_matrix ((4, 4) array_like, optional) – This image’s native world -> displayed world (e.g. Rigid.get_display_matrix).

  • grid (dict, optional) – Output pixel grid from another Display’s get_grid() / grid_with_spacing(). When given, it fully defines the output pixels and this Display only supplies its volume.

  • as_array (bool) – Return the slice as a numpy array, a vtkImageData, or both. Color slices keep their channels in VTK.

  • as_vtk (bool) – Return the slice as a numpy array, a vtkImageData, or both. Color slices keep their channels in VTK.

  • copy_array (bool, default False) – Copy the array. The axis-aligned path otherwise returns a view into the volume.

  • force_reslice (bool, default False) – Always use the reslice kernel, even when a native slice would do.

  • background (float, optional) – Value outside the volume. None = image.background (-3001 for images, 0 Gy for a dose). Color always uses 0.

Returns:

‘dimensions’, ‘spacing’, ‘pixel_spacing’, ‘x_axis’, ‘y_axis’, ‘normal’, ‘origin’, ‘grid’, plus ‘array’ and/or ‘vtk_image’. A slice outside the volume is filled with background (never None).

Return type:

dict

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> res = display.compute_array('Axial', display.position['Axial'])
>>> res = display.compute_array('Coronal', display.position['Coronal'], as_vtk=True)
compute_plane_geometry(plane, position, matrix_override=None)[source]#

Plane origin (= position, the slice’s top-left corner), view axes and spacing, used to place the output.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> geometry = display.compute_plane_geometry('Axial', display.position['Axial'])
>>> geometry['normal']
compute_slice_lines(plane, position)[source]#

Crosshair lines of the other two planes on this plane, keyed by plane name (see _intersection_line).

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> lines = display.compute_slice_lines('Axial', display.position['Axial'])
>>> lines['Sagittal']['pos'], lines['Sagittal']['angle']
get_grid(plane, position, matrix_override=None)[source]#

Output pixel grid for this plane: corner, axes, in-plane spacing and shape.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> grid = display.get_grid('Axial', display.position['Axial'])
>>> grid['shape'], grid['sx'], grid['sy']
go_to_plane(plane, origin, normal, x_axis=None)[source]#

Rotate the shared view so plane lies in a world plane, then scroll every plane through origin.

Used by markups and planes of interest to jump to the view they were drawn on / look straight at a cut. The rotation pivot and the crosshair move to origin. Every plane of this image turns with the shared rotation, so the other two show the target plane edge-on.

Parameters:
  • plane (str) – ‘Axial’, ‘Sagittal’ or ‘Coronal’: the view that should lie in the world plane.

  • origin ((3,) array_like) – World point on the plane; becomes the rotation center and crosshair.

  • normal ((3,) array_like) – World normal of the plane.

  • x_axis ((3,) array_like, optional) – In-plane direction the view’s x axis should follow. None keeps the current x axis as close as possible.

Returns:

The new position (top-left corner) of plane.

Return type:

(3,) ndarray

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> position = display.go_to_plane('Axial', [10.0, -40.0, 120.0], [0.0, 0.26, 0.97])
>>> res = display.compute_array('Axial', position)
reset()[source]#

Back to the native orientation: no rotation, pivot and crosshair at the image center, every plane at its initial slice.

Examples

>>> import medicalimageanalysis as mia
>>> mia.Data.image['CT 01'].display.reset()
set_rotation_center(center)[source]#

Set the rotation pivot (a click, ROI centroid, a Rigid’s center, …) and move the crosshair there.

Examples

>>> import medicalimageanalysis as mia
>>> image = mia.Data.image['CT 01']
>>> image.display.set_rotation_center(image.roi['Liver'].mesh.center)
update_rotation(r_x=0, r_y=0, r_z=0, absolute=True)[source]#

Rotate the shared view (every plane) and pivot the crosshair and every plane’s position to match.

Parameters:
  • r_x (float) – Degrees about the world x, y, z axes.

  • r_y (float) – Degrees about the world x, y, z axes.

  • r_z (float) – Degrees about the world x, y, z axes.

  • absolute (bool, default True) – True: the angles are the total rotation (e.g. read straight off a slider). False: they are added to the current rotation_totals.

Returns:

The incremental rotation applied this call (old -> new orientation), for pivot_position().

Return type:

(3, 3) ndarray

Notes

self.matrix is rebuilt from rotation_totals (Euler ‘xyz’) each call, not accumulated from deltas. Rotations about different axes don’t commute, so accumulating would make the orientation path-dependent; rebuilding keeps (0, 0, 0) the identity.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> R = display.update_rotation(r_x=0, r_y=0, r_z=15)
>>> R = display.update_rotation(r_z=1, absolute=False)
wheel_position(plane, position, steps=1, main=True)[source]#

Move a slice position along its plane’s normal by a number of scroll steps (one voxel each).

main=True also stores it as this plane’s position and moves the crosshair with it.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> new_position = display.wheel_position('Axial', display.position['Axial'], steps=-3)
pivot_position(position, R)[source]#

Rotate one widget’s position about rotation_center by R (the value returned by update_rotation()).

update_rotation already moves self.position; use this for positions the caller keeps itself.

Examples

>>> import medicalimageanalysis as mia
>>> display = mia.Data.image['CT 01'].display
>>> R = display.update_rotation(r_z=5)
>>> widget_position = display.pivot_position(widget_position, R)