Search code examples
pythonimage-processingniftinibabelnipype

Down-sample mri T1 image in python with Nipy


I have a T1 image (NIFTI), already aligned, with dimension 121 x 145 x 121.
The image is loaded by nibabel. The voxel size is 1.5 x 1.5 x 1.5 mm.
I want to down-sample it to an image with 2.0 x 2.0 x 2.0 mm resolution and keep the images aligned.

I have little knowledge in MRI image manipulation. I couldn't find a clear tutorial.

How do I do that ? If you know any other Python library that can do it, it would also work.


Solution

  • I would suggest using Nibabel. It can downsample your nifti file in just a few lines.
    Example to resample to an image to a voxel size of 2x2x2:

    import nibabel
    import nibabel.processing
    
    input_path = r'/input/path/input_img.nii.gz'
    output_path = r'/output/path/output_img.nii.gz'
    voxel_size = [2, 2, 2]
    
    input_img = nibabel.load(input_path)
    resampled_img = nibabel.processing.resample_to_output(input_img, voxel_size)
    nibabel.save(resampled_img, output_path)
    

    Just update input_path and output_path to reflect your files. The second argument in the resample_to_output function (voxel_size) needs to either match the dimensions of your input or be a single value, nibabel will then assume you want the same voxel size for all dimensions.

    Nibabel info:
    Docs: http://nipy.org/nibabel/.
    Install instructions: https://anaconda.org/conda-forge/nibabel