Inhomogeneity correction using N4 and Python

Hey dear supporters,

I would like to perform inhomogeneity correction of head and pelvis NIfTI MRI images using Python.

My code looks like this:

Image = sitk.ReadImage(path)
image = sitk.Cast(Image, sitk.sitkFloat32)

Mask = sitk.ReadImage(mask_path)
mask = sitk.Cast(Mask, sitk.sitkFloat32)

n4 = sitk.N4BiasFieldCorrectionImageFilter()
n4.SetMaximumNumberOfIterations([50, 50, 30, 20])

corrected_image = n4.Execute(image)

sitk.WriteImage(corrected_image, path[:-7]+'_b.nii.gz')

I have now noticed that here (unlike here) there is no way listed how to shrink the image. However, it seems to be common to perform bias field correction at four resolution levels. Can I insert sitk.Shrink() in there somehow? Or what do you guys advise?

Thanks in advance for your support

Hello @Cambridgeshire,

Please see the example on read-the-docs it includes shrinking.

1 Like