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