0

What is the correct way to convert CT scan nifti files to Hounsfield units? My code is as follows:

path = 'input/volume/volume-0.nii'
img_obj = nib.load(path)
img_data = img_obj.get_fdata()

slope = img_obj.dataobj.slope
intercept = img_obj.dataobj.inter

img_data[img_data >= 1200] = 0 #trying to remove bone area
images = slope * img_data.astype(np.float32)
hu_images = images + intercept

But when I try to normalize this hu converted image to [0,1], it yields a black image.

Dushi Fdz
  • 161
  • 3
  • 22

1 Answers1

0

nib.get_fdata() should already scale your data if the corresponding header fields are set correctly. For an explanation, see this nipy article.

Note that if you're trying to set high HU densities to 0 (with img_data[img_data >= 1200] = 0), you're giving it the same density as water. What you probably want is to set it to air density (HU -1,000).

In order to scale the image to [0, 1] you could use:

from skimage.exposure import rescale_intensity

rescale_intensity(img_data, in_range='image', out_range=(0., 1.))
asdf
  • 1,006
  • 1
  • 9
  • Thank You. Isn't the HU intensity corresponding to the bone area in CT scan is HU 1200+? – Dushi Fdz Jun 09 '22 at 21:06
  • Cortical bone (the outmost part of the bone) can reach densities >1,000 HU, but there may be physical conditions with reduced bone density. Trabecular bone on the other hand can be closer to the 300 – 800 HU range. – asdf Jun 09 '22 at 21:55