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

DICOM redactor improvement: Enabling more photometric interpretations #1103

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import shutil
from copy import deepcopy
import tempfile
Expand Down Expand Up @@ -58,9 +59,10 @@ def redact(
# Convert DICOM to PNG and add padding for OCR (during analysis)
is_greyscale = self._check_if_greyscale(instance)
image = self._rescale_dcm_pixel_array(instance, is_greyscale)
self._save_pixel_array_as_png(image, is_greyscale, "tmp_dcm", tmpdirname)
image_name = str(uuid.uuid4())
self._save_pixel_array_as_png(image, is_greyscale, image_name, tmpdirname)

png_filepath = f"{tmpdirname}/tmp_dcm.png"
png_filepath = f"{tmpdirname}/{image_name}.png"
loaded_image = Image.open(png_filepath)
image = self._add_padding(loaded_image, is_greyscale, padding_width)

Expand Down Expand Up @@ -225,8 +227,11 @@ def _check_if_greyscale(instance: pydicom.dataset.FileDataset) -> bool:
:return: FALSE if the Photometric Interpretation is RGB.
"""
# Check if image is grayscale using the Photometric Interpretation element
color_scale = instance[0x0028, 0x0004].value
is_greyscale = color_scale != "RGB"
try:
color_scale = instance.PhotometricInterpretation
except AttributeError:
color_scale = None
is_greyscale = (color_scale in ["MONOCHROME1", "MONOCHROME2"])

return is_greyscale

Expand All @@ -243,7 +248,10 @@ def _rescale_dcm_pixel_array(
"""
# Normalize contrast
if "WindowWidth" in instance:
image_2d = apply_voi_lut(instance.pixel_array, instance)
if is_greyscale:
image_2d = apply_voi_lut(instance.pixel_array, instance)
else:
image_2d = instance.pixel_array
else:
image_2d = instance.pixel_array

Expand Down