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

Alternative manufacturers preprocessing #12

Open
wants to merge 2 commits into
base: update_torch2
Choose a base branch
from
Open
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
38 changes: 35 additions & 3 deletions onconet/utils/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -201,10 +224,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.
Expand Down