Labelmap Tools
Convert segmentation labelmaps into binary registration masks, with optional label exclusion and physically isotropic dilation, and clean binary masks.
Mask Cleanup
remove_islands() drops the disconnected pieces of a
mask (ITK ConnectedComponentImageFilter then
RelabelComponentImageFilter): by default it keeps only the largest
face-connected component, or with min_island_volume_mm3 every component
at least that large. fill_holes() fills the cavities
the mask encloses (ITK BinaryFillholeImageFilter) and leaves openings to
the image border alone. Both return a 0/1 itk.UC mask.
Run them before tetrahedral meshing: every island and enclosed cavity becomes
an extra boundary shell, and
extract_label_tetrahedra_netgen() can fail on a
surface whose shells crowd each other.
from monai_physio import ProcessLabelmaps, ProcessVolumetricMesh
tools = ProcessLabelmaps()
solid = tools.fill_holes(tools.remove_islands(mask))
meshes = ProcessVolumetricMesh().extract_label_tetrahedra_netgen(
solid, element_size_mm=2.0
)
Module Reference
Labelmap Tools for MONAI Physio
This module provides the ProcessLabelmaps class with the definitive
utility for turning a multi-label (or binary) segmentation labelmap into a
binary registration mask, optionally excluding specific labels and dilating
the result by a physical radius in millimeters, and for cleaning binary masks
by removing islands and filling enclosed holes.
- class monai_physio.process_labelmaps.ProcessLabelmaps(log_level=20)[source]
Utilities for converting segmentation labelmaps into registration masks.
A labelmap is an
itk.Imageof integer labels where0is background and each positive value identifies an anatomical structure. A registration mask is a binaryitk.Imagewhere every foreground voxel is1. This class centralizes the labelmap-to-mask conversion so that thresholding, label exclusion, and physically isotropic dilation are performed identically everywhere in the platform.Example
>>> tools = ProcessLabelmaps() >>> # Binary mask of every labeled voxel, dilated 5 mm >>> mask = tools.convert_labelmap_to_mask(labelmap, mask_dilation_mm=5.0) >>> # Exclude the table/background labels 8 and 9 before masking >>> mask = tools.convert_labelmap_to_mask( ... labelmap, mask_dilation_mm=5.0, exclude_labels=[8, 9] ... ) >>> # One solid piece, e.g. before tetrahedral meshing >>> mask = tools.fill_holes(tools.remove_islands(mask))
- convert_labelmap_to_mask(labelmap, mask_dilation_mm=0.0, exclude_labels=None)[source]
Convert a labelmap into a binary registration mask.
Any voxel whose label is in
exclude_labelsis set to background first; every remaining non-zero voxel becomes foreground (1). The binary mask is then dilated bymask_dilation_mmmillimeters of physical radius. The radius is converted into per-axis voxel counts from the labelmap’s spacing so the dilation is physically isotropic even on anisotropic grids; each per-axis count is clamped to at least 1 voxel whenmask_dilation_mm > 0.- Parameters:
labelmap (
Image) – Multi-label or binaryitk.Image. Any non-zero voxel that is not excluded is treated as foreground.mask_dilation_mm (
float) – Physical radius of the binary dilation in millimeters. Pass0(or negative) to skip dilation and return the raw thresholded mask. Default 0.0.exclude_labels (
Optional[list[int]]) – Optional list of integer label values to force to background before thresholding. WhenNone(the default) no labels are excluded.
- Return type:
Image- Returns:
itk.Image[itk.UC, 3]binary mask in the same physical space aslabelmap(origin, spacing, direction copied from the input).
- remove_islands(mask, min_island_volume_mm3=None)[source]
Remove the disconnected pieces of a binary mask.
Foreground voxels are grouped into face-connected components with ITK’s
ConnectedComponentImageFilter. Voxels that touch only along an edge or at a corner belong to different components.- Parameters:
- Return type:
Image- Returns:
itk.Image[itk.UC, N]0/1 mask in the same physical space as mask; all zero if mask is empty.
- fill_holes(mask)[source]
Fill the enclosed holes of a binary mask.
ITK’s
BinaryFillholeImageFiltersets to foreground every background region that is not face-connected to the image border, so cavities enclosed by the mask are filled while openings to the outside are kept.- Parameters:
mask (
Image) – Binary mask; any non-zero voxel is foreground.- Return type:
Image- Returns:
itk.Image[itk.UC, N]0/1 mask in the same physical space as mask.
- create_distance_map(labelmap, max_distance_mm=20.0, distance_scale=5.0, preserve_labels=True, fill_background_only=False, exclude_labels=None)[source]
Encode a labelmap as a continuous label-plus-boundary-distance image.
Each output voxel holds its original integer label plus a small fractional offset that encodes how far the voxel lies from the nearest boundary between two differently-labeled regions:
- value = label + min(distance_to_nearest_boundary_mm,
max_distance_mm) / distance_scale
The boundary set is every voxel that 6-neighbors a voxel with a different label (background label
0participates, so the outer surface of each structure is a boundary). The unsigned physical distance from each voxel to that set is computed withSignedMaurerDistanceMapImageFilter(taking the magnitude), clipped tomax_distance_mm, divided bydistance_scale, and added to the voxel’s original label.With the defaults (
20mm clip,5scale) the fractional offset stays in[0.0, 4.0], potentially passing adjacent integer labels but emphasizing in medial alignment as well as boundary.The motivation is registration metrics such as Greedy’s NCC: a raw integer labelmap is piecewise-constant, so the local variance inside each region is zero and NCC produces NaN gradients. Replacing it with this continuous encoding gives every region a smoothly varying signal while preserving label identity.
- Parameters:
labelmap (
Image) – Multi-label (or binary)itk.Imageof integer labels.max_distance_mm (
float) – Distance clip, in millimeters. Default 20.0.distance_scale (
float) – Divisor applied to the clipped distance before it is added to the label. Default 5.0. With the default clip this bounds the fractional offset to[0, 4.0].
- Return type:
Image- Returns:
itk.Image[itk.F, 3]in the same physical space aslabelmap(origin, spacing, direction copied from the input).
Navigation