# Inhomogeneity correction using N4 and Python

**URL:** https://discourse.itk.org/t/inhomogeneity-correction-using-n4-and-python/6123
**Category:** Algorithms
**Tags:** python, n4biasfield, simpleitk
**Created:** [August 1, 2023, 10:24am UTC](https://discourse.itk.org/t/inhomogeneity-correction-using-n4-and-python/6123 "2023-08-01T10:24:19Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Cambridgeshire](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cambridgeshire](https://discourse.itk.org/u/Cambridgeshire)
#### Post date: [August 1, 2023, 10:24am UTC](https://discourse.itk.org/t/inhomogeneity-correction-using-n4-and-python/6123/1 "2023-08-01T10:24:19Z")

</div>

Hey dear supporters,

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

My code looks like this:

```auto
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](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1N4BiasFieldCorrectionImageFilter.html) (unlike [here](https://slicer.readthedocs.io/en/latest/user_guide/modules/n4itkbiasfieldcorrection.html)) 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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 1, 2023, 1:02pm UTC](https://discourse.itk.org/t/inhomogeneity-correction-using-n4-and-python/6123/2 "2023-08-01T13:02:18Z")

</div>

Hello @Cambridgeshire,

Please see the [example on read-the-docs](https://simpleitk.readthedocs.io/en/master/link_N4BiasFieldCorrection_docs.html) it includes shrinking.
