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.
Anyway, to actually load in the data, you can call the get_fdata () method, which will return a numpy array with the same dimensions as the image data. We’ll take a look at the anatomical MRI data ( anat.nii.gz ):
To run nipy's tests, you will need to install the nose Python testing package. If you are using Python 2.7, you will also need to install the mock testing package - e.g.: You can also run nipy's tests with the nipnost script in the tools directory of the nipy distribution:
Nibabel is an awesome Python package that allows us to read and load nifti images, and convert them to numpy arrays in a straightforward manner. We’ll load in an example anatomical MRI scan ( anat.nii.gz) from the current directory using nibabel below:
So you can assume that a nifti image of an fMRI file has 4 dimensions, with the first three being the spatial dimensions (similar to the anatomical MRI file: X × Y × Z) and the last (fourth) being the time dimension ( T ).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With