An Introduction to Display Neuroimaging DICOM Images in Python

In this article, we will introduce you on how to display neuroimaging DICOM images in Python.

Step 1: install libraries

You should install these python libraries:

nibabel==3.2.1
SimpleITK==2.0.2
numpy==1.19.5
pydicom==2.1.2
opencv-contrib-python==3.4.8.29
opencv-python==3.4.8.29
intensity-normalization==1.4.5
torch==1.8.1
imutils==0.5.4
matplotlib==3.3.4
pyrobex==0.4.1
dicom2nifti
scipy

You can save them in a requirements.txt file and use pip to install.

pip install -r requirements.txt

Step 2: display neuroimaging DICOM images

We will use an example to show you how to display. The full example code is here.

First, we import some libraries.

import os
import pydicom
import matplotlib.pyplot as plt

Then, we will display all dicom images in a directory.

folder_path = 'path'

X = []

for top, dir, f in os.walk(folder_path):
    for filename in f:
        ds = pydicom.dcmread(folder_path + '\\' + filename)
        X.append(ds)

window = plt.figure(figsize=(20,32))
n_cols = 6
n_rows = (len(X) // n_cols + 1)

for idx in range(0, len(X)):
    window.add_subplot(n_rows, n_cols, idx + 1)
    plt.imshow(X[idx].pixel_array, "gray")

Run this code, we may see:

An Introduction to Display Neuroimaging DICOM Images in Python