I have a nifti file 1.nii.gz
Now, i never dealt with nifti files.
So, just opening it using this software i realized that a nii.gz is a sort of container that contains 3 arrays of 2d pictures. In fact, if i scroll the mouse i can see 448 2d picture for the "direction" labeled in the picture as 1, 448 2d pictures for the "direction" 2 and 25 2d pictures for the "direction" 3.
After this, i opened the shell and i tried to use this nii.gz with Nibabel library
import nibabel as nib
img = nib.load(1.nii.gz)
But, if i type
img.shape
i get (448,448,25) as result, so it seems that this .nii.gz is a 3d matrix and not a container with 3 arrays of 2d pictures. Can you explain me ?
Nifti
is a medical images format, to store both images, and companied data, the images are usually in grayscale, and they are taken as slices, each slice with a different cross-section of the body.
They store all the slices in the same array, and sometimes they take the slices during different times so sometimes they add a fourth dimension to the array.
So to show the images, or manipulate them, you can slice them and see the images inside.
In your case the shape of your data (448,448,25) tells that:
There are 25 images (slices) with dimensions 448 x 448
import nibabel as nib
import matplotlib.pyplot as plt
# Change the path to your path
path = 'path to img.nii.gz'
Nifti_img = nib.load(path)
nii_data = my_img.get_fdata()
nii_aff = my_img.affine
nii_hdr = my_img.header
print(nii_aff ,'\n',nii_hdr)
print(nii_data.shape)
if(len(nii_data.shape)==3):
for slice_Number in range(nii_data.shape[2]):
plt.imshow(nii_data[:,:,slice_Number ])
plt.show()
if(len(nii_data.shape)==4):
for frame in range(nii_data.shape[3]):
for slice_Number in range(nii_data.shape[2]):
plt.imshow(nii_data[:,:,slice_Number,frame])
plt.show()