Python - Convert pixel value to Hounsfield Units

1k Views Asked by At

I'm working on image chest scans, I am trying to convert pixel values on Hounsfield Units.

I'm using python for that purpose, my scans have different ranges and the problem is, window of (width of 3200, and center of 450)?

How can I make those scans have normal ranges (shift the hounsfield units) for me to work normally on with a window of (width of 800, and center of 150)?

2

There are 2 best solutions below

0
On BEST ANSWER

I am not very familiar with Hounsfield units and I am not sure about the "windows" you mention. Normally, I would associate windows with an "area process" where any given pixel's new value is partially determined by its neighbours within a given area.

If, however, you mean a pure "point process" where any given pixel's new value is solely determined by a function of its old value, you can proceed as follows:

  1. Subtract 450 so the value is centred on zero with a range of 3200
  2. Divide by 4 so the range is 800
  3. Add 150 to re-centre

So, in concrete terms:

new = (old - 450)/4 + 150
0
On

To have normal ranges, you have to do the bellow. It returns image normalized.

def transform_to_hu(medical_image, image):
    # medical_image = pydicom.read_file
    # image = medical_image.pixel_array

    intercept = medical_image.RescaleIntercept
    slope = medical_image.RescaleSlope
    hu_image = image * slope + intercept

    return hu_image


def normalize_image(image, center, width):
    # image = transform_to_hu result

    img_min = center - width // 2
    img_max = center + width // 2
    window_image = image.copy()
    window_image[window_image < img_min] = img_min
    window_image[window_image > img_max] = img_max
    
    return window_image