Grid#

class medicalimageanalysis.utils.roi.operations.Grid(shape_zyx, origin, spacing, direction)[source]

Bases: object

The CT grid all ROIs are put on (same conventions as the to_* conversions).

Parameters:
  • shape_zyx (tuple[int, int, int]) – Mask shape (nz, ny, nx).

  • origin (np.ndarray) – World (x, y, z) of voxel (0, 0, 0).

  • spacing (np.ndarray) – Voxel spacing (sx, sy, sz) in mm.

  • direction (np.ndarray) – 3x3 matrix; columns are the image i, j, k axes in world.

Examples

A plain 1 mm grid:

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.operations import Grid
>>> grid = Grid((16, 64, 64), np.zeros(3), np.ones(3), np.eye(3))

The grid of a loaded CT:

>>> from medicalimageanalysis import Data
>>> ct = Data.image['CT 01']
>>> grid = Grid(ct.array.shape, ct.origin, ct.spacing, ct.matrix)
shape_zyx: tuple
origin: ndarray
spacing: ndarray
direction: ndarray
property ramp

Width in mm of the 0..255 boundary ramp for masks made on this grid.

Two in-plane pixels, so both pixels either side of a boundary stay unclipped and mask -> contours can interpolate the true sub-pixel boundary.

Returns:

2 x the larger in-plane spacing.

Return type:

float

Examples

>>> import numpy as np
>>> grid = Grid((16, 64, 64), np.zeros(3), (0.8, 0.8, 3.0), np.eye(3))
>>> grid.ramp
1.6
property geom

The positional geometry arguments every conversion takes.

Returns:

(origin, spacing, direction).

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray]

Examples

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.to_contours import mask_to_contours
>>> grid = Grid((16, 64, 64), np.zeros(3), np.ones(3), np.eye(3))
>>> origin, spacing, direction = grid.geom
>>> rings = mask_to_contours(mask, *grid.geom, as_list=True)