contours_to_mask#

medicalimageanalysis.utils.roi.to_mask.contours_to_mask(contours, shape_zyx, origin, spacing, direction, ramp=None, interpolate=False, direction_rows=True, **kw)[source]

Convert contours to a mask on the given image grid.

Two paths are chosen automatically:

  • Contours that lie on this image’s slices are filled directly on their slice. This is exact at image resolution and the fastest path. It is the usual case for an RTSTRUCT on its referenced image, or contours from mask_to_contours on the same grid.

  • Any other contours (sagittal, coronal, oblique, or drawn on a different grid) go through the shape-interpolation field, which is built per region and sampled at every voxel centre.

By default missing slices stay empty on both paths. Pass interpolate=True to fill them from the neighbouring contours; this always uses the field.

Parameters:
  • contours (list[np.ndarray]) – World-space (n, 3) rings.

  • shape_zyx (tuple[int, int, int]) – Output mask shape (nz, ny, nx).

  • origin (tuple[float, float, float]) – World (x, y, z) of voxel (0, 0, 0).

  • spacing (tuple[float, float, float]) – Voxel spacing (sx, sy, sz) in mm.

  • direction (np.ndarray) – 3x3 image direction matrix.

  • ramp (float | None) – Width in mm of the 0..255 boundary ramp. None uses the default.

  • interpolate (bool) – How far apart contour planes can be and still be joined. False (default): only contours on adjacent image slices are joined, using the slice spacing from spacing and direction. A missing slice is left empty, and the structure ends half a slice past its first and last contours. True: the slice spacing is the median spacing between contoured slices, so missing slices are filled by shape interpolation. Use this for deliberately sparse contours, contours drawn on a different grid than this one, or sagittal / coronal contours on an axial grid. Gaps larger than 1.5x that spacing are still left empty.

  • direction_rows (bool) – Layout of direction, forwarded to normalize_direction.

  • **kw – Extra options passed to the region field build (_region_volumes). Ignored when the contours are filled directly on their slices.

Returns:

uint8 mask (nz, ny, nx), 0..255, >= 128 inside.

Return type:

np.ndarray

See also

contours_to_mask_and_mesh

Field-based mask and mesh from one build.

Examples

Round-trip contours on every slice (filled directly on each slice):

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.to_contours import mask_to_contours
>>> from medicalimageanalysis.utils.roi.to_mask import contours_to_mask
>>> origin, spacing, direction = (0.0, 0.0, 0.0), (1.0, 1.0, 2.0), np.eye(3)
>>> box = np.zeros((8, 32, 32), dtype=np.uint8)
>>> box[2:6, 8:24, 8:24] = 1
>>> rings = mask_to_contours(box, origin, spacing, direction, as_list=True)
>>> mask = contours_to_mask(rings, (8, 32, 32), origin, spacing, direction)
>>> bool(mask[3, 16, 16] >= 128), bool(mask[0, 16, 16] >= 128)
(True, False)

Contours on every other slice. Gaps stay empty by default, and interpolate=True fills them:

>>> box = np.zeros((8, 32, 32), dtype=np.uint8)
>>> box[2:7, 8:24, 8:24] = 1
>>> by_slice = mask_to_contours(box, origin, spacing, direction)
>>> sparse = by_slice[2] + by_slice[4] + by_slice[6]
>>> gapped = contours_to_mask(sparse, (8, 32, 32), origin, spacing, direction)
>>> filled = contours_to_mask(sparse, (8, 32, 32), origin, spacing, direction,
...                           interpolate=True)
>>> bool(gapped[3, 16, 16] >= 128), bool(filled[3, 16, 16] >= 128)
(False, True)

Use the result as a boolean mask:

>>> inside = mask >= 128
>>> volume_cc = inside.sum() * np.prod(spacing) / 1000