1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20""" A SimpleITK example that demonstrates how to apply N4BiasFieldImageFilter
21 to an image. """
22
23import sys
24import os
25import SimpleITK as sitk
26
27
28def 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
40 image = inputImage
41
42 if len(args) > 4:
44 else:
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())
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
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
88if __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")
Implementation of the N4 bias field correction algorithm.
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...
Image OtsuThreshold(const Image &image, const Image &maskImage, 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.
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
Image Exp(Image &&image1)
Computes the exponential function of each pixel.
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.
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...