I am working with nifti images (CT scans) and when I normalize the Hounsfield Unit converted images, the output is just a full black image. My code to convert to HU scale is as follows:
def transform_to_hu(img_data, img_obj):
slope = img_obj.dataobj.slope
intercept = img_obj.dataobj.inter
img_data[img_data >= 1200] = 0
images = slope * img_data.astype(np.float64)
images += np.float64(intercept)
return np.array(images, dtype=np.float64)
hu_scans = transform_to_hu(img_data, img_obj)
And then I normalize the HU converted images using following function:
def normalize(volume):
level = 70
window = 200
max = level + window/2
min = level - window/2
volume = volume.clip(min,max)
"""Normalize the volume"""
volume[volume < min] = min
volume[volume > max] = max
volume = (volume - min) / (max - min)
volume = volume.astype("float32")
return volume
normalized_image = normalized(hu_scans)
Why is output image black after normalization?