Elior Drori, Mezer Lab, The Hebrew University of Jerusalem, 2023
mri_unbias estimates and corrects smooth intensity bias fields in 3D brain MRI
images. It fits an n-th degree 3D polynomial to voxel intensities inside a
region assumed to be homogeneous, typically a white-matter mask. The fitted
polynomial models the spatial bias field, which is then evaluated over the full
image and used to correct the original volume.
This repository contains two implementations of the same method:
matlab/ MATLAB implementation and example script
python/ Python package, CLI, examples, and tests
example_data/ Shared example NIfTI image and white-matter mask
- Provide a 3D MRI image and a binary mask for a homogeneous tissue region.
- Fit a smooth 3D polynomial of degree
poly_degreeto the masked intensities. - Evaluate the polynomial across the full image to estimate the bias field.
- Divide the image by the estimated bias field.
The key assumption is that intensity variation inside the mask primarily reflects scanner/acquisition bias rather than biological variation.
Run the MATLAB example:
addpath('matlab')
mri_unbias_exampleRun the Python example:
mri-unbias-exampleThis writes corrected NIfTI outputs and a before/after visualization under
example_data/output/python/.
Run the Python CLI on explicit NIfTI inputs:
mri-unbias \
example_data/t1_fl3d_FA30.nii.gz \
example_data/WM_mask.nii.gz \
--degree 3 \
--corrected example_data/output/python/t1_fl3d_FA30_unbiased_poly3.nii.gz \
--bias-field example_data/output/python/t1_fl3d_FA30_bias_estimation_poly3.nii.gzMain function:
[img_corrected, bias_field] = mri_unbias(img, wm_mask, poly_degree)The MATLAB implementation requires PolyfitnTools from MATLAB File Exchange:
https://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
Make sure both polyfitn.m and polyvaln.m are in your MATLAB path.
Example:
addpath('matlab')
mri_unbias_exampleThe example loads the shared data in example_data/, writes corrected NIfTI
outputs, generates a before/after visualization, and runs polynomial-degree
diagnostics.
Install the Python package from the python/ directory:
cd python
pip install -e ".[all]"Core NumPy API:
import nibabel as nib
from mri_unbias import mri_unbias
img_nii = nib.load("../example_data/t1_fl3d_FA30.nii.gz")
mask_nii = nib.load("../example_data/WM_mask.nii.gz")
img = img_nii.get_fdata()
wm_mask = mask_nii.get_fdata() > 0
img_corrected, bias_field = mri_unbias(img, wm_mask, degree=3)Optional brain-mask stabilization:
brain_mask = nib.load("brain_mask.nii.gz").get_fdata() > 0
img_corrected, bias_field = mri_unbias(
img,
wm_mask,
degree=3,
brain_mask=brain_mask,
)Providing brain_mask does not change the polynomial fit. It restricts the
applied bias-field range outside the brain to the full min/max bias range
observed inside the brain mask. This is optional and mainly avoids absurd
polynomial extrapolation values outside the brain.
Command-line NIfTI workflow:
mri-unbias \
../example_data/t1_fl3d_FA30.nii.gz \
../example_data/WM_mask.nii.gz \
--degree 3 \
--corrected ../example_data/output/python/t1_fl3d_FA30_unbiased_poly3.nii.gz \
--bias-field ../example_data/output/python/t1_fl3d_FA30_bias_estimation_poly3.nii.gzWith optional outside-brain bias stabilization:
mri-unbias \
image.nii.gz \
wm_mask.nii.gz \
--degree 3 \
--brain-mask brain_mask.nii.gz \
--corrected image_unbiased.nii.gz \
--bias-field image_bias.nii.gzRun the bundled example data from the source repository:
mri-unbias-examplePython tests:
cd python
pytestOutputs are saved in separate implementation-specific folders:
example_data/output/matlab/
example_data/output/python/
| File | Description |
|---|---|
t1_fl3d_FA30_unbiased_poly3.nii.gz |
Corrected image |
t1_fl3d_FA30_bias_estimation_poly3.nii.gz |
Estimated bias field |
before_after_visualization.png |
Before/after visualization |
diagnostics_polynomial_degree_selection.png |
MATLAB diagnostic elbow plot |
The diagnostics test multiple polynomial degrees, run bias correction for each degree, and measure intensity uniformity inside the mask using white-matter standard deviation or coefficient of variation. The elbow point is the first degree where the relative improvement falls below a threshold, with 10% used by default.
In typical brain MRI, the bias field is smooth and slowly varying across the brain. Low-degree polynomials, typically degree 1-3, are usually sufficient. Higher degrees may begin to fit anatomical variability rather than true bias.
- The choice of polynomial degree balances smoothness and flexibility.
- The method assumes the mask region is approximately homogeneous in intensity.
- Because the bias field is estimated from white matter, this approach should not be used when the study directly analyzes white-matter intensities. In that case, estimate the bias from another reference tissue or use a different correction approach.
This toolbox is first introduced in Drori et al., 2026 (preprint). If you use this toolbox in your research, please cite:
Drori, E., Kurer, N., and Mezer, AA. Sensorimotor basal ganglia circuit asymmetry explains lateralized motor dysfunction in early Parkinson's disease. bioRxiv (2026). https://doi.org/10.64898/2026.03.16.711841