I have some 3D Niftii datasets of brain MRI scans (FLAIR, T1, T2,..). The FLAIR scans for example are 144x512x512 with Voxel Size of 1.1, 0.5, 0.5 and I want to have 2D-slices from axial, coronal and sagittal view, which I use as input for my CNN.
What I want to do: Read in .nii files with nibabel, save them as Numpy array and store the slices from axial, coronal and sagittal as 2D-PNGs.
What I tried:
-Use med2image python library
-wrote own python script with nibabel, Numpy and image
PROBLEM: The axial and coronal pictures are somehow stretched in one direction. Sagittal works out like it should.
I tried to debug the python script and used Matplotlib to show the array, that I get, after
image = nibabel.load(inputfile)
image_array=image.get_fdata()
by using for example:
plt.imshow(image_array[:,:, 250])
plt.show()
and found out, the data is already stretched there.
I could figure out to get the desired output with
header = image.header
sX=header['pixdim'][1]
sY=header['pixdim'][2]
sZ=header['pixdim'][3]
plt.imshow(image_array[:,:, 250],aspect=sX/sZ)
But how can I apply something like "aspect", when saving my image? Or is there a possibility to already load the .nii file with parameters like that, to have data, that I can work with?
It looks like, the pixel dimensions are not taken care of, when nibabel loads the .nii image. But unfortunately there's no way for me to solve this problem..