Interactive processing of image files

Interactive processing of image files#

In the medical field, there are lots of particular image formats incoorporating image sequences, metadata, and more. For example, n-dimensional TIFF files or DICOM files.

The Python ecosystem provides several libraries to handle, process and visualize such file formats. See, for example:

This notebook uses data from the Kaggle dataset “dlwpt-volumetric-dicom-lung”:

import os
import numpy as np

# We use imageio to automatically load and handle n-dimensional TIF images 
import imageio.v3 as iio
# We use pydicom to load and process DICOM files
import pydicom
# We use stackview for the interactive visualization of the images and its slices
import stackview

Load and process TIF with sequential imagery#

# Read the image
image = iio.imread('data_mrt/Haase_MRT_tfl3d1.tif')
# Read metadata for the image
meta = iio.immeta('data_mrt/Haase_MRT_tfl3d1.tif')
# Inspect the available keys for the metadata
print(meta.keys())
dict_keys(['byteorder', 'is_indica', 'is_fluoview', 'is_tvips', 'is_philips', 'is_vista', 'is_micromanager', 'is_astrotiff', 'is_scanimage', 'is_imagej', 'ImageJ', 'images', 'slices', 'unit', 'loop', 'Info', 'is_virtual', 'is_tiffep', 'is_nuvu', 'is_shaped', 'is_sis', 'is_nih', 'is_lsm', 'is_epics', 'is_volumetric', 'is_subifd', 'is_mrc', 'is_eer', 'is_fei', 'is_streak', 'is_svs', 'is_frame', 'is_mdgel', 'is_mediacy', 'is_pilatus', 'is_qpi', 'is_multipage', 'is_uniform', 'is_agilent', 'is_stk', 'is_ome', 'is_scn', 'is_andor', 'is_mmstack', 'is_ndtiff', 'is_metaseries', 'is_gdal', 'is_sem', 'is_ndpi', 'is_avs', 'is_dng', 'is_bif', 'is_geotiff'])
# Inspect selected content of the metadata
print(meta['Info'])
NRRD0004
# Complete NRRD file format specification at:
# http://teem.sourceforge.net/nrrd/format.html
type: float
dimension: 3
space: left-posterior-superior
sizes: 320 320 240
space directions: (1,0,0) (0,1,0) (0,0,1)
kinds: domain domain domain
endian: little
encoding: raw
space origin: (0,0,0)

Interactive vizualisation of the n-dimensional TIF image.

Note: This view will not render on the course website. You need to run the code locally or on the Jupyter-Hub of your institute

stackview.slice(image, continuous_update=True)

Load and process a DICOM series#

datasets = []

# Load the DICOM files with pydicom into pydicom DataSets
with os.scandir("data_dicom/series1") as entries:
    for entry in entries:
        if entry.is_file() and entry.name.endswith(".dcm"):
            datasets.append(pydicom.dcmread(entry.path))

print(f"Found {len(datasets)} DICOM files in the series")
Found 99 DICOM files in the series

You can examine the available DICOM metadata elements for such a file in datasets by simply printing it:

print(datasets[0])

Let’s examine some the metadata for a couple of the DICOM files

for file in datasets[:3]:
    print("\nPatient:", file.PatientName)
    print("Modality:", file.Modality)
    print("Study Date:", file.StudyDate)
    print("InstanceNumber:", file.InstanceNumber)
Patient: C3N-00247
Modality: CT
Study Date: 20100227
InstanceNumber: 95

Patient: C3N-00247
Modality: CT
Study Date: 20100227
InstanceNumber: 96

Patient: C3N-00247
Modality: CT
Study Date: 20100227
InstanceNumber: 25

As we can see by the InstanceNumber, the DICOM files are unsorted. What we want to do now is:

  • Sort the list datasets of pydicoms DICOM files, sort them based on their InstanceNumber

  • Stack the pixel array of the DICOM files into a 3D NumPy array

  • Visualize the stacked array with stackview

And since we already know how to make use of our AI assistant, let’s aks bob to provide us with according code!

# Import bia-bob as bob
from bia_bob import bob
# Initialize bia-bob with Blablador and API key from environment variable
bob.initialize(endpoint='blablador', model='alias-code')
This notebook may contain text, code and images generated by artificial intelligence. Used model: alias-code, vision model: None, endpoint: https://helmholtz-blablador.fz-juelich.de:8000/v1, bia-bob version: 0.34.3.. Do not enter sensitive or private information and verify generated contents according to good scientific practice. Read more: https://github.com/haesleinhuepf/bia-bob#disclaimer
%%bob
TODO