contours_to_mask_and_mesh#

medicalimageanalysis.utils.roi.to_mesh.contours_to_mask_and_mesh(contours, shape_zyx, origin, spacing, direction, reduction=0.5, ramp=None, interpolate=False, as_polydata=True, direction_rows=True, **kw)[source]

Build a mask and a surface mesh from one field build per region.

Cheaper than calling contours_to_mask and contours_to_mesh separately, and the two outputs come from the same field. By default only contours on adjacent image slices are joined, so missing slices leave gaps in both outputs.

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.

  • reduction (float) – Target decimation fraction for the mesh (0 = no decimation).

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

  • interpolate (bool) – How far apart contour planes can be and still be joined. Applies to both the mask and the mesh, which come from the same field. False (default): only contours on adjacent image slices are joined, using the slice spacing from spacing and direction. A missing slice leaves a gap in both outputs. origin, spacing and direction must describe the grid the contours were drawn on (for RTSTRUCT, the referenced image). 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, or sagittal / coronal contours. Gaps larger than 1.5x that spacing are still left open.

  • as_polydata (bool) – If True, the mesh is vtkPolyData. If False, (points, triangles).

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

  • **kw – Extra options passed to the region field build (_region_volumes).

Returns:

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

Return type:

tuple[np.ndarray, vtk.vtkPolyData | tuple[np.ndarray, np.ndarray]]

Examples

Get both outputs for one ROI:

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.to_contours import mask_to_contours
>>> from medicalimageanalysis.utils.roi.to_mesh import contours_to_mask_and_mesh
>>> 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, mesh = contours_to_mask_and_mesh(
...     rings, (8, 32, 32), origin, spacing, direction,
... )
>>> mask.dtype, mask.shape
(dtype('uint8'), (8, 32, 32))

Missing slices stay empty by default; 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]
>>> gapped, _ = contours_to_mask_and_mesh(
...     sparse, (8, 32, 32), origin, spacing, direction,
... )
>>> filled, _ = contours_to_mask_and_mesh(
...     sparse, (8, 32, 32), origin, spacing, direction,
...     interpolate=True,
... )
>>> bool(gapped[3, 16, 16] >= 128), bool(filled[3, 16, 16] >= 128)
(False, True)

Undecimated mesh as raw arrays:

>>> mask, (points, triangles) = contours_to_mask_and_mesh(
...     rings, (8, 32, 32), origin, spacing, direction,
...     reduction=0,
...     as_polydata=False,
... )