N4-Biascorrection Matlab

Description

This repo contains the source code for the N4BiasCorrection functionality that is to be called from matlab as a matlab executable.

This code provides an MATLAB C++ interface to call N4BiasCorrection implemented by [SimpleITK](Source)

Building The Executable MEX

Note: The pre-compiled binary can be found here. Should you choose to compile on your own, continue reading.

  1. git clone the SimpleITK project and compile following the official doc. The functionalities of this repo requires the compiled libraries from the SimpleITK project.

  2. Clone this repo and in CMakeLists.txt, modify the variables SimpleITK_DIR and ITK_DIR.

If you built your SimpleITK using the super build example provided by their official document, these two variables should point to the SimpleITK-build/ folder and the ITK-build/ folder. You should be able to find the files SimpleITKConfig.cmake and ITKConfig.cmake respectively in these folders. See this tutorial on how to configure Cmake for building SimpleITK applications.

  1. Run cmake.
mkdir -p build
cd build/
cmake ..

Should you choose to compile with the debug mode, Run cmake with cmake -DCMAKE_BUILD_TYPE=Debug ./ . Debug configuration example for VS Code is included in launch.json

  1. Compile
make

Running N4BiasCorrection in Matlab

Make sure the compiled executable is in matlab's search path.

[corrected_image, log_bias_field] = N4BiasCorrection(image);

This function supports only non-conplex 2D and 3D images with data type float(single) and double.

One can also produce the bias-corrected image with:

corrected_image = image ./ exp(log_bias_field);

Demo

Bias Correction Demonstration