The Digital Communications in Medicine (DICOM) format is used to store CT scans, X-rays, and MRIs, as well as other medical images.
Table of Contents
The Digital Communications in Medicine (DICOM) format is used to store CT scans, X-rays, and MRIs, as well as other medical images.
DICOM files may also contain patient identifying information, allowing the image to be connected to a specific individual. It is the most widely used medical imaging format in hospitals. DICOM maintains the high quality of the medical images. The patient information such as patient_id, abnormality, age, name, etc. can be recorded alongside the image in DICOM format.
Pydicom is a python library for working with dicom images. In this blog, we will install and utilize the common functions of pydicom library. You can also check the official documents of pydicom from this link.
There are multiple ways to install pydicom library in your environment. We preferred the following two ways; by using PIP or Conda.
How to install Pydicom using PIP or Conda
Install pydicom using PIP
pip install pydicom
Install pydicom using Conda
conda install -c conda-forge pydicom
After successful installation, you have to import the required libraries.
import pydicom
import matplotlib.pyplot as plt
The Pydicom library will be used to extract data from the Dicom file, while Matplotlib will be used to plot or display the Dicom file in pixels.
How to read DICOM image using Pydicom
dicom_path = "mammogram_1.dcm"
dicom_img = pydicom.dcmread(dicom_path)
The ‘pydicom.dcmread‘ read the Dicom file. In the above code, we read a Dicom file of a mammogram and store all the data into a ‘dicom_img’ variable.
How to get information from DICOM image
patient_id = dicom_img.PatientID
image_label = dicom_img.SeriesDescription
If you want to check out all the information stored inside the Dicom image. Simply print(‘dicom_img’), it will show the information of Dicom which you can use further for your tasks. For example; I just extract the patient ID and Series Description inside the Dicom file.
How to show DICOM image using Matplotlib
dicom_pixels = dicom_img.pixel_array
plt.imshow(dicom_pixels, cmap="gray")
plt.show()
Finally, if you want to show the Dicom image. You have to convert the Dicom into a pixels array. The ‘dicom_img.pixel_array‘ will perform this task and you can show it using matplotlib or any other library.
Complete Code
# Libraries
import pydicom
import matplotlib.pyplot as plt
# Read Dicom
dicom_path = "mammogram_1.dcm"
dicom_img = pydicom.dcmread(dicom_img)
# Get Dicom Information
patient_id = dicom_img.PatientID
image_label = dicom_img.SeriesDescription
# Convert Dicom into Image
dicom_pixels = dicom_img.pixel_array
plt.imshow(dicom_pixels, cmap="gray")
plt.show()