0
votes

I have some medical images of nii.gz format which are of different shapes. I want to resize all to the same shape inorder to feed to a deep learnig model, I tried using resample_img() of nibabel, but it destroys my images. I want to do some other function just to resize it to a particular shape, say (512,512,129).

Someone please help me in this regard. I am stuck in this step for quite a good number of days.

2

2 Answers

2
votes

Maybe you can use this:

https://scikit-image.org/docs/dev/api/skimage.transform.html

I saw it in one of the papers. Here is the example in function ScaleToFixed:

https://github.com/sacmehta/3D-ESPNet/blob/master/Transforms.py

Here is how I did it. I have the volume of shape 320x320x130 (black and white so no rgb dimension). I want to make it twice as small. This worked for me:

import skimage.transform as skTrans
im = nib.load(file_path).get_fdata()
result1 = skTrans.resize(im, (160,160,130), order=1, preserve_range=True)
1
votes

You can use TorchIO:

import torchio as tio

image = tio.ScalarImage('path/to/image.nii.gz')
transform = tio.CropOrPad((512,512,129))
output = transform(image)

If you would like to keep the original field of view, you could use the Resample transform instead.

Disclaimer: I'm the main developer of TorchIO.