From edf2ba7fb5fce92c0fb7d13a5c3484f5e0e723d0 Mon Sep 17 00:00:00 2001 From: diegokauer Date: Mon, 25 Nov 2024 11:58:15 -0500 Subject: [PATCH 1/2] modify get_dicom_image to get view position from alternative tag for FUJIFILM images --- onconet/utils/dicom.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/onconet/utils/dicom.py b/onconet/utils/dicom.py index 0923483..fb225a7 100644 --- a/onconet/utils/dicom.py +++ b/onconet/utils/dicom.py @@ -201,10 +201,19 @@ def get_dicom_info(dicom: pydicom.Dataset): int: binary integer 0 or 1 corresponding to the type of View Position int: binary integer 0 or 1 corresponding to the type of Image Laterality """ - if not hasattr(dicom, 'ViewPosition'): - raise AttributeError('ViewPosition does not exist in DICOM metadata') - view_str = dicom.ViewPosition + # Some cases (FUJIFILM) have cases where view position is in + # Acquisition Device Processing Description (0018, 1400) + # rather than standard DICOM tag + if not hasattr(dicom, 'ViewPosition'): + if 'CC' in dicom[0x0018, 0x1400].value: # Acquisition Device Processing Description + view_str = 'CC' + elif 'MLO' in dicom[0x0018, 0x1400].value: + view_str = 'MLO' + else: + raise AttributeError('ViewPosition does not exist in DICOM metadata') + else: + view_str = dicom.ViewPosition # Have seen cases where ImageLaterality is not present in DICOM metadata, # and the relevant information is in the ViewPosition tag. Check for this. From 922121e2485da736a88225ad5b818b474cc64759 Mon Sep 17 00:00:00 2001 From: diegokauer Date: Mon, 25 Nov 2024 12:15:34 -0500 Subject: [PATCH 2/2] defined alternative preprocessing function from dicom_to_arr_pydicom using pydicom builtin function, including support for manufacturers that have a reverse view (FUJIFILM and Planmed) --- onconet/utils/dicom.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/onconet/utils/dicom.py b/onconet/utils/dicom.py index fb225a7..de47e30 100644 --- a/onconet/utils/dicom.py +++ b/onconet/utils/dicom.py @@ -191,6 +191,29 @@ def dicom_to_arr(dicom, window_method='minmax', index=0, pillow=False, overlay=F return image +def dicom_to_arr_pydicom(dicom, index=0, pillow=False): + logger = get_logger() + + presentation = 'IDENTITY' + if (0x2050, 0x0020) in dicom: + presentation = dicom[0x2050, 0x0020].value + + pixels = dicom.pixel_array + + if presentation == 'INVERSE': + logger.debug("Using Inverse Presentation") + pixels = (dicom.WindowCenter + dicom.WindowWidth) - pixels + image = apply_voi_lut(pixels, dicom, index) + + if pillow: + image = image.astype(np.int32) + if image.shape[-1] in {3, 4}: + image = image.mean(axis=-1, dtype=np.int32) + return Image.fromarray(image, mode='I') + else: + return image + + def get_dicom_info(dicom: pydicom.Dataset): """Return tags for View Position and Image Laterality.