SimpleITK  
N4BiasFieldCorrection/N4BiasFieldCorrection.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # =========================================================================
19 
20 """ A SimpleITK example that demonstrates how to apply N4BiasFieldImageFilter
21  to an image. """
22 
23 import sys
24 import os
25 import SimpleITK as sitk
26 
27 
28 def main(args):
29  """ A SimpleITK example that demonstrates how to apply N4BiasFieldImageFilter
30  to an image. """
31  if len(args) < 2:
32  print(
33  "Usage: N4BiasFieldCorrection inputImage "
34  + "outputImage [shrinkFactor] [maskImage] [numberOfIterations] "
35  + "[numberOfFittingLevels]"
36  )
37  sys.exit(1)
38 
39  inputImage = sitk.ReadImage(args[1], sitk.sitkFloat32)
40  image = inputImage
41 
42  if len(args) > 4:
43  maskImage = sitk.ReadImage(args[4], sitk.sitkUInt8)
44  else:
45  maskImage = sitk.OtsuThreshold(inputImage, 0, 1, 200)
46 
47  shrinkFactor = 1
48  if len(args) > 3:
49  shrinkFactor = int(args[3])
50  if shrinkFactor > 1:
51  image = sitk.Shrink(inputImage, [shrinkFactor] * inputImage.GetDimension())
52  maskImage = sitk.Shrink(
53  maskImage, [shrinkFactor] * inputImage.GetDimension()
54  )
55 
57 
58  numberFittingLevels = 4
59 
60  if len(args) > 6:
61  numberFittingLevels = int(args[6])
62 
63  if len(args) > 5:
64  corrector.SetMaximumNumberOfIterations([int(args[5])] * numberFittingLevels)
65 
66  corrected_image = corrector.Execute(image, maskImage)
67 
68  log_bias_field = corrector.GetLogBiasFieldAsImage(inputImage)
69 
70  corrected_image_full_resolution = inputImage / sitk.Exp(log_bias_field)
71 
72  sitk.WriteImage(corrected_image_full_resolution, args[2])
73 
74  if shrinkFactor > 1:
76  corrected_image, "Python-Example-N4BiasFieldCorrection-shrunk.nrrd"
77  )
78 
79  return_images = {
80  "input_image": inputImage,
81  "mask_image": maskImage,
82  "log_bias_field": log_bias_field,
83  "corrected_image": corrected_image,
84  }
85  return return_images
86 
87 
88 if __name__ == "__main__":
89  images = main(sys.argv)
90  if "SITK_NOSHOW" not in os.environ:
91  sitk.Show(images["input_image"], "Input Image 20")
92  sitk.Show(images["mask_image"], "Mask Image")
93  sitk.Show(images["log_bias_field"], "Log Bias Image")
94  sitk.Show(images["corrected_image"], "N4 Corrected")
itk::simple::OtsuThreshold
Image OtsuThreshold(const Image &image, uint8_t insideValue=1u, uint8_t outsideValue=0u, uint32_t numberOfHistogramBins=128u, bool maskOutput=true, uint8_t maskValue=255u, bool returnBinMidpoint=false)
Threshold an image using the Otsu Threshold.
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::N4BiasFieldCorrectionImageFilter
Implementation of the N4 bias field correction algorithm.
Definition: sitkN4BiasFieldCorrectionImageFilter.h:73
itk::simple::Shrink
Image Shrink(const Image &image1, std::vector< unsigned int > shrinkFactors=std::vector< unsigned int >(3, 1))
Reduce the size of an image by an integer factor in each dimension.
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< PathType > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...
itk::simple::Exp
Image Exp(const Image &image1)
Computes the exponential function of each pixel.
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< PathType > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...