A Beginner’s Guide to Commonly Used Libraries for “Medical Imaging”

Thebti Boutheina
6 min readApr 29, 2023

--

Understanding SimpleITK, PyRadiomics, Nibabel, and Pydicom for Medical Image Analysis

Welcome to my first blog post on Medium! As a data scientist, I am excited to share my knowledge and insights on the topic with you. In this post, I will be exploring commonly used libraries for medical imaging, including SimpleITK, PyRadiomics, Nibabel, and Pydicom. By the end of this post, you will have a better understanding of how these libraries can be used to improve image analysis and processing in the field of healthcare.

As we all know, medical imaging is a vital tool in healthcare, allowing doctors to diagnose and treat patients. However, analyzing different types of 2D or 3D medical imaging, such as CT scans, MRI scans, ultrasound images, and X-rays can be a complex and challenging task, especially when dealing with large datasets and they often pose challenges when it comes to processing and analyzing them.

So when we talk about medical images, we’re not just looking at a regular picture like the ones we see on Instagram or Facebook. Medical images, such as CT scans and MRIs, are special types of pictures that contain a lot of additional information beyond just the pixels that make up the image itself. This information is called metadata, and it includes things like the patient’s name, the date the image was taken, the type of imaging technique used, and the position of the patient’s body during the scan. This metadata is crucial for accurate diagnosis and treatment planning, and it’s important to be able to work with it in addition to the image data itself. That’s where libraries like Pydicom and SimpleITK come in, they allow you to not only read and manipulate the image data, but also to access and work with the metadata. To use these libraries in your Python environment, you may need to install them first by running pip install library_name. Let’s dive in!

In this blog, we will take a closer look at these four libraries and how they can help us tackle these issues.

Medical Imaging Librairies

SimpleITK

If you’re just starting out with medical image analysis, SimpleITK is a great library to get familiar with. It’s an easy-to-use Python library that makes working with medical images a breeze. With SimpleITK, you can easily read, write, and manipulate 2D and 3D medical images in various formats like DICOM, Nifti, and Analyze. The library also offers several image processing functionalities, like resampling, smoothing, registration, and segmentation, which are essential for medical image analysis.

One of the cool things about SimpleITK is its integration with NumPy. This means you can easily (1) convert a SimpleITK image object to a NumPy array using the GetArrayFromImage function. Conversely, you can also (2) convert a NumPy array to a SimpleITK image object using the GetImageFromArray function. These functions make it easy to work with medical images in Python and to switch between SimpleITK and NumPy as needed.

import SimpleITK as sitk
import numpy as np

# Load an image in SimpleITK format
sitk_image = sitk.ReadImage('example_image.nii.gz')

# (1)Convert the SimpleITK image to a numpy array
np_array = sitk.GetArrayFromImage(sitk_image)
# (2)Convert the numpy array to a SimpleITK image
sitk_image = sitk.GetImageFromArray(np_array)

Pydicom

Pydicom is a Python library that provides access to DICOM files, which are standard medical image files used in radiology, cardiology, and other medical imaging fields. With Pydicom, users can read and manipulate DICOM files, extract metadata and pixel data, modify headers, and perform image processing tasks. Pydicom is user-friendly and can be used in conjunction with other image processing libraries like SimpleITK and Nibabel. One of the advantages of Pydicom is that it provides support for both the classic DICOM and the newer DICOMweb protocol. Another advantage is its ability to handle compressed DICOM files. To import pydicom library and read an image in Python, you can use the following code:

import pydicom
from PIL import Image
# Read the DICOM image
ds = pydicom.dcmread('path/to/dicom/image.dcm')

# Extract patient name and birthday (metadata)
patient_name = ds.PatientName
patient_birthday = ds.PatientBirthDate
# Print the patient name
print("Patient Name: ", patient_name)

# Get the pixel data and shape of the image
pixel_data = ds.pixel_array
rows, cols = pixel_data.shape

# Normalize the pixel values to between 0 and 255
pixel_data = (np.maximum(pixel_data, 0) / pixel_data.max()) * 255.0
pixel_data = np.uint8(pixel_data)

