How to visualize Nifti Image in Python

You can visualize the Nifti Image in Python using the Matplotlib library. If you want to learn about computer vision, I strongly advise you to read This book.

Step 1: Install the NiBabel library

pip install nibabel

Step 2: Visualize the Nifti Image

# Import the required modules
import nibabel as nib
import matplotlib.pyplot as plt

# Load the Nifti File
image = nib.load('data.nii')

# Convert the image data as a NumPy array
array = image.get_fdata()

# Display the shape of NumPy array
print(array.shape)

# Display the one slice using matplotlib
plt.imshow(array[:,:,85], cmap='gray')
plt.show()

Output:

(240, 240, 155)

Related Articles:

Leave a Comment

Your email address will not be published.