Resampling in Medical Imaging

Facebook
Twitter
LinkedIn

What do you know about the spacing?

Sometimes, we use a tool or a method in our daily projects without knowing where it originated! This happened to me a few years ago when I first started working in the medical imaging field. I was using many tools I found on the internet but was skipping the phase where I needed to learn the basics. One of the important things I was overlooking was voxel spacing.

We need to understand that medical data has its own characteristics and can’t be treated like a normal image. In medical imaging, a pixel is called a voxel, and each voxel has a size (ideally, all voxels have the same size). This voxel size is what we refer to as spacing. The dimensions of a voxel — width, height, and depth — are the real physical dimensions, and we use these when calculating the volume or surface area of an organ or anatomical structure in the body.

Now we come to “Resampling,” the subject of this post. Resampling means changing the dimensions of the voxel and the overall image (volume) size. This change serves several purposes, including:

  1. Reducing Computational Complexity: Higher resolution images often require more computational resources for processing and analysis. By increasing the spacing (decreasing the resolution), you can reduce the computational complexity while still retaining sufficient information for diagnosis or analysis.
  2. Data Standardization: Medical imaging data may come from different scanners or imaging protocols, resulting in variations in spatial resolution. Resampling can be used to standardize the resolution across different datasets, making them more comparable and facilitating consistent analysis.
  3. Improving Visualization: In some cases, high-resolution images may contain more detail than necessary for visualization or interpretation. By reducing the resolution through resampling, you can create images that are easier to visualize without sacrificing essential diagnostic information.
  4. Data Compression: Resampling can also be used as a form of data compression, especially for large volumetric datasets. By increasing the spacing between voxels, you can reduce the storage requirements of the image data without significantly impacting its diagnostic utility.
  5. Addressing Computational Limits: In certain scenarios where computational resources are limited, such as real-time processing or remote diagnostics, resampling can help optimize the data for efficient analysis within the available constraints.



Let’s Code It!

There are several ways to apply resampling to medical data, but I recommend using a proper library for medical imaging, such as SimpleITK. This library helps perform this operation cleanly and efficiently. First, you need to install the library:

pip install SimpleITK

SimpleITK works similarly to VTK in these scenarios (if you’re familiar with it). You need to define the filter, configure its parameters, and then apply it to your data. Changing the spacing can be complex. You need to define the new size of the volume along the three axes, ensure the origin and direction remain the same as the input file, and finally, define the interpolator to be used after resizing. Once all configurations are set, you can execute the resampler on the input image, as shown in the code below:

import SimpleITK as sitk

nifti_path = "input_scan.nii.gz"
sitk_img = sitk.ReadImage(nifti_path)

original_spacing = sitk_img.GetSpacing()
new_spacing = (0.4, 0.4, 0.4)
resample_filter = sitk.ResampleImageFilter()
resample_filter.SetDefaultPixelValue(0)
resample_filter.SetOutputSpacing(new_spacing)
resample_filter.SetSize([int(round(osz*ospc/nspc)) for osz, ospc, nspc in zip(sitk_img.GetSize(), original_spacing, new_spacing)])
resample_filter.SetOutputDirection(sitk_img.GetDirection())
resample_filter.SetOutputOrigin(sitk_img.GetOrigin())
resample_filter.SetInterpolator(sitk.sitkLinear)

resampled_img = resample_filter.Execute(sitk_img)

sitk.WriteImage(resampled_img, "data/resampled.nii.gz", useCompression=True)

print("Original Spacing:", sitk_img.GetSpacing())
print("Resampled Spacing:", resampled_img.GetSpacing())

Useful links

More to explorer

Fast Medical Imaging Cropping

Efficiently split large NIfTI medical images into smaller chunks for easier processing and analysis using SimpleITK.