union#

medicalimageanalysis.utils.roi.operations.union(rois, grid, margins=None, output='mesh', reduction=0.5, **kw)[source]

Union of several ROIs: rois[0] | rois[1] | …

Parameters:
  • rois (list) – ROIs (contours, meshes or masks; types can be mixed).

  • grid (Grid) – Common CT grid.

  • margins (list | None) – One margin per ROI (mm, scalar or (x, y, z)), applied before the union. None for no margins.

  • output (str) – ‘mesh’, ‘contours’ or ‘mask’.

  • reduction (float) – Decimation fraction when output=’mesh’.

  • **kw – Extra options passed to the contour / mesh conversions.

Returns:

The union in the requested format.

Return type:

vtk.vtkPolyData | list[np.ndarray] | np.ndarray

Raises:

ValueError – If the number of margins does not match the number of ROIs.

Examples

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.operations import Grid, dilate, union
>>> grid = Grid((16, 64, 64), np.zeros(3), np.ones(3), np.eye(3))
>>> a = np.zeros((16, 64, 64), dtype=np.uint8)
>>> a[4:12, 16:40, 16:40] = 255
>>> b = np.zeros((16, 64, 64), dtype=np.uint8)
>>> b[4:12, 28:52, 28:52] = 255

Combine two ROIs:

>>> both = union([a, b], grid, output="mask")
>>> bool(both[8, 20, 20] >= 128), bool(both[8, 48, 48] >= 128)
(True, True)

Margin on the first ROI only:

>>> mesh = union([a, b], grid, margins=[5, 0])

Chain operations: PTV = (CTV1 | CTV2) + 5 mm

>>> ctv = union([ctv1, ctv2], grid, output="mask")
>>> ptv = dilate(ctv, grid, 5)