# N4 Bias Field Correction

**URL:** https://discourse.itk.org/t/n4-bias-field-correction/3972
**Category:** Algorithms
**Tags:** python, n4biasfield, simpleitk
**Created:** [March 21, 2021, 5:24pm UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972 "2021-03-21T17:24:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![banikr](https://discourse.itk.org/user_avatar/discourse.itk.org/banikr/32/3296_2.png) [@banikr](https://discourse.itk.org/u/banikr)
#### Post date: [March 21, 2021, 5:24pm UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972/1 "2021-03-21T17:24:15Z")

</div>

Hello All,  
I want to apply bias field correction in 3D T1w MR volumes(including non-brain sections too). Images are in NIfTI format. Though it is recommended to use masks, I don’t have masks or atlas for the data.  
I saw the documentation here, [N4 Bias Field Correction — SimpleITK 2.0rc2 documentation](https://simpleitk.readthedocs.io/en/master/link_N4BiasFieldCorrection_docs.html)  
and ran the following codes:

```
input = sitk.ReadImage(files[0])
image = sitk.Cast(input, sitk.sitkFloat32)
# print(type(image))
correctedImg = sitk.N4BiasFieldCorrection(image)
sitk.WriteImage(correctedImg, outfile) # outfile is just string variable or path

```

But the output comes totally blank. What am I missing?

Edit: I got the bias corrected image in MNI format. Is there a way to get the images in native MR space?

---

<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: [March 22, 2021, 1:06am UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972/2 "2021-03-22T01:06:40Z")

</div>

Hello @banikr,

Not sure what is the format of ‘outfile’, so it could be an issue there. Possibly try looking at the images before writing to disk:

```auto
import SimpleITK as sitk

image = sitk.ReadImage(files[0], sitk.sitkFloat32)
sitk.Show(image, 'original image')
sitk.Show(sitk.N4BiasFieldCorrection(image), 'corrected image')

```

When writing, SimpleITK infers the format based on the file extension. Saving a float image in an ‘sitk.sitkUInt8’ format will lead to unexpected results.

---

<div class="post-metadata">

### Author: ![banikr](https://discourse.itk.org/user_avatar/discourse.itk.org/banikr/32/3296_2.png) [@banikr](https://discourse.itk.org/u/banikr)
#### Post date: [March 23, 2021, 8:46pm UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972/3 "2021-03-23T20:46:47Z")

</div>

Hi @zivy  
The codes worked and perform bias field correction.  
 ![image](https://discourse.itk.org/uploads/default/original/2X/f/f67e3a19acb1a484aa4692bf2c8b6117fe19906f.png) ![image](https://discourse.itk.org/uploads/default/original/2X/d/d887942be0ab4e9af0cf282e807b196bf9f06db2.png)  
Are there ways to varify that the bias field correction actually worked?  
Is it performing bias field correction in just the brain part or whole head? I needed the whole head bias correction.

In the main image, the intensity ranged from `0~499`,  
in the corrected image I see an intensity range: `0~489.8`  
Apart from that, there are no significant changes.

N.B. `outfile` is just the path of the corrected image.

---

<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: [March 24, 2021, 7:17pm UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972/4 "2021-03-24T19:17:09Z")

</div>

Hello @banikr,

If you did not provide a mask then it uses the whole image for computing the bias field. As you are actually only interested in the head, you likely can use simple thresholding, possibly try Otsu to define the head mask:

```auto
mask = sitk.OtsuThreshold(image,0,1)

```

and provide the mask as input to the bias field correction. Don’t forget to check that the mask actually segments the head.

To verify that the correction actually works you can evaluate it with a reasonably large dataset, see [this TCIA collection](https://wiki.cancerimagingarchive.net/display/Public/TCGA-GBM).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 28, 2021, 2:42pm UTC](https://discourse.itk.org/t/n4-bias-field-correction/3972/5 "2021-12-28T14:42:27Z")

</div>

A post was split to a new topic: [Read a JPEG series](https://discourse.itk.org/t/read-a-jpeg-series/4704)
