Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Resampling in medical imaging

Guide to resampling medical imaging data using Python and SimpleITK to adjust voxel spacing

Resampling in medical imaging changes voxel spacing and the volume size that follows from it. A CT voxel is not a JPEG pixel: width × height × depth are physical millimetres. Change the spacing and you must recompute size, or the volume shrinks or inflates in the wrong units.

If you meant crop (same grid, smaller FOV), that is fast medical imaging cropping. If you meant MONAI Spacingd inside a compose for tumor segmentation, that is preprocessing 3D volumes in MONAI. If you meant what preprocessing is, that is what is image preprocessing.

Voxel, not pixel

In a photograph, a pixel is a square on a screen. In a CT or MRI, a voxel has a real size — the spacing. You use that size when you compute an organ volume or a surface. Two scanners of the “same” series can ship 0.7 mm and 1.2 mm spacing. Treat them as JPEGs and every millimetre-based number is a lie.

Resampling is the deliberate change of that spacing (and therefore of the array size). Reasons people actually do it:

  • Compute. Finer spacing is more voxels. Coarser spacing is a cheaper forward pass.
  • Standardize. Pool scans from two protocols onto one grid so a net is not learning scanner resolution.
  • Visualize / compress / real-time. Less detail than the acquisition, on purpose, because the viewer or the link cannot take the native volume.

New size from old spacing

You do not pick a new size and hope. You pick a new spacing. Size is then:

new_size[i] = round(old_size[i] * old_spacing[i] / new_spacing[i])

Keep origin and direction from the input. If you change size without that product, the physical FOV drifts and the volume no longer sits on the same anatomy.

Interpolator: linear vs nearest

After the new grid is defined, every new voxel is interpolated from the old ones.

  • Linear (sitk.sitkLinear) — intensities, CT, MRI. Smooth. The default for a scan you will window or feed to a net.
  • Nearest neighbour (sitk.sitkNearestNeighbor) — integer labels and masks. Linear on a mask invents class 1.4 between liver and background. That is not a label.

B-spline is sharper on intensities and slower. Start linear. Use nearest the moment the array is a segmentation.

SimpleITK ResampleImageFilter

Install the library that actually knows medical spacing:

pip install SimpleITK

Define the filter, set output spacing, recompute size from the product above, copy origin and direction, pick the interpolator, execute.

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())
# intensities: sitk.sitkLinear
# masks / integer labels: sitk.sitkNearestNeighbor
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())
print("Original Size:", sitk_img.GetSize())
print("Resampled Size:", resampled_img.GetSize())

That is the whole job on this page: spacing in, size from the product, interpolator that matches the array type, write a compressed NIfTI. A MONAI Spacingd in a tumor-seg compose is a different page. A crop that does not change spacing is a different page.

PYCAD builds the imaging side of this when the resampled volume has to live in a clinic app. Case studies.

We build custom medical imaging platforms — advanced DICOM viewers, AI segmentation, and the clinical systems around them.

Get in Touch

Copyright © 2026 PYCAD. All Rights Reserved.