Diff of /src/dicom_utils.py [000000] .. [3475df]

Switch to unified view

a b/src/dicom_utils.py
1
import numpy as np
2
def transform_to_hu(medical_image):
3
    intercept = medical_image.RescaleIntercept
4
    slope = medical_image.RescaleSlope
5
    image = medical_image.pixel_array
6
    hu_image = image * slope + intercept
7
8
    return hu_image
9
10
def apply_windowing(ds, window_center, window_width, normalized=False, zero_centered=True):
11
    
12
    # Get the pixel data and rescale to Hounsfield units (HU)
13
    pixel_array = ds.pixel_array.astype(np.float32)
14
    intercept = ds.RescaleIntercept
15
    slope = ds.RescaleSlope
16
    hu_array = pixel_array * slope + intercept
17
    
18
    # Apply windowing
19
    window_min = window_center - (window_width / 2)
20
    window_max = window_center + (window_width / 2)
21
    windowed_array = np.clip(hu_array, window_min, window_max)
22
    if(zero_centered):
23
        windowed_array = zero_center(windowed_array)
24
    if(normalized):
25
    # Normalize the windowed array to [0, 1]
26
        normalized_array = (windowed_array - window_min) / (window_max - window_min)
27
        return normalized_array
28
    else: 
29
        return windowed_array
30
31
def set_window(img, hu=[-800.,1200.]):
32
    window = np.array(hu)
33
    newimg = (img-window[0]) / (window[1]-window[0])
34
    newimg[newimg < 0] = 0
35
    newimg[newimg > 1] = 1
36
    newimg = (newimg * 255).astype('uint8')
37
    return newimg
38
39
40
def zero_center(hu_image):
41
    hu_image = hu_image - np.mean(hu_image)
42
    return hu_image