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 import SimpleITK as sitk
21 import sys
22 import os
23 
24 
25 def main(args):
26  if len(args) < 2:
27  print(
28  "Usage: N4BiasFieldCorrection inputImage "
29  + "outputImage [shrinkFactor] [maskImage] [numberOfIterations] "
30  + "[numberOfFittingLevels]"
31  )
32  sys.exit(1)
33 
34  inputImage = sitk.ReadImage(args[1], sitk.sitkFloat32)
35  image = inputImage
36 
37  if len(args) > 4:
38  maskImage = sitk.ReadImage(args[4], sitk.sitkUInt8)
39  else:
40  maskImage = sitk.OtsuThreshold(inputImage, 0, 1, 200)
41 
42  shrinkFactor = 1
43  if len(args) > 3:
44  shrinkFactor = int(args[3])
45  if shrinkFactor > 1:
46  image = sitk.Shrink(
47  inputImage, [shrinkFactor] * inputImage.GetDimension()
48  )
49  maskImage = sitk.Shrink(
50  maskImage, [shrinkFactor] * inputImage.GetDimension()
51  )
52 
54 
55  numberFittingLevels = 4
56 
57  if len(args) > 6:
58  numberFittingLevels = int(args[6])
59 
60  if len(args) > 5:
61  corrector.SetMaximumNumberOfIterations(
62  [int(args[5])] * numberFittingLevels
63  )
64 
65  corrected_image = corrector.Execute(image, maskImage)
66 
67  log_bias_field = corrector.GetLogBiasFieldAsImage(inputImage)
68 
69  corrected_image_full_resolution = inputImage / sitk.Exp(log_bias_field)
70 
71  sitk.WriteImage(corrected_image_full_resolution, args[2])
72 
73  if shrinkFactor > 1:
75  corrected_image, "Python-Example-N4BiasFieldCorrection-shrunk.nrrd"
76  )
77 
78  return_images = {"input_image": inputImage,
79  "mask_image": maskImage,
80  "log_bias_field": log_bias_field,
81  "corrected_image": corrected_image}
82  return return_images
83 
84 
85 if __name__ == "__main__":
86  images = main(sys.argv)
87  if "SITK_NOSHOW" not in os.environ:
88  sitk.Show(images["input_image"], "Input Image 20")
89  sitk.Show(images["mask_image"], "Mask Image")
90  sitk.Show(images["log_bias_field"], "Log Bias Image")
91  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::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< std::string > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...
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::Exp
Image Exp(const Image &image1)
Computes the exponential function of each pixel.