A MONAI transform pipeline for a 3D volume
Normalising the intensity of an MRI volume, cropping it and adding random augmentation.
Goal: Harmonising voxel size (spacing) between scans from different machines.
import SimpleITK as sitk
img = sitk.Image(128, 128, 40, sitk.sitkFloat32)
img.SetSpacing((0.8, 0.8, 3.0))
img.SetOrigin((-50.0, -50.0, 0.0))
new_spacing = (1.0, 1.0, 1.0)
new_size = [round(sz * sp / ns) for sz, sp, ns in zip(img.GetSize(), img.GetSpacing(), new_spacing)]
res = sitk.Resample(img, new_size, sitk.Transform(), sitk.sitkLinear, img.GetOrigin(),
new_spacing, img.GetDirection(), 0.0, img.GetPixelID())
print("Size and spacing before:", img.GetSize(), img.GetSpacing())
print("Size and spacing after:", res.GetSize(), res.GetSpacing())
print("Origin preserved:", res.GetOrigin())
Medical images have a physical spacing in millimetres. We resample to 1×1×1 mm, computing the new size from the ratio, and use linear interpolation for the image (for masks use nearest neighbour). Spatial information (origin and direction) is preserved.
Size and spacing before: (128, 128, 40) (0.8, 0.8, 3.0) Size and spacing after: (102, 102, 120) (1.0, 1.0, 1.0) Origin preserved: (-50.0, -50.0, 0.0)
This example was actually run and tested on 21 November 2026 on a CPU — SimpleITK 2.5.8, python 3.13.18
Normalising the intensity of an MRI volume, cropping it and adding random augmentation.
Applying realistic distortions and extracting patches for training.
Applying an ITK filter to a 3D image through its functional Python API.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.