How to convert a normal array into nifti file using Python

Facebook
Twitter
LinkedIn

Video version of this blog post

Introduction

When working in the healthcare field, there are two types of files that can be used. The first are dicom files, which are files that represent a 2D slice of a body part that you wish to study. Then there’s the 3D representation of the body, which is a collection of 2D slices built from multiple dicoms or even regular images or arrays.

In previous articles, we discussed the differences between dicom and nifti, as well as how to create nifti files from dicoms. So, in this article, we’ll go over how to generate these nifti files from a standard array.

Why do we need to do this conversion?

You may wonder why we need to perform such conversions. The answers are as follows:

  • They can be used if you have normal JPG or PNG images, which you convert into dicoms and then into nifti, but you can see that this requires two steps, so converting the images from JPG or PNG to nifti will be easier when you use the method in this article, and you will see that it is very simple.
  • They can be CSV files that represent volumes, as we can find in many datasets, so if you need them in a single nifti file that you want to use in a special case, you can use the method described in this article.
  • And the most commonly used method, and the one I required for my project, was that when you perform a segmentation task, you must save the output mask as a single nifti file; for this, this method will be your solution, as it was in mine.

Tools you need

You will need two libraries for this task: numpy and nibabel. Nibabel is a library designed specifically for nifti extensions, but the conversion array/nifti requires a numpy array as input.

To install them, you can use pip as always (or conda):

pip install numpy

pip install nibabel

Methodology

There are two steps to this process that you must complete:

  • Convert the array you have from any type to a numpy array; if the array you have represents images, you must specify the data types, such as float for images and int for masks (if you are doing segmentation, but it can be float as well).

converted_array = numpy.array(normal_array, dtype=numpy.float32)

I chose float 32 as the type because I needed it, but you can use any type you need.

  • The second step is to use one line of code to convert the numpy array into a nifti file. In the nibabel library, there is a function called ‘Nifti1Image’ that takes 5 parameters, three of which are optional and the other two are required. In my case, the first two parameters were sufficient, but if you require more, you can add them. The first parameter is the numpy array that we just converted, and the second parameter is affine, which is a homogeneous affine that provides a relationship between voxel coordinates (3D representation of a pixel) and world coordinates (from the documentation). This second parameter can be None or a 4×4 array; for our purposes, we will use a 4×4 array created by the numpy.eye function.

The other parameters aren’t required right now, but there is one that you might need later: ‘header’. This parameter will take the header of your file, if you have one. Which is the metadata (patient and image information).

nifti_file = nibabel.Nifti1Image(converted_array, affine)

With:

affine = numpy.eye(4)

Your nifti file is now ready to use; you can use it right away or save it to use later.

Another command in the nibabel library called ‘save’ can be used to save the file.

nibabel.save(nifti_file, path_to_save)

Here is the full script:

import numpy as np
import nibabel as nib

converted_array = numpy.array(normal_array, dtype=numpy.float32) # You need to replace normal array by yours
affine = numpy.eye(4)
nifti_file = nibabel.Nifti1Image(converted_array, affine)

nibabel.save(nifti_file, path_to_save) # Here you put the path + the extionsion 'nii' or 'nii.gz'

I hope you found this article useful, because we will need these conversions when we discuss data augmentation for 3D volumes in the next article.

Find a sponsor for your web site. Get paid for your great content. shareasale.com.

More to explorer

Making Sense of AI in Medical Images

Explore how AI revolutionizes medical imaging, enhancing diagnosis and treatment. Dive into real-world AI applications for better healthcare outcomes.