3

I have a nifti file 1.nii.gz

enter image description here

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 ?

AleWolf
  • 133
  • 1
  • 6

2 Answers2

9

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()
Bilal
  • 3,191
  • 4
  • 21
  • 49
  • Thanks so much for you reply. I really appreciate it. If the file 1.nii.gz contains 25 images with dimensions 448x448 (== the images relative to the lateral view of the head, labeled with the number 3 in the picture from my post), where the 448 slices of label 1 and the 448 slices of label 2 are extracted from ? – AleWolf May 16 '20 at 16:57
  • 1
    @AleWolf that's another question, if you want me to answer that question you have to provide a sample of your Dataset, so I can see and understand what's exactly inside, sometimes they put the images in one nifti, and the mask(ground truth) on another one, so I can't judge without a sample of your data. – Bilal May 17 '20 at 01:25
  • Here a sample https://easyupload.io/368cv3 Thank you infinitely , i would really appreciate if you explained clearly these kinds of details since i never worked with nifti files and i know very little about them – AleWolf May 17 '20 at 08:53
  • 1
    @AleWolf I have checked your file, it contains only images, no labels inside, the labels might be on another file, this file is a group of grayscale images. – Bilal May 17 '20 at 09:10
  • For labels, i intended the labels I have drawed with the red color in the picture of my initial post . I write again my last question: If the file 1.nii.gz contains 25 images with dimensions 448x448 (== the images relative to the lateral view of the head, labeled with the number 3 in the picture from my post), where the 448 slices of label 1 and the 448 slices of label 2 come from ? – AleWolf May 17 '20 at 09:16
  • @AleWolf sorry for misunderstanding, now I think that I have got your question, the other images in your post I guess they are coming from the software that you are using, it makes 3D reconstruction of the brain, and reprojects that on the other planes. – Bilal May 17 '20 at 09:19
  • Mhm, that means that generally a nifty file is an array of 2d slices ? In this case, an array of 25 slices (with dimension 448x448) ? – AleWolf May 17 '20 at 09:20
  • 1
    @AleWolf yes in your case there is no time frames, only slices. – Bilal May 17 '20 at 09:21
  • Time frames means : "the same MRI exam (with the same number of slices), but taken at different moment" ? – AleWolf May 17 '20 at 09:22
  • @AleWolf yes exactly. – Bilal Sep 12 '21 at 14:20
0

If you want to view the full sized image in plot

import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt

test_load = nib.load('imgx/label0002.nii.gz').get_fdata()
print(test_load.shape)

#this will plot full siz size 
plt.imshow(test_load[:,:,test_load.shape[2]//2])
plt.show()