text_plain_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pydicom

def make_lut(pixels, width, center, p_i):
    slope = 1.0
    intercept = 0.0
    min_pixel = int(np.amin(pixels))
    max_pixel = int(np.amax(pixels))
    lut = [0] * (max_pixel + 1)
    invert = False
    if p_i == 'MONOCHROME1':
        invert = True
    else:
        center = max_pixel - min_pixel - center
    for pix_value in range(min_pixel, max_pixel):
        lut_value = pix_value * slope + intercept
        voi_value = ((lut_value - center) / width + 0.5) * 255.0
        clamped_value = min(max(voi_value, 0), 255)
        if invert:
            lut[pix_value] = round(255 - clamped_value)
        else:
            lut[pix_value] = round(clamped_value)
    return lut

from pydicom.pixel_data_handlers.util import apply_voi_lut
image = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00012/T1w/Image-17.dcm')
pixels = image.pixel_array
print('Min pixel value: ' + str(np.min(pixels)))
print('Max pixel value: ' + str(np.max(pixels)))
plt.figure(figsize=(6, 6))
plt.imshow(pixels, cmap='gray')