How to change the dimensions of MRIs (in Nifti format) in python?

Hi neurosStars

I have 700 MRIs. each MRI with the shape (170,256,256,1). and I need to change these MRIs to the shape (no of the MRIs (700), 256,256,1). so I can feed them to the deep learning segmentation model.

for img in range(len(image_names_subset)):
  image_dataset = nib.load(image_names_subset[img]) #read the nifti images

image_dataset=nib.squeeze_image(image_dataset) #squeeze the image

image_dataset = processing.conform(image_dataset, out_shape=(256, 256, 1)) #change the dimensions


(256, 256, 1)

after I run this code the number of images does not exist. I got (256,256,1) and what I expected is (700,256,256,1).

Any clarification will help


It seems like you would be losing a whole axis of data if you did this. If each MRI is a 3D volume (dimensions 170X256X256), and your final desired shape is N_ScansX256X256, it seems like you are getting rid of the x-axis. Is that what you want?


Yes, I want to work with 2D MRIs instead of 3D. because the segmentation model that I am trying to implement is expecting these dimensions (N_Scans,256, 256,1).
if there is another way to do that without losing the x-axis I want to know, please.

You can load an image in with image = nilearn.image.load_img(path_to_img), then slice it with x_slice = image.slicer[X_COORD,:,:]. You can store these x_slices across subjects in a list and then use concat_img = nilearn.image.concat_imgs(list_of_slices) to create the concatenated image, and then save it out with concat_img.to_filename(outpath).