Isotropic voxel resampling

From wenmin-wu/ds-skills · View interactive version

Isotropic voxel resampling is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Resample 3D CT volumes to uniform voxel spacing using scipy zoom, normalizing physical dimensions across scanners

How to use this skill

Skill file

Isotropic Voxel Resampling

Overview

CT scanners produce volumes with non-uniform voxel spacing (e.g., 0.7×0.7×2.5 mm). Models expecting consistent spatial resolution need isotropic resampling. Compute the resize factor from original spacing to target (typically 1×1×1 mm), then apply scipy's zoom interpolation. Essential when mixing scans from different scanners or protocols.

Quick Start

import numpy as np
import scipy.ndimage

def resample(image, scan, new_spacing=[1, 1, 1]):
    """Resample a 3D volume to isotropic voxel spacing.
    
    Args:
        image: (D, H, W) numpy array
        scan: list of pydicom Dataset objects (for metadata)
        new_spacing: target voxel size in mm [z, y, x]
    Returns:
        resampled image, actual new spacing
    """
    spacing = np.array(
        [scan[0].SliceThickness] + list(scan[0].PixelSpacing),
        dtype=np.float32
    )
    resize_factor = spacing / new_spacing
    new_shape = np.round(image.shape * resize_factor)
    real_resize = new_shape / image.shape
    real_spacing = spacing / real_resize
    resampled = scipy.ndimage.zoom(image, real_resize, mode='nearest')
    return resampled, real_spacing

resampled_vol, spacing = resample(hu_volume, slices, [1, 1, 1])

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.