0

I am using numpy and nibabel lib to load data from nii file. This is my code to load the image data

import os, glob
import nibabel as nib 
import numpy as np
import matplotlib.pyplot as plt

DataInputPath = '/content/drive/MyDrive/Data/Volumes/Img' 
ImgPath = os.path.join(DataInputPath, 'Patient1.nii')
Img = nib.load(ImgPath).get_fdata() 
np.min(Img), np.max(Img), Img.shape, type(Img) 

And the output (image properties) are as shown below:

"(-1000.0, 2976.0, (512, 512, 193), numpy.memmap)"

When I want to plot the image using matplotlib using the code below, the output images seems like cropped as it using the image shape as above 512x512x193. How do I change the dimension when I want to plot the slice from x and y, so that the image won't be cropped? Plotting the slice z seems okay.

Ex1: Plotting X slice and the image is cropped

ImgSlice = Img[100,:,:] 
plt.imshow(ImgSlice, cmap='gray')
plt.imshow
plt.show()

enter image description here

Ex2: Plotting Z where the image is okay

ImgSlice = Img[:,:,100] 
plt.imshow(ImgSlice, cmap='gray')
plt.imshow
plt.show()

enter image description here

jonedabb
  • 59
  • 6
  • In the first example, `ImgSlice = Img[100,:,:] ` which means the shape of the slice image is [1,512,193] which means only 1 y-axis pixel, you should glob a group of pixels, for example `ImgSlice = Img[:100,:,:]` or `ImgSlice = Img[100:200,:,:]` – CuCaRot Feb 19 '21 at 08:11
  • Hi @Toby thanks for you reply. The array is in 3d-dimensional, but there are 4 indexes from your example. – jonedabb Feb 19 '21 at 08:44
  • No, it's 3D, `Img[:100,:,:]` means you take the first 100 elements from the first axis and all elements in others dimension. – CuCaRot Feb 19 '21 at 08:50

0 Answers0