contour_longest_distance#

medicalimageanalysis.utils.roi.statistics.contour_longest_distance(contours, exact=False)[source]

Longest in-plane distance across a stack of contours, in mm.

Each contour is handled on its own and the largest result is returned. A candidate segment only counts if it lies inside the contour polygon, so the distance never crosses a concave notch. (N, 3) contours are projected onto their best-fit plane first.

Parameters:
  • contours (list[np.ndarray]) – Contours as (N, 2) or (N, 3) point arrays in mm.

  • exact (bool) – If False, only convex hull vertices are tested (fast, can undercount on concave contours). If True, every contour point is tested (slower, exact).

Returns:

Longest internal distance in mm, or 0.0 if no valid segment is found.

Return type:

float

Examples

Diagonal of a 20 x 10 mm rectangle:

>>> import numpy as np
>>> from medicalimageanalysis.utils.roi.statistics import contour_longest_distance
>>> rectangle = np.array([[0, 0], [20, 0], [20, 10], [0, 10]])
>>> round(contour_longest_distance([rectangle], exact=True), 2)
22.36

The same rectangle as a 3D contour lying on z = 5 mm:

>>> rectangle_3d = np.c_[rectangle, np.full(4, 5.0)]
>>> round(contour_longest_distance([rectangle_3d]), 2)
22.36

Largest value across a stack of slices:

>>> small = rectangle * 0.5
>>> round(contour_longest_distance([small, rectangle]), 2)
22.36