Exemplo n.º 1
0
def _get_window(dicom_file: pydicom.FileDataset, window='default', unit=None):
    if isinstance(window, dict):
        wc = window.get('window_center')
        ww = window.get('window_width')
    elif window == 'default' or unit is None:
        try:
            wc = dicom_file.WindowCenter
            ww = dicom_file.WindowWidth
            if isinstance(wc, pydicom.multival.MultiValue):
                wc = wc[0]
            if isinstance(ww, pydicom.multival.MultiValue):
                ww = ww[0]
        except:
            BitsStored = dicom_file.get('BitsStored')
            wc = 2**(BitsStored - 1)
            ww = 2**BitsStored
    elif isinstance(window, str) and unit in ['HU', 'Hounsfield Unit']:
        wc, ww = get_ct_window(window)
    else:
        try:
            wc = dicom_file.WindowCenter
            ww = dicom_file.WindowWidth
            if isinstance(wc, pydicom.multival.MultiValue):
                wc = wc[0]
            if isinstance(ww, pydicom.multival.MultiValue):
                ww = ww[0]
        except:
            wc = None
            ww = None

    if wc is None or ww is None:
        raise ValueError('Either window center or window width not found')
    return wc, ww
Exemplo n.º 2
0
def voi_lut_trans(dicom_file: pydicom.FileDataset,
                  image_data: numpy.ndarray,
                  depth,
                  window='default',
                  unit: str = None):
    VOILUTSequence = getattr(dicom_file, 'VOILUTSequence', [{}])[0]
    lut_descriptor = VOILUTSequence.get('LUTDescriptor')
    if lut_descriptor is None:
        lut_descriptor = VOILUTSequence.get('LUTDescriptor')

    if isinstance(lut_descriptor, bytes):
        PixelRepresentation = int(dicom_file.get('PixelRepresentation'))
        if modality_classifier(
                dicom_file) is None and PixelRepresentation == 0:
            dtype = numpy.ushort
        else:
            dtype = numpy.short
        lut_descriptor = numpy.frombuffer(lut_descriptor, dtype)

    lut_data = VOILUTSequence.get('LUTData')
    if isinstance(lut_data, bytes):
        if lut_descriptor[2] == 8:
            dtype = numpy.uint8
        elif lut_descriptor[2] == 16:
            dtype = numpy.uint16
        else:
            raise ValueError(
                f'LUTDescriptor[2] should be 8 or 16. Got {lut_descriptor[2]}')
        lut_data = numpy.frombuffer(lut_data, dtype)
    scale_factor = depth / int(2**getattr(dicom_file, 'BitsStored', 12))
    return lut_trans(image_data, lut_descriptor, lut_data, scale_factor)
Exemplo n.º 3
0
def voi_classifier(dicom_file: pydicom.FileDataset):
    """
    Get value of interest (voi) transform method name from dicom object

    :param dicom_file: object read from pydicom
    :return: voi transform method name. Includes
            'LINEAR', 'LINEAR_EXACT', 'SIGMOID', 'TABLE', or None
    """
    VOILUTFunction = dicom_file.get('VOILUTFunction')
    VOILUTSequence = dicom_file.get('VOILUTSequence')
    WindowCenter = dicom_file.get('WindowCenter')

    if (VOILUTSequence is not None) and (WindowCenter is None):
        return 'TABLE'
    elif WindowCenter is not None:
        if VOILUTFunction is None:
            return 'LINEAR'
        else:
            return VOILUTFunction
    else:
        return 'LINEAR'
Exemplo n.º 4
0
def modality_lut_trans(dicom_file: pydicom.FileDataset, image_data: numpy.ndarray):
    ModalityLUTSequence = dicom_file.get('ModalityLUTSequence')[0]
    lut_descriptor = ModalityLUTSequence.get('LUTDescriptor')
    if lut_descriptor is None: # debug
        lut_descriptor = ModalityLUTSequence.get('LUTDescriptor') # if don't do this line, line 40 will be None

    if isinstance(lut_descriptor, bytes):
        PixelRepresentation = int(dicom_file.get('PixelRepresentation'))
        dtype = numpy.ushort if PixelRepresentation == 0 else numpy.short
        lut_descriptor = numpy.frombuffer(lut_descriptor, dtype)

    lut_data = ModalityLUTSequence.get('LUTData')
    if isinstance(lut_data, bytes):
        if lut_descriptor[2] == 8:
            dtype = numpy.uint8
        elif lut_descriptor[2] == 16:
            dtype = numpy.uint16
        else:
            raise ValueError(f'LUTDescriptor[2] should be 8 or 16. Got {lut_descriptor[2]}')
        lut_data = numpy.frombuffer(lut_data, dtype)
    unit = ModalityLUTSequence.get('ModalityLUTType')
    return lut_trans(image_data, lut_descriptor, lut_data), unit
Exemplo n.º 5
0
 def get_dcm_filed(__dcm: pydicom.FileDataset, __field_name: str):
     __field = __dcm.get(__field_name)
     if type(__field) == pydicom.multival.MultiValue:
         return int(__field[0])
     else:
         return int(__field)