Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to convert DICOM to JPG to this repository #1013

Open
alistairewj opened this issue Jun 2, 2020 · 0 comments
Open

Add code to convert DICOM to JPG to this repository #1013

alistairewj opened this issue Jun 2, 2020 · 0 comments

Comments

@alistairewj
Copy link
Member

At the moment the code used to convert the x-rays from DICOMs to JPGs is not publicly available as it's tied up in another repo. It would be good to have a publicly executable set of code that converts DICOMs to JPGs in the same way as we did to create MIMIC-CXR-JPG from MIMIC-CXR. The core function used was:

import numpy as np
import pydicom
import cv2

def dcm2img(input_file_path, output_file_path):
    """Extract the image from a DICOM and write it to an image file."""

    # Read the DICOM and extract the image.
    dcm_file = pydicom.dcmread(input_file_path)
    raw_image = dcm_file.pixel_array

    assert len(raw_image.shape) == 2,\
        "Expecting single channel (grayscale) image."

    # Normalize pixels to be in [0, 255].
    raw_image = raw_image - raw_image.min()
    normalized_image = raw_image / raw_image.max()
    rescaled_image = (normalized_image * 255).astype(np.uint8)

    # Correct image inversion.
    if dcm_file.PhotometricInterpretation == "MONOCHROME1":
        rescaled_image = cv2.bitwise_not(rescaled_image)

    # Perform histogram equalization.
    final_image = cv2.equalizeHist(rescaled_image)

    # Write the image to file.
    cv2.imwrite(output_file_path, final_image)
@briangow briangow transferred this issue from MIT-LCP/mimic-cxr May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants