Read 3D NIFTI Images in Python3


Medical imaging is an essential tool in the diagnosis, treatment, and monitoring of various medical conditions. One of the most widely used medical imaging techniques is Magnetic Resonance Imaging (MRI), which produces three-dimensional images of the human body. These images are saved in a standard file format called NIFTI, which stands for Neuroimaging Informatics Technology Initiative.

In this article, we will discuss the following:

  1. Reading the NIFTI image
  2. Visualizing it
  3. Finally, save it to 2d images.

Python is a popular programming language used in many scientific fields, including medical imaging. There are several libraries available in Python that allow us to work with NIFTI files, such as nibabel and nilearn. Here, we will use the nibabel library to read a 3D NIFTI image.

First, we need to install the nibabel library. You can do this by running the following command in your terminal.

pip install nibabel

Reading NIFTI Image

Once you have installed the library, you can start by importing the necessary modules.

import nibabel as nib
import matplotlib.pyplot as plt
import cv2

Next, we will load the NIFTI file into Python.

nifti_img = nib.load('PANCREAS.nii.gz')

The nib.load() function reads the NIFTI file and returns an object of the Nifti1Image class, which represents the 3D image data. We can access various properties of the image, such as its shape, data type, and affine transformation matrix.

print(nifti_img.shape) # prints the shape of the image data
print(nifti_img.get_data_dtype()) # prints the data type of the image data
print(nifti_img.affine) # prints the affine transformation matrix


(512, 512, 240)
[[ -0.859375   0.        -0.         0.      ]
 [  0.        -0.859375   0.       439.140625]
 [  0.         0.        -1.         0.      ]
 [  0.         0.         0.         1.      ]]

Visualizing NIFTI Image

We can also visualize the image using the matplotlib library.

plt.imshow(nifti_img.get_fdata()[:, :, 50], cmap='gray')
The slice of the image at the z-coordinate of 50.
The slice of the image at the z-coordinate of 50.

Here, we are visualizing a slice of the image at the z-coordinate of 50. We use the get_fdata() function to get the image data as a NumPy array, which we can then visualize using imshow().

Saving NIFTI Image to 2D Images

In addition to reading 3D NIFTI images in Python, we can also save all slices of the image as individual JPEG files using the OpenCV library. OpenCV is a popular computer vision library that provides various image processing functions, including reading, writing, and manipulating images.

To save all slices of the 3D NIFTI image as individual JPEG files, we can use the following code.

# Get the image data as a NumPy array
image_data = nifti_img.get_fdata()

# Save each slice as a JPEG file
for i in range(image_data.shape[2]):
    slice_data = image_data[:, :, i]
    slice_data = cv2.normalize(slice_data, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    cv2.imwrite(f"data/slice_{i}.jpg", slice_data)

In the above code, we first load the NIFTI file using the nib.load() function and get the image data as a NumPy array using the get_fdata() function. We then loop through each slice of the image data along the z-axis and save it as a JPEG file using the cv2.imwrite() function.

Before saving each slice as a JPEG file, we normalize the pixel values in the range of 0 to 255 using the cv2.normalize() function. This ensures that the pixel values are within the range that can be saved as an 8-bit unsigned integer using the cv2.imwrite() function.

Finally, we save each slice as a JPEG file with a unique name using the f string format. This creates a series of JPEG files named slice_0.jpg, slice_1.jpg, slice_2.jpg, and so on, in the directory named data.

The figure above shows 25 different 2d images saved as jpg.
The figure above shows 25 different 2d images saved as jpg.


In conclusion, we can use Python and nibabel and OpenCV libraries to read and process 3D NIFTI images, and save all slices of the image as individual JPEG files. nibabel provides a convenient way to load NIFTI files and extract the image data as a NumPy array, which can be easily processed using various Python libraries. OpenCV provides functions for reading, writing, and manipulating images, and can be used to normalize and save the image slices as JPEG files. These techniques can be useful for a variety of applications, such as medical image analysis, computer vision, and machine learning.

Read More


I am an independent researcher in the field of Artificial Intelligence. I love to write about the technology I am working on.

Related Posts

Leave a Reply

Your email address will not be published.