DICOM Series in Python Explained


Hello Reader,

Welcome to another edition of PYCAD newsletter where we cover interesting topics in Machine Learning and Computer Vision applied to Medical Imaging. The goal of this newsletter is to help you stay up-to-date and learn important concepts in this amazing field! I've got some cool insights for you below ↓

​

DICOM Series in Python

Lately I was working on a task for 3D medical imaging where I needed to load specific slices from a series of DICOM files.

So if a patient case has 300 slices, how would you load slices 50 to 100 only?
In this quick tutorial I will show you 2 code examples on how to do this in Python.

Approach 1: Using PyDicom

Approach 1: Using SimpleITK

​

How to handle dark DICOM images

When working with DICOMs for medical imaging, from time to time you’ll find cases where the image is completely dark. Why do we have such cases? And how to tackle them using Python?

The darkness or brightness of DICOM (Digital Imaging and Communications in Medicine) images, such as MRIs and CT scans, can depend on several factors:

  1. Imaging parameters:
    ​
    During the acquisition of medical images, various scanner settings and parameters are chosen, such as the duration and intensity of the imaging signals (e.g., magnetic fields in the case of MRI, or X-ray exposure in the case of CT). These parameters can significantly influence the contrast and brightness of the resulting images.
    ​
  2. Patient characteristics:
    ​
    Factors such as the patient’s size, the specific body part being imaged, and the presence of any diseases or conditions can affect how the imaging signals are absorbed and reflected, which in turn affects the image brightness.
    ​
  3. Window Level and Window Width:
    ​
    The term β€œwindowing” in the context of medical imaging refers to the process of adjusting the range of pixel intensities that are displayed in the image. The window level is the center of this range, and the window width is the size of the range. By adjusting these parameters, you can highlight different tissues and structures in the image. For example, you can adjust the window level and width to highlight the bones, soft tissues, or lungs in a CT scan.
    ​
  4. Image compression:
    ​
    DICOM images can be compressed to save storage space, and this can sometimes lead to a loss of quality, including changes in brightness and contrast.
    ​
  5. Inherent imaging modalities:
    ​
    Each imaging modality(MRI, CT, PET, etc.) has a different way of acquiring images. This fundamental difference in image acquisition results in different appearances of the output images.
    ​

If you’re building a deep learning model for a medical imaging application, you might want to adjust the brightness for dark patient cases.

To do this, you have several options such as: windowing, histogram equalization and adaptive histogram equalization.

Here’s how to apply them in Python:

  1. Windowing: PyDicom is a Python package specifically for parsing DICOM files, which are the standard for distributing and sharing medical images. Once you’ve read the DICOM file with PyDicom, you can manipulate the pixel array for windowing. SimpleITK also provides functions to do windowing.
    ​
  2. Histogram Equalization: OpenCV and scikit-image provide functions for this. In OpenCV, you can use the cv2.equalizeHist() function, and in scikit-image, you can use the exposure.equalize_hist() function.
    ​
  3. Adaptive Histogram Equalization (AHE): OpenCV provides the cv2.createCLAHE() function for Contrast Limited Adaptive Histogram Equalization (CLAHE), which is a variant of AHE.

​

Here's a sample code on how to do this:


​

​

What'd you think of today's edition?

​

​

That's it for this week's edition, I hope you enjoyed it!

​

Machine Learning for Medical Imaging

πŸ‘‰ Learn how to build AI systems for medical imaging domain by leveraging tools and techniques that I share with you! | πŸ’‘ The newsletter is read by people from: Nvidia, Baker Hughes, Harvard, NYU, Columbia University, University of Toronto and more!

Read more from Machine Learning for Medical Imaging

Hi Reader! I hope you're doing well in this fine weekend! In the past weeks I've been working on implementing basic image segmentation models for 2D and 3D from scratch. While doing so, I found a few things that were delightfully surprising while other things were painfully irritating. I tell you all about it in this edition of the newsletter! What Building AI Models from Scratch has Thought me One of the reasons why I did these experimentations was to understand some of the nitty gritty...

Hi Reader, I haven't sent you a newsletter email for some time now. This is because there are major events happening in my personal life. We just had our first kid, so I'm still trying to adapt to the new routine set by this cute little creature! I also changed my office! I used to work from home, but now I am working in a coworking space. I'm hoping that this will help me deliver more value to the newsletter subscribers as well as our clients at PYCAD. Now, back to the newsletter! I've got...

Dental implant - Wikipedia

Hello Reader, Welcome to another edition of PYCAD newsletter where we cover interesting topics in Machine Learning and Computer Vision applied to Medical Imaging. The goal of this newsletter is to help you stay up-to-date and learn important concepts in this amazing field! I've got some cool insights for you below ↓ Applications of Machine Learning for Dentistry At PYCAD, we have worked a lot on the applications of AI to the dentistry domain. Here are 3 incredible ones. 1 - Diagnosis and...