Loading and Visualization

Learn to display a medical image stored in a file.

We'll cover the following

Data and format

In this course, we’ll use open-source data. We’ll use the X-ray dataset for pneumothorax segmentation in the DICOM format. For the NIfTI-1 Data Format, we’ll use the CT dataset for liver cancer segmentation. This course does not require you to download the dataset to your computer. However, if you want to save it to your computer, see the course’s appendix.

DICOM

To work with DICOM files, we’ll use the pydicom library. We import all the libraries necessary for the work. After importing the required libraries, we read the file using the dcmread(). Let’s take the file as an example to display the given X-ray image.

As a result, the image data variable is of type pydicom.dataset.FileDataset since it contains not only the snapshot but also the metadata. To access the image, we need to use the pixel_array attribute of the DICOM image. Render the image using Matplotlib and use plt.cm.bone as a color scheme to make the image look like an X-ray.

The result will look like this:

Press + to interact
import pydicom # library for working with DICOM files
from matplotlib import cm # color schemes for visualization
from matplotlib import pyplot as plt # library for visualization
example = 'stage_2_images/ID_01fe90211.dcm'
imagedata= pydicom.dcmread(example)
plt.figure(figsize=(12, 12))
plt.imshow(imagedata.pixel_array, cmap=plt.cm.bone)
plt.show()

Self-study task: Remove the plt.cm.bone color scheme, or try other color scheme.

Output of plt.cm.rainbow

NIfTI-1

The NIfTI DFWG (Data Format Working Group) suggested NIfTI-1 as a short-term approach to enhance the interoperability of functional MRI data analysis software packages.

Let’s look at an example to understand this approach.

  • First, we import all the libraries necessary for this work.
  • Then, we read the file using load().
  • We use get_fdata()to get an image.
  • We work with the file volume_pt5/volume-44.nii. Turn the image 9090 degrees, and look at the shape of the file contents.
  • The “depth” here means CT slices. We use imshow() to visualize 5050 and 118118 CT slices.

Note: We can see the output of the array after turning the image by clicking on the > symbol.

Let’s visualize a couple of CT slices (5050 and 118118 in this case):

Press + to interact
import nibabel # library for working with NIfTI-1 Data Format
import numpy as np # numpy for image manipulation
from matplotlib import cm # color schemes for visualization
from matplotlib import pyplot as plt # library for visualization
filepath = 'volume_pt5/volume-44.nii'
imagedata=nibabel.load(filepath)
array = imagedata.get_fdata()
array = np.rot90(np.array(array))
print(array.shape)
f = plt.figure(figsize=(12,12))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
ax.imshow(array[...,50].astype(np.float32), cmap=plt.cm.bone)
ax2.imshow(array[...,118].astype(np.float32), cmap=plt.cm.bone)

Self-study task: Visualize various values of CT slices.

Ocean-colored image of volume-44.nii file