Convert DICOM
Working with DICOM converted from JPEG/PNG
When jpeg or png images are uploaded to an MD.ai DICOM dataset, it gets wrapped as a DICOM file with the appropriate DICOM tags. Optionally, these images can be specified using a filename schema that defines hierarchical relationships (exam/series/image). When this dataset is exported, the files are DICOM, not the original jpeg/png images. The original jpeg/png image filenames can be found in the ImageComments
tag.
If you would like to use the converted DICOM directly, care must be taken to use the correct color space, since JPEG uses YCbCr, which is specified by DICOM in the PhotometricInterpretation
tag with a value of YBR_FULL_422
(or variants). Usually, we want to normalize to the RGB color space when working with machine learning code. The following example code ensures that the resulting pixel array from color DICOM (such as those converted from JPEG/PNG) is converted to RGB, by reading the SamplesPerPixel
and PhotometricInterpretation
tags and calling the pydicom
utility function convert_color_space
if necessary:
Convert DICOM to JPEG/PNG
Converts a single dicom file to an 8 bit format
Save as jpg or png
Supporting functions
Common window width and window level for CT exams
Target | WW | WL |
---|---|---|
brain | 80 | 40 |
subdural | 215 | 75 |
stroke_1 | 8 | 32 |
stroke_2 | 40 | 40 |
temporal_bone | 2800 | 600 |
neck_soft_tissue | 375 | 40 |
lung | 1500 | -500 |
emphysema | 800 | -800 |
mediastinum | 400 | 40 |
pulmonary_embolism | 700 | 100 |
abdomen | 350 | 40 |
liver | 120 | 70 |
kidney | 700 | 50 |
bone | 2500 | 480 |
Convert JPG, PNG to DICOM
Converts a single jpg or png file to dicom format
Convert CT nifti to DICOM
Use the convert_ct
function in common_utils
to convert nifti files to DICOM for uploading to MD.ai. You'll need to choose your input and output directories. Optionally, you can change the plane or default window/level settings. You'll also need a sample dicom which you can download from here
This function will write the converted DICOM files to the output directory and will give each image from a nifti file the same Study and Series UIDs. Use the CLI tool to upload the newly created DICOM images into your project
Convert DICOM to nifti
Use the dicom2nifiti library from icometrix
This will convert the dicom files from the dicom_directory to the output_folder.
Convert MR .mha files to DICOM
Use the function provided below to convert MR modality .mha
files to DICOM for uploading to MD.ai. You'll need to provide the filepath to the MHA file. Optionally, you can change the plane or default window/level settings. You'll also need a sample dicom file which you can download from here. This method was provided by Ramon Correa, one of our summer 2022 interns.
Convert Annotations to DICOM SR
With the util class dicom_utils.SrExport()
, all annotations will be converted, capturing the label's and their parent's name in SR format (The specific Code Value of each annotation will be arbitrary). An SR file will be created for each annotator in each annotated study. The SR's DICOM data will be consistent with the study it references. The study and annotation information comes from the inputted "Annotation Json" and "Metadata Json" (These jsons have to be referencing the same dataset(s) for it to work).
The labels will be ordered from exam level to series to image. Each image level label will have a "Referenced Image" section preceding the label to indicate it's source. Series labels will have "Series UID: xxx" under them for better referencing as well.
Convert Annotations to DICOM SEG (Recommended)
With the util class dicom_utils.SegExport()
, only local annotations will be exported. This export process converts the annotation data into a binary mask, and creates the relevant segmentation DICOM data to export a DICOM Segmentation file. A Segmentation file will be created for each annotator in each annotated series. Additionally, if combine_label_groups
is False, a different file will be created for each label group. The file's DICOM headers will be consistent with the original DICOM's. The study and annotation information comes from the inputted "Annotation Json" and "Metadata Json" (These jsons have to be referencing the same dataset(s) for it to work).
The segmentation frames are grouped together by their labels and within those groups, they are ordered by their source's frame number.
Convert Freeform Annotations to DICOM SEG (Legacy)
Instructions can be found here.
Support functions
Read DICOM UIDs and tags from your original files
Use this code on your original data to create a dataframe of DICOM tags. These are sample tags, you can add or remove values, as needed.