def bias_correct(
input: sitk.Image,
mask: sitk.Image = None,
shrink_factor: int = 4,
num_fitting_levels: int = 4,
num_iterations: int = 50,
) -> sitk.Image:
"""Perform N4 bias correction on MRI
Note:
- if no mask is provided it will be generated using Otsu-thresholding
"""
if not isinstance(mask, sitk.Image):
mask = sitk.OtsuThreshold(input, 0, 1, 200)
input = sitk.Cast(input, sitk.sitkFloat32)
image = sitk.Shrink(input, [shrink_factor] * input.GetDimension())
mask = sitk.Shrink(mask, [shrink_factor] * input.GetDimension())
corrector = sitk.N4BiasFieldCorrectionImageFilter()
corrector.SetMaximumNumberOfIterations([num_iterations] * num_fitting_levels)
corrector.Execute(image, mask)
log_bias_field = corrector.GetLogBiasFieldAsImage(input)
corrected_image_full_resolution = input / sitk.Exp(log_bias_field)
return corrected_image_full_resolution
But since ITK Python does not have the convenience function GetLogBiasFieldAsImage and does not wrap itk::BSplineControlPointImageFilter, it seems I cannot implement bias correction as in ANTs using ITK Python.
If (there are no workarounds) I might do a pull request, but I wonder if I should implement the GetLogBiasFieldAsImage like helper or wrap BSplineControlPointImageFilter to implement something like this.
Then again, I might just install ITK (for MONAI ITKReader) and SimpleITK (for image pre-processing) side-by-side, since dealing with image/filter types is much easier and auto-completion works (for simpleitk)