Image Tools

Image I/O, preprocessing, and manipulation utilities.

Module Reference

Image Tools for PhysioMotion4D

This module provides utilities for converting between different medical image formats and performing image processing operations.

class physiomotion4d.image_tools.ImageTools(log_level=20)[source]

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 = ImageTools()
>>> # 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)
__init__(log_level=20)[source]

Initialize ImageTools.

Parameters:

log_level (int | str) – Logging level (default: logging.INFO)

imreadVD3(filename)[source]

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.

Parameters:

filename (str) – Path to the image file to read

Returns:

Vector image with double precision

Return type:

itk.Image[itk.Vector[itk.D,3],3]

Example

>>> displacement_field = ImageTools().imreadVD3('deformation.mha')
imwriteVD3(image, filename, compression=True)[source]

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.

Parameters:
  • 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)

Return type:

None

Example

>>> ImageTools().imwriteVD3(displacement_field, 'deformation.mha')
convert_itk_image_to_sitk(itk_image)[source]

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.

Parameters:

itk_image (Image) – Input ITK image (can be scalar or vector image)

Return type:

Image

Returns:

SimpleITK image with identical data and metadata

Note

Memory layout is preserved during conversion. Both ITK and SimpleITK use (z, y, x) ordering for 3D images in numpy arrays.

Example

>>> tools = ImageTools()
>>> itk_image = itk.imread('image.nii.gz')
>>> sitk_image = tools.convert_itk_image_to_sitk(itk_image)
convert_sitk_image_to_itk(sitk_image)[source]

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.

Parameters:

sitk_image (Image) – Input SimpleITK image (can be scalar or vector image)

Return type:

Image

Returns:

ITK image with identical data and metadata

Note

Memory layout is preserved during conversion. Both SimpleITK and ITK use (z, y, x) ordering for 3D images in numpy arrays.

Example

>>> tools = ImageTools()
>>> sitk_image = sitk.ReadImage('image.nii.gz')
>>> itk_image = tools.convert_sitk_image_to_itk(sitk_image)
convert_array_to_image_of_vectors(arr_data, reference_image, ptype=itk.D)[source]

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.

Return type:

Any

flip_image(in_image, in_mask=None, flip_x=False, flip_y=False, flip_z=False, flip_and_make_identity=False)[source]

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).

Parameters:
  • in_image (Image) – The input image to flip

  • in_mask (Optional[Image]) – The input mask to flip

  • flip_x (bool) – Flip the image and mask along the x-axis

  • flip_y (bool) – Flip the image and mask along the y-axis

  • flip_z (bool) – Flip the image and mask along the z-axis

  • flip_and_make_identity (bool) – Flip the image and mask and make the direction matrix identity.

Return type:

Any | tuple[Any, Any]

Navigation

Utility Modules | Transform Tools | Contour Tools