[6fe801]: / utils / niftimanager.py

Download this file

34 lines (27 with data), 938 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import nibabel as nib
import matplotlib.pyplot as plt
def load_nifti(file_path):
'''
Load the NIfTI image and access the image data as a Numpy array.
Args:
file_path ('str'): Path to the NIfTI file.
Returns:
data_array ('np.array'): Numpy array representing the image data.
nii_image: Loaded NIfTI image object.
'''
nii_image = nib.load(file_path)
data_array = nii_image.get_fdata()
return data_array, nii_image
def show_nifti(file_data, title, slice=25):
'''
Display a single slice from the NIfTI volume.
Args:
file_data ('np.array'): Numpy array representing the image data.
title ('str'): Title for the plot.
slice ('int'): Slice index to display.
'''
plt.imshow(file_data[slice, :, :], cmap='gray')
plt.title(title)
# plt.colorbar()
plt.axis('off')
plt.show()