2

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.

Shilpa Mathew
  • 31
  • 1
  • 2

3 Answers3

3

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)
3

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.

fepegar
  • 595
  • 5
  • 15
0

It has been a while since the question has been asked I will provide an answer which worked for me and hopefully can help others!

patient = r'/Data_folder' # string containing path to image
file_name = os.path.basename(patient)
image = tio.ScalarImage(patient)
transform = tio.CropOrPad((480, 480, 360))
output = transform(image)

save_path1 = r'/Data_folder/resized/'

if os.path.exists(save_path1):
    print('Path exists defining save path')
    save_path = save_path1 + file_name 
else:
    print('Path doesnt  exists creating path and defining save path')
    os.mkdir(save_path1)
    save_path = save_path1 + file_name 

output.save(save_path)
Anonymous
  • 61
  • 5