There are several libraries that read DICOM, but this article will explain how to use SimpleITK to read medical images such as MRI and CT.
SimpleITK is a library that makes it easy to use ITK, a format for medical image analysis, in Python and other languages. (Https://en.wikipedia.org/wiki/SimpleITK) It is also used in software such as 3D Slicer, ImageJ, and ITK-Snap.
For example, MRI images are often used as DICOM data, and Simple ITK can read DICOM directly. Also, DICOM images are often saved as NIFTI (.nii, .nii.gz) or NRRD format (.nrrd), but can be read as well. (Https://simpleitk.readthedocs.io/en/master/IO.html) However, regarding the output, it seems that it cannot be output in DICOM format.
Images read in SimpleITK format can be displayed and converted to ndarray format for processing and analysis. The purpose of using this library is that it was necessary to load images in SimpleITK format on another library for image analysis called PyRadiomics. I have summarized it as my own learning record.
You can do the same with Google Collaboratory, but uploading local DICOM data etc. is troublesome, so we built the environment.
conda install -c simpleitk simpleitk
You can install it normally with pip.
It is assumed that multiple DICOM images are saved in the same folder.
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt
#Load Dicom
imgdir = "Directory name where DICOM exists"
sys.argv[1] = imgdir
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(sys.argv[1])
reader.SetFileNames(dicom_names)
image = reader.Execute() #Image read in ITK format
#When reading NIFTI or NRRD
image = sitk.ReadImage("Image directory")
#Get image size
size = pre.GetSize()
print("Image size:", size[0], size[1], size[2])
#Convert ITK format to ndarray format
ndImage = sitk.GetArrayFromImage(image)
#Display image
plt.imshow(ndImage[n], cmap='gray') #n shows for any slice
In the above code, we checked the size and displayed it briefly. There are various uses after loading an image. For example, since it is in 3D ndarray format, you can see the signal value of any voxel with print. I would like to summarize in another article what kind of analysis can be done and output of images.
Recommended Posts