# Create a PIL image from the pixel data
img = Image.fromarray(pixel_data)

# Save the image as PNG
img.save("path/to/png/image.png")

In this code, we first load the DICOM image using Pydicom’s dcmread function. We then extract the pixel data and shape of the image. We normalize the pixel values to between 0 and 255 to convert them to an 8-bit grayscale image, which is the format used by PNG images. We then create a PIL image from the pixel data using the Image.fromarray function. Finally, we save the image as PNG using the save method of the PIL image object.

Nibabel

Nibabel is a Python library for reading and writing neuroimaging data in various formats such as NIfTI, Analyze, MINC, etc. It provides a simple interface for working with complex medical image data and supports both 3D and 4D images. With Nibabel, you can load medical image files into Python as a numpy array, manipulate the data, and save it back to a file. One of the unique features of Nibabel is its ability to work with the affine transformation matrix associated with the image data, which can be used to transform the data into different coordinate systems. Nibabel also has built-in functions for resampling and reorienting images, as well as for performing various types of image processing such as smoothing, filtering, and segmentation. Here is an example Python code using Nibabel to load and manipulate a medical image:

import nibabel as nib
import numpy as np

# Load the medical image file
img = nib.load('image.nii.gz')

# Extract the image data as a numpy array
data = img.get_fdata()

# Manipulate the data (e.g. apply a Gaussian filter)
filtered_data = apply_gaussian_filter(data)

# Create a new NIfTI image object with the filtered data and the same affine transformation matrix
filtered_img = nib.Nifti1Image(filtered_data, img.affine)

# Save the filtered image to a new file
nib.save(filtered_img, 'filtered_image.nii.gz')

Here’s an example of using the dicom2nifti library to convert a DICOM series to a NIfTI file:

import dicom2nifti

# set input and output paths
input_folder = '/path/to/dicom/series'
output_file = '/path/to/output/nifti/file.nii.gz'

# convert the DICOM series to NIfTI format
dicom2nifti.convert_directory(input_folder, output_file)

PyRadiomics

PyRadiomics is a Python package for the extraction of radiomics features from medical imaging data. It provides a wide range of functionality for feature extraction, including support for both 2D and 3D image data, a variety of image pre-processing options, and a large selection of feature classes and feature calculators. The package also includes tools for image segmentation and co-registration, making it a powerful tool for medical image analysis. PyRadiomics is designed to be easy to use, with a simple API and extensive documentation and examples. It is compatible with a wide range of medical imaging formats, including DICOM and NIfTI, making it a flexible tool for use in a variety of medical imaging applications. Here is an example of how to read and manipulate an image using PyRadiomics in Python:

import SimpleITK as sitk
from radiomics import featureextractor

# Load the image
image = sitk.ReadImage('path/to/image.nii.gz')

# Create the PyRadiomics feature extractor
extractor = featureextractor.RadiomicsFeatureExtractor()

# Extract features from the image
features = extractor.execute(image)

# Print the features
for feature_name in features.keys():
print(feature_name, ':', features[feature_name])

In this example, we first load the image using SimpleITK, then create a RadiomicsFeatureExtractor object from PyRadiomics. We then use this extractor to extract features from the image, and finally print the resulting features. Note that PyRadiomics has a large number of features that can be extracted, so the output will be quite extensive.

Hey there, my fellow medical imaging enthusiasts! I hope you enjoyed reading this blog as much as I enjoyed writing it. We covered some of the most powerful and user-friendly libraries out there for processing medical images: SimpleITK, PyRadiomics, Nibabel, and Pydicom. These tools can make your life a whole lot easier when it comes to dealing with complex image data, from loading and manipulating images to extracting key features and even converting between file formats. It’s truly amazing how these libraries have simplified the process of working with medical images, enabling researchers and clinicians to better understand and diagnose medical conditions. I encourage you to give them a try and see how they can enhance your own work.

Thanks for joining me on this journey, and stay tuned for more exciting topics in the future!

--

--

Thebti Boutheina
Thebti Boutheina

Written by Thebti Boutheina

Data Scientist & Research Writer

Responses (4)