contour_positions#

medicalimageanalysis.utils.roi.to_contours.contour_positions(contours, origin, direction, direction_rows=True)[source]

Positions of the planes a contour list lies on, in mm along the image k axis.

Each ring’s mean distance from origin along the k axis is rounded to 0.001 mm, de-duplicated and sorted. Typical use is cutting a mesh back at the originally contoured slices with mesh_to_contours.

Parameters:
  • contours (list[np.ndarray]) – World-space (n, 3) rings. Rings with fewer than 3 points are ignored.

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

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

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

Returns:

Sorted unique plane positions in mm from the origin along k.

Return type:

np.ndarray

Examples

Contours traced from a mask with 2 mm slices:

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.to_contours import (
...     contour_positions,
...     mask_to_contours,
... )
>>> mask = np.zeros((8, 32, 32), dtype=np.uint8)
>>> mask[2:6, 8:24, 8:24] = 1
>>> rings = mask_to_contours(
...     mask, (0.0, 0.0, 0.0), (1.0, 1.0, 2.0), np.eye(3), as_list=True,
... )
>>> contour_positions(rings, (0.0, 0.0, 0.0), np.eye(3))
array([ 4.,  6.,  8., 10.])

Hand-made rings, two on the same plane:

>>> square = np.array([[0, 0], [10, 0], [10, 10], [0, 10]], dtype=float)
>>> rings = [np.c_[square, np.full(4, z)] for z in (3.0, 3.0, 6.0)]
>>> contour_positions(rings, (0.0, 0.0, 0.0), np.eye(3))
array([3., 6.])