"""
Image Tools for MONAI Physio
This module provides utilities for converting between different medical image formats
and performing image processing operations.
"""
import logging
from typing import Any, Optional, Union, cast, overload
import itk
import numpy as np
import SimpleITK as sitk
from numpy.typing import NDArray
from .monai_physio_base import MONAIPhysioBase
[docs]
class ProcessImages(MONAIPhysioBase):
"""
Utilities for medical image format conversions and processing.
This class provides methods for converting between ITK (Insight Toolkit) and
SimpleITK image formats while preserving all metadata (origin, spacing, direction,
pixel type). Supports both scalar and vector (multi-component) images.
Example:
>>> tools = ProcessImages()
>>> # Convert ITK to SimpleITK
>>> sitk_image = tools.convert_itk_image_to_sitk(itk_image)
>>> # Convert back to ITK
>>> itk_image_back = tools.convert_sitk_image_to_itk(sitk_image)
"""
[docs]
def __init__(self, log_level: int | str = logging.INFO) -> None:
"""Initialize ProcessImages.
Args:
log_level: Logging level (default: logging.INFO)
"""
super().__init__(class_name=self.__class__.__name__, log_level=log_level)
[docs]
@staticmethod
def index_to_world(image: itk.Image, kji: np.ndarray) -> np.ndarray:
"""Return the world positions of NumPy array indices of *image*.
*kji* rows are ``(k, j, i)`` indices of ``itk.GetArrayFromImage``,
reversed relative to the image's ``(i, j, k)`` index; they may be
fractional. The array starts at the buffered region's index
``start``, so the position is ``origin + D @ ((start + ijk) * spacing)``.
"""
origin = np.asarray(image.GetOrigin(), dtype=np.float64)
spacing = np.asarray(image.GetSpacing(), dtype=np.float64)
direction = itk.array_from_matrix(image.GetDirection()).astype(np.float64)
start = np.asarray(image.GetBufferedRegion().GetIndex(), dtype=np.float64)
ijk = np.asarray(kji, dtype=np.float64)[..., ::-1] + start
return np.asarray(origin + (ijk * spacing) @ direction.T)
[docs]
@staticmethod
def world_to_index(image: itk.Image, points: np.ndarray) -> np.ndarray:
"""Return the fractional ``(k, j, i)`` array indices of world points.
The inverse of :meth:`index_to_world`.
"""
origin = np.asarray(image.GetOrigin(), dtype=np.float64)
spacing = np.asarray(image.GetSpacing(), dtype=np.float64)
direction = itk.array_from_matrix(image.GetDirection()).astype(np.float64)
start = np.asarray(image.GetBufferedRegion().GetIndex(), dtype=np.float64)
offset = np.asarray(points, dtype=np.float64) - origin
ijk = offset @ np.linalg.inv(direction).T / spacing - start
return np.asarray(ijk[..., ::-1])
[docs]
def imread_vd3(self, filename: str) -> Any:
"""Read an ITK vector image with double precision vectors.
ITK's imread is not wrapped for itk.Image[itk.Vector[itk.D,3],3],
so this method reads as itk.Image[itk.Vector[itk.F,3],3] and converts
to double precision.
Args:
filename (str): Path to the image file to read
Returns:
itk.Image[itk.Vector[itk.D,3],3]: Vector image with double precision
Example:
>>> displacement_field = ProcessImages().imread_vd3('deformation.mha')
"""
# Read as float precision vector image
image = itk.imread(filename)
if "VD" in str(type(image)):
return image
image_arr = itk.array_from_image(image)
image_double = self.convert_array_to_image_of_vectors(image_arr, image, itk.D)
return image_double
[docs]
def imwrite_vd3(self, image: Any, filename: str, compression: bool = True) -> None:
"""Write an ITK vector image with double precision vectors.
ITK's imwrite is not wrapped for itk.Image[itk.Vector[itk.D,3],3],
so this method converts to itk.Image[itk.Vector[itk.F,3],3] and writes.
Args:
image (itk.Image[itk.Vector[itk.D,3],3]): Vector image to write
filename (str): Path to the output file
compression (bool): Whether to use compression (default: True)
Example:
>>> ProcessImages().imwrite_vd3(displacement_field, 'deformation.mha')
"""
# Convert to float precision for writing
if "VD" not in str(type(image)):
raise ValueError("Image must be a vector image with double precision")
image_arr = itk.array_from_image(image)
image_float = self.convert_array_to_image_of_vectors(image_arr, image, itk.F)
# Write the float image
itk.imwrite(image_float, filename, compression=compression)
[docs]
def convert_itk_image_to_sitk(self, itk_image: itk.Image) -> sitk.Image:
"""
Convert an ITK image to a SimpleITK image.
This method converts an ITK (Insight Toolkit) image to SimpleITK format while
preserving all metadata including origin, spacing, direction, and pixel type.
Works with both scalar and vector (multi-component) images.
Args:
itk_image: Input ITK image (can be scalar or vector image)
Returns:
SimpleITK image with identical data and metadata
Example:
>>> tools = ProcessImages()
>>> itk_image = itk.imread('image.nii.gz')
>>> sitk_image = tools.convert_itk_image_to_sitk(itk_image)
"""
array = itk.array_from_image(itk_image)
# Get image metadata
origin = itk.origin(itk_image)
spacing = itk.spacing(itk_image)
direction = itk.array_from_matrix(itk_image.GetDirection())
# Check if this is a vector image
is_vector = False
if hasattr(itk_image, "GetNumberOfComponentsPerPixel"):
n_components = itk_image.GetNumberOfComponentsPerPixel()
is_vector = n_components > 1
if is_vector:
sitk_image = sitk.GetImageFromArray(array, isVector=True)
else:
sitk_image = sitk.GetImageFromArray(array, isVector=False)
# Set metadata
# Convert origin and spacing to tuples (reverse order for SimpleITK: x, y, z)
sitk_image.SetOrigin(tuple(origin))
sitk_image.SetSpacing(tuple(spacing))
# Direction matrix needs to be flattened
# ITK and SimpleITK use the same direction convention, we just need to flatten it correctly
direction_flat = direction.flatten()
sitk_image.SetDirection(direction_flat.tolist())
return sitk_image
[docs]
def convert_sitk_image_to_itk(self, sitk_image: sitk.Image) -> itk.Image:
"""
Convert a SimpleITK image to an ITK image.
This method converts a SimpleITK image to ITK (Insight Toolkit) format while
preserving all metadata including origin, spacing, direction, and pixel type.
Works with both scalar and vector (multi-component) images.
Args:
sitk_image: Input SimpleITK image (can be scalar or vector image)
Returns:
ITK image with identical data and metadata
Example:
>>> tools = ProcessImages()
>>> sitk_image = sitk.ReadImage('image.nii.gz')
>>> itk_image = tools.convert_sitk_image_to_itk(sitk_image)
"""
array = sitk.GetArrayFromImage(sitk_image)
# Get image metadata
origin = sitk_image.GetOrigin() # Returns (x, y, z)
spacing = sitk_image.GetSpacing() # Returns (x, y, z)
direction = sitk_image.GetDirection() # Returns flattened direction matrix
dimension = sitk_image.GetDimension()
n_components = sitk_image.GetNumberOfComponentsPerPixel()
# Check if this is a vector image
is_vector = n_components > 1
# Create ITK image from numpy array
if is_vector:
# Vector image
itk_image = itk.image_from_array(array, is_vector=True)
else:
# Scalar image
itk_image = itk.image_from_array(array, is_vector=False)
# Set origin (reverse order: SimpleITK gives x,y,z, ITK expects x,y,z internally
# but we set it using the same order)
itk_image.SetOrigin(origin)
# Set spacing
itk_image.SetSpacing(spacing)
# Set direction matrix
# Reshape direction to matrix form
if dimension == 2:
direction_matrix = np.array(direction).reshape(2, 2)
elif dimension == 3:
direction_matrix = np.array(direction).reshape(3, 3)
else:
raise ValueError(f"Unsupported image dimension: {dimension}")
# Convert numpy array to ITK matrix and set
itk_direction = itk.matrix_from_array(direction_matrix)
itk_image.SetDirection(itk_direction)
return itk_image
[docs]
def convert_array_to_image_of_vectors(
self,
arr_data: NDArray[Any],
reference_image: Any,
ptype: Any = itk.D,
) -> Any:
"""
Convert a numpy array to an ITK image of vector type.
This method is needed because itk in python does not support creating
images of vectors with itk.D precision. Luckily array_view_from_image
does support itk.D precision vectors.
"""
if ptype not in [itk.F, itk.D]:
if ptype == np.float32:
ptype = itk.F
elif ptype == np.float64:
ptype = itk.D
else:
raise ValueError(f"Unsupported component type: {ptype}")
itk_image = itk.Image[itk.Vector[ptype, 3], 3].New()
itk_image.SetRegions(reference_image.GetLargestPossibleRegion())
itk_image.SetSpacing(reference_image.GetSpacing())
itk_image.SetOrigin(reference_image.GetOrigin())
itk_image.SetDirection(reference_image.GetDirection())
itk_image.Allocate()
itk.array_view_from_image(itk_image)[:] = arr_data
return itk_image
[docs]
def convert_intensity_image(
self, image: itk.Image, dtype: Optional[type] = None
) -> itk.Image:
"""Prepare a scalar image for the wrapped ITK intensity filters.
Args:
image: Scalar 3-D intensity image, including an in-memory image.
dtype: Explicit output type: ``np.float32``, ``np.float64``, or
``np.int16``. With None, preserve uint8, int16, uint16, float32,
and float64; convert other integers to float64. Integers outside
float64's exact range are rejected rather than rounded silently,
including when float64 is requested explicitly.
Explicit int16 conversion rounds ties to even and clips to
[-32768, 32767]; nonfinite values are rejected.
Returns:
Image with the same grid. Already suitable images are returned as is.
Raises:
ValueError: For a nonscalar/non-3-D image, unsupported output type,
nonfinite int16 input, or integers outside the exact float64 range.
"""
if image.GetImageDimension() != 3 or image.GetNumberOfComponentsPerPixel() != 1:
raise ValueError("Expected a scalar 3-D intensity image")
values = itk.array_view_from_image(image)
if dtype is None:
if values.dtype in (np.uint8, np.int16, np.uint16, np.float32, np.float64):
return image
dtype = np.float64
if dtype not in (np.float32, np.float64, np.int16):
raise ValueError("dtype must be np.float32, np.float64, or np.int16")
if (
dtype == np.float64
and np.issubdtype(values.dtype, np.integer)
and values.size
and (int(values.min()) < -(2**53) or int(values.max()) > 2**53)
):
raise ValueError("Integer intensities exceed float64's exact range")
if values.dtype == np.dtype(dtype):
return image
if dtype == np.int16:
if not np.isfinite(values).all():
raise ValueError("Cannot convert nonfinite intensities to int16")
values = np.clip(np.rint(values), -32768, 32767)
result = itk.image_from_array(values.astype(dtype))
result.CopyInformation(image)
result.SetRegions(image.GetLargestPossibleRegion())
return result
[docs]
def make_isotropic_image(self, image: itk.Image, spacing: float = 0) -> itk.Image:
"""Resample a 3-D *image* to isotropic spacing.
Args:
image: 3-D ITK image to resample.
spacing: Isotropic spacing to resample to. If 0, the finest voxel
pitch of *image* is used.
Returns:
Resampled image with uniform spacing.
Raises:
ValueError: If *image* is not 3-D or spacing is negative/nonfinite.
"""
if image.GetImageDimension() != 3:
raise ValueError(
f"make_isotropic_image requires a 3-D image; "
f"got {image.GetImageDimension()}-D"
)
image = self.convert_intensity_image(image)
input_spacing = np.asarray(image.GetSpacing(), dtype=np.float64)
if not np.isfinite(spacing) or spacing < 0:
raise ValueError("spacing must be finite and nonnegative")
size = np.asarray(image.GetLargestPossibleRegion().GetSize(), dtype=np.int64)
min_spacing = float(input_spacing.min()) if spacing == 0 else float(spacing)
new_spacing = [min_spacing] * 3
# Ceiling to avoid clipping the image boundary.
new_size = [
int(np.ceil(size[i] * input_spacing[i] / min_spacing)) for i in range(3)
]
ImageType = type(image)
interpolator = itk.LinearInterpolateImageFunction[ImageType, itk.D].New()
resampler = itk.ResampleImageFilter[ImageType, ImageType].New()
resampler.SetInput(image)
resampler.SetInterpolator(interpolator)
resampler.SetOutputSpacing(new_spacing)
resampler.SetSize(new_size)
resampler.SetOutputOrigin(image.GetOrigin())
resampler.SetOutputDirection(image.GetDirection())
resampler.Update()
result = resampler.GetOutput()
result.DisconnectPipeline()
return result
[docs]
def resample_image_by_scale(
self, image: itk.Image, scale: float, interpolate: bool = True
) -> itk.Image:
"""Resample a 3-D *image* to *scale* times its voxel count per axis.
The physical extent is preserved: spacing is rescaled to compensate for
the new voxel count, and the origin shifts by half the spacing change so
the resampled voxel centers stay inside the original extent.
Args:
image: 3-D ITK image to resample.
scale: Per-axis voxel-count multiplier. Values below ``1.0``
coarsen, values above ``1.0`` upsample.
interpolate: Use linear interpolation. ``False`` selects nearest
neighbor, which is what labelmaps need.
Returns:
Resampled image, covering the same physical extent as *image*.
Raises:
ValueError: If *image* is not 3-D, or *scale* is not positive.
"""
if image.GetImageDimension() != 3:
raise ValueError(
f"resample_image_by_scale requires a 3-D image; "
f"got {image.GetImageDimension()}-D"
)
if scale <= 0.0:
raise ValueError(f"scale must be positive; got {scale}")
spacing = np.asarray(image.GetSpacing(), dtype=np.float64)
size = np.asarray(image.GetLargestPossibleRegion().GetSize(), dtype=np.int64)
new_size = np.maximum(1, np.ceil(size * scale)).astype(np.int64)
new_spacing = spacing * size / new_size
ImageType = type(image)
if interpolate:
interpolator = itk.LinearInterpolateImageFunction[ImageType, itk.D].New()
else:
interpolator = itk.NearestNeighborInterpolateImageFunction[
ImageType, itk.D
].New()
direction = itk.array_from_matrix(image.GetDirection())
resampler = itk.ResampleImageFilter[ImageType, ImageType].New()
resampler.SetInput(image)
resampler.SetInterpolator(interpolator)
resampler.SetOutputSpacing([float(v) for v in new_spacing])
resampler.SetSize([int(n) for n in new_size])
resampler.SetOutputOrigin(
np.asarray(image.GetOrigin(), dtype=np.float64)
+ direction @ ((new_spacing - spacing) / 2.0)
)
resampler.SetOutputDirection(image.GetDirection())
resampler.Update()
result = resampler.GetOutput()
result.DisconnectPipeline()
return result
@staticmethod
def _per_axis_values(
value: Union[float, list, tuple, NDArray[Any]],
dimension: int,
name: str,
) -> list[float]:
"""Broadcast a scalar to every axis, or validate a per-axis sequence.
Args:
value: Scalar applied to every axis, or one value per axis.
dimension: Number of image dimensions expected.
name: Parameter name, used in error messages.
Returns:
One value per axis.
Raises:
ValueError: If a sequence has the wrong length, or any value is
negative.
"""
if np.isscalar(value):
values = [float(cast(float, value))] * dimension
else:
values = [float(v) for v in value] # type: ignore[union-attr]
if len(values) != dimension:
raise ValueError(
f"{name} needs a scalar or one value per image dimension "
f"({dimension}), got {len(values)}."
)
if any(v < 0.0 for v in values):
raise ValueError(f"{name} must be >= 0, got {values}")
return values
[docs]
def pad_image(
self,
image: itk.Image,
pad_portion: Optional[
Union[float, list[float], tuple[float, ...], NDArray[Any]]
] = None,
pad_voxels: Optional[
Union[int, list[int], tuple[int, ...], NDArray[Any]]
] = None,
background_value: float = 0.0,
) -> itk.Image:
"""Pad *image* on every side with a constant-valued margin.
The margin is given either as a portion of each axis' physical extent
(*pad_portion*) or directly in voxels (*pad_voxels*); exactly one of the
two must be supplied. Either accepts a scalar, applied to every axis, or
one value per image dimension. Both pad the lower and the upper end of
every axis, so ``pad_voxels=10`` grows all six faces of a 3-D image by
ten voxels. Spacing and direction are untouched.
The origin and size are updated together so the original voxels keep
their physical positions: the padded image's index ``(0, 0, 0)`` sits one
margin below the input's, and the input data occupies the interior.
(``itk.ConstantPadImageFilter`` alone reports the margin as a negative
start index instead, which most file formats drop on write - shifting the
data. The region-of-interest pass here folds that index back into the
origin.)
Args:
image: ITK image to pad.
pad_portion: Portion of an axis' physical extent (``size * spacing``)
to add at both ends, as a scalar for every axis or one value per
dimension: ``0.1`` grows an axis spanning 200 mm by 20 mm per
side. Rounded up to whole voxels. Mutually exclusive with
*pad_voxels*.
pad_voxels: Margin in voxels, as a scalar for every axis or one value
per dimension, applied at both ends of each axis. Mutually
exclusive with *pad_portion*.
background_value: Pixel value written into the new margin
(default: 0.0).
Returns:
Padded image with the same pixel type, spacing and direction.
Raises:
ValueError: If neither or both of *pad_portion* and *pad_voxels* are
given, if either is negative, or if a sequence does not have one
entry per image dimension.
"""
if (pad_portion is None) == (pad_voxels is None):
raise ValueError(
"Specify exactly one of pad_portion or pad_voxels; got "
f"pad_portion={pad_portion}, pad_voxels={pad_voxels}."
)
size = [int(s) for s in image.GetLargestPossibleRegion().GetSize()]
if pad_portion is not None:
portions = self._per_axis_values(pad_portion, len(size), "pad_portion")
# extent_i = size_i * spacing_i, and pad_portion * extent_i of margin
# is that distance divided by spacing_i, so the spacing cancels.
margin = [int(np.ceil(p * s)) for p, s in zip(portions, size)]
self.log_info(
"Padding by %s voxels per side (%s of extent); size %s -> %s",
margin,
[f"{p * 100.0:.1f}%" for p in portions],
size,
[s + 2 * p for s, p in zip(size, margin)],
)
else:
assert pad_voxels is not None # guaranteed by the check above
margin = [
int(v)
for v in self._per_axis_values(pad_voxels, len(size), "pad_voxels")
]
self.log_info(
"Padding by %s voxels per side; size %s -> %s",
margin,
size,
[s + 2 * p for s, p in zip(size, margin)],
)
ImageType = type(image)
# SetConstant is typed to the pixel type, so an integer image rejects a
# Python float.
pixel_type = itk.template(image)[1][0]
constant = (
float(background_value)
if pixel_type in (itk.F, itk.D)
else round(background_value)
)
pad_filter = itk.ConstantPadImageFilter[ImageType, ImageType].New()
pad_filter.SetInput(image)
pad_filter.SetPadLowerBound(margin)
pad_filter.SetPadUpperBound(margin)
pad_filter.SetConstant(constant)
pad_filter.Update()
padded = pad_filter.GetOutput()
# Re-anchor the padded region at index 0, moving the margin into the
# origin so the physical position of the original data is preserved.
roi_filter = itk.RegionOfInterestImageFilter[ImageType, ImageType].New()
roi_filter.SetInput(padded)
roi_filter.SetRegionOfInterest(padded.GetLargestPossibleRegion())
roi_filter.Update()
result = roi_filter.GetOutput()
result.DisconnectPipeline()
return result
[docs]
def crop_to_mask(
self, image: itk.Image, mask: itk.Image, margin_mm: float = 0.0
) -> itk.Image:
"""Crop an image to a mask's nonzero bounds and surrounding tissue.
Args:
image: Scalar 3-D image to crop, preserving its pixel type.
mask: Nonempty scalar mask or labelmap on exactly the same grid.
Every nonzero label contributes to the bounds.
margin_mm: Nonnegative physical margin, rounded up per axis and
clipped to the input extent. Surrounding image voxels are kept.
Returns:
Cropped image with unchanged voxel positions, spacing and direction.
Raises:
ValueError: For invalid margins, empty masks, or mismatched grids.
"""
if not np.isfinite(margin_mm) or margin_mm < 0:
raise ValueError("margin_mm must be finite and nonnegative")
if (
image.GetImageDimension() != 3
or mask.GetImageDimension() != 3
or image.GetNumberOfComponentsPerPixel() != 1
or mask.GetNumberOfComponentsPerPixel() != 1
or image.GetLargestPossibleRegion() != mask.GetLargestPossibleRegion()
or not np.allclose(image.GetSpacing(), mask.GetSpacing())
or not np.allclose(image.GetOrigin(), mask.GetOrigin(), rtol=0, atol=1e-6)
or not np.allclose(image.GetDirection(), mask.GetDirection())
):
raise ValueError(
"image and mask must be scalar 3-D images on the same grid"
)
foreground = itk.array_view_from_image(mask)
start, stop = self._nonzero_bounds(foreground)
spacing = np.asarray(image.GetSpacing())
margins = np.ceil(margin_mm / spacing).astype(int)[::-1]
for axis in range(3):
start[axis] = max(0, int(start[axis] - margins[axis]))
stop[axis] = min(foreground.shape[axis], int(stop[axis] + margins[axis]))
values = itk.array_view_from_image(image)
result = itk.image_from_array(
np.ascontiguousarray(
values[tuple(slice(a, b) for a, b in zip(start, stop))]
)
)
result.SetSpacing(image.GetSpacing())
result.SetDirection(image.GetDirection())
index = np.asarray(image.GetLargestPossibleRegion().GetIndex())
result.SetOrigin(
np.asarray(image.GetOrigin())
+ itk.array_from_matrix(image.GetDirection())
@ (spacing * (index + np.asarray(start[::-1])))
)
return result
@staticmethod
def _nonzero_bounds(values: np.ndarray) -> tuple[list[int], list[int]]:
"""Find array bounds with axis projections rather than foreground indices."""
foreground = values != 0
if not foreground.any():
raise ValueError("mask must contain at least one nonzero voxel")
start, stop = [], []
for axis in range(values.ndim):
others = tuple(other for other in range(values.ndim) if other != axis)
hits = np.flatnonzero(foreground.any(axis=others))
start.append(int(hits[0]))
stop.append(int(hits[-1]) + 1)
return start, stop
[docs]
def crop_to_nonzero(
self, labelmap_image: itk.Image, pad_voxels: int = 0
) -> itk.Image:
"""Return *labelmap_image* cropped to its nonzero voxels, then zero-padded.
The voxels keep their physical positions: the origin moves with the
crop and the pad, while spacing and direction are unchanged.
Args:
labelmap_image: Labelmap or mask holding at least one nonzero voxel.
pad_voxels: Zero voxels added around the crop on every side.
Returns:
The cropped labelmap, as uint16.
Raises:
ValueError: If the labelmap has no nonzero voxels.
"""
labels = itk.GetArrayViewFromImage(labelmap_image)
# The array's axes are reversed relative to the image, so the crop's
# start comes out as (z, y, x) and is flipped for the origin.
start, stop = self._nonzero_bounds(labels)
cropped_arr = np.pad(
labels[tuple(slice(a, b) for a, b in zip(start, stop))], pad_voxels
)
cropped = itk.GetImageFromArray(
np.ascontiguousarray(cropped_arr.astype(np.uint16))
)
spacing = np.asarray(labelmap_image.GetSpacing(), dtype=np.float64)
direction = itk.array_from_matrix(labelmap_image.GetDirection())
cropped.SetSpacing(labelmap_image.GetSpacing())
cropped.SetDirection(labelmap_image.GetDirection())
cropped.SetOrigin(
np.asarray(labelmap_image.GetOrigin(), dtype=np.float64)
+ direction @ (spacing * (np.array(start[::-1]) - pad_voxels))
)
return cropped
[docs]
def binary_dilate_image(
self,
image: itk.Image,
radius: int,
foreground_value: int = 1,
background_value: int = 0,
) -> itk.Image:
"""Binary-dilate *image* with a ball structuring element.
Args:
image: Binary (or label) image to dilate.
radius: Radius, in voxels, of the ball structuring element.
foreground_value: Pixel value treated as foreground (default: 1).
background_value: Pixel value written for background voxels
(default: 0).
Returns:
Dilated image with the same pixel type as *image*.
"""
ImageType = type(image)
dimension = image.GetImageDimension()
StructuringElementType = itk.FlatStructuringElement[dimension]
structuring_element = StructuringElementType.Ball(int(radius))
dilate_filter = itk.BinaryDilateImageFilter[
ImageType, ImageType, StructuringElementType
].New()
dilate_filter.SetInput(image)
dilate_filter.SetKernel(structuring_element)
dilate_filter.SetForegroundValue(foreground_value)
dilate_filter.SetBackgroundValue(background_value)
dilate_filter.Update()
result = dilate_filter.GetOutput()
result.DisconnectPipeline()
return result
[docs]
def binary_erode_image(
self,
image: itk.Image,
radius: int,
foreground_value: int = 1,
background_value: int = 0,
) -> itk.Image:
"""Binary-erode *image* with a ball structuring element.
Args:
image: Binary (or label) image to erode.
radius: Radius, in voxels, of the ball structuring element.
foreground_value: Pixel value treated as foreground (default: 1).
background_value: Pixel value written for eroded-away voxels
(default: 0).
Returns:
Eroded image with the same pixel type as *image*.
"""
ImageType = type(image)
dimension = image.GetImageDimension()
StructuringElementType = itk.FlatStructuringElement[dimension]
structuring_element = StructuringElementType.Ball(int(radius))
erode_filter = itk.BinaryErodeImageFilter[
ImageType, ImageType, StructuringElementType
].New()
erode_filter.SetInput(image)
erode_filter.SetKernel(structuring_element)
erode_filter.SetForegroundValue(foreground_value)
erode_filter.SetBackgroundValue(background_value)
erode_filter.Update()
result = erode_filter.GetOutput()
result.DisconnectPipeline()
return result
[docs]
def keep_largest_connected_component(
self,
image: itk.Image,
foreground_value: int = 1,
fully_connected: bool = False,
) -> itk.Image:
"""Keep only the largest connected component of a binary image.
Args:
image: Binary (non-zero = foreground) image.
foreground_value: Value written for the retained component's
voxels (default: 1).
fully_connected: Whether diagonally-adjacent voxels are
considered connected (default: False, i.e. face connectivity
only).
Returns:
Binary image, same pixel type as *image*, containing only the
largest connected component with value *foreground_value*
(background is 0).
"""
# SimpleITK's filters are not templated on pixel type in the Python
# layer, so this avoids the itk.ConnectedComponentImageFilter /
# itk.RelabelComponentImageFilter Python wrappings, which only cover
# a limited set of input/output pixel type combinations that vary
# across ITK Python builds.
ImageType = type(image)
sitk_image = self.convert_itk_image_to_sitk(image)
cc_image = sitk.ConnectedComponent(sitk_image, fully_connected)
relabeled = sitk.RelabelComponent(cc_image, sortByObjectSize=True)
largest = sitk.BinaryThreshold(
relabeled,
lowerThreshold=1,
upperThreshold=1,
insideValue=foreground_value,
outsideValue=0,
)
result = self.convert_sitk_image_to_itk(largest)
if type(result) is ImageType:
return result
return itk.cast_image_filter(result, ttype=(type(result), ImageType))
@overload
def flip_image(
self,
in_image: itk.Image,
in_mask: None = None,
flip_x: bool = False,
flip_y: bool = False,
flip_z: bool = False,
flip_and_make_identity: bool = False,
) -> itk.Image: ...
@overload
def flip_image(
self,
in_image: itk.Image,
in_mask: itk.Image,
flip_x: bool = False,
flip_y: bool = False,
flip_z: bool = False,
flip_and_make_identity: bool = False,
) -> tuple[itk.Image, itk.Image]: ...
[docs]
def flip_image(
self,
in_image: itk.Image,
in_mask: Optional[itk.Image] = None,
flip_x: bool = False,
flip_y: bool = False,
flip_z: bool = False,
flip_and_make_identity: bool = False,
) -> Union[itk.Image, tuple[itk.Image, itk.Image]]:
"""
Flip the image and mask.
Only axis-aligned flips are supported. If ``flip_and_make_identity`` is
True, the image and mask are first flipped along any axes whose
corresponding diagonal entries in the direction matrix are negative
(assuming the direction matrix encodes only axis-aligned flips), then
any additional requested flips are performed, and finally the direction
matrix is set to the identity matrix. This is useful when combining ITK
images with VTK objects (that often do not support a direction matrix).
Args:
in_image: The input image to flip
in_mask: The input mask to flip
flip_x: Flip the image and mask along the x-axis
flip_y: Flip the image and mask along the y-axis
flip_z: Flip the image and mask along the z-axis
flip_and_make_identity: Flip the image and mask and make the direction
matrix identity.
"""
flip0 = False
flip1 = False
flip2 = False
if flip_and_make_identity:
# itk.array_from_matrix avoids itk.Matrix.__array__, whose missing
# copy keyword triggers a numpy>=2.0 DeprecationWarning.
direction = itk.array_from_matrix(in_image.GetDirection())
flip0 = direction[0, 0] < 0
flip1 = direction[1, 1] < 0
flip2 = direction[2, 2] < 0
if flip_x:
flip0 = True
if flip_y:
flip1 = True
if flip_z:
flip2 = True
if flip0 or flip1 or flip2:
self.log_info(f"Flipping image: {flip0}, {flip1}, {flip2}")
flip_filter = itk.FlipImageFilter.New(Input=in_image)
flip_filter.SetFlipAxes([int(flip0), int(flip1), int(flip2)])
flip_filter.SetFlipAboutOrigin(True)
flip_filter.Update()
out_image = flip_filter.GetOutput()
if flip_and_make_identity:
id_mat = itk.Matrix[itk.D, 3, 3]()
id_mat.SetIdentity()
out_image.SetDirection(id_mat)
if in_mask is not None:
flip_filter = itk.FlipImageFilter.New(Input=in_mask)
flip_filter.SetFlipAxes([int(flip0), int(flip1), int(flip2)])
flip_filter.SetFlipAboutOrigin(True)
flip_filter.Update()
out_mask = flip_filter.GetOutput()
if flip_and_make_identity:
id_mat = itk.Matrix[itk.D, 3, 3]()
id_mat.SetIdentity()
out_mask.SetDirection(id_mat)
return out_image, out_mask
else:
return out_image
else:
if in_mask is not None:
return in_image, in_mask
else:
return in_image