profile

Machine Learning for Medical Imaging

DICOM Series in Python Explained

Published 10 months agoΒ β€’Β 2 min read

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

by Nour Islam Mokhtari from pycad.co

πŸ‘‰ 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,, Welcome to the PYCAD newsletter, where every week you receive doses of machine learning and computer vision techniques and tools to help you learn how to build AI solutions to empower the most vulnerable members of our society, patients. TotalSegmentator : Whole Body Segmentation at your Fingertips This free tool available online can do full body segmentation, it's called TotalSegmentator. I have already mentioned this tool in a previous edition of the newsletter, but in this...

9 days agoΒ β€’Β 3 min read

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 ↓ A Medical Imaging Expert Told Me This Recently I saw a post on LinkedIn where a medical imaging expert showcased his work of segmenting the lungs and its bronchial trees. You can...

16 days agoΒ β€’Β 2 min read

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 ↓ How we helped accelerate inference time for a client's AI product Below is a screenshot of a benchmark we did for a client of ours. The goal was to accelerate inference time. This...

22 days agoΒ β€’Β 3 min read
Share this post