SimpleITK  
FastMarchingSegmentation/FastMarchingSegmentation.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 demonstrating the FastMarchingImageFilter class. """
21 
22 import sys
23 import SimpleITK as sitk
24 
25 
26 def main(args):
27  """ A SimpleITK example demonstrating the FastMarchingImageFilter class. """
28  if len(args) < 10:
29  print(
30  "Usage:",
31  "FastMarchingSegmentation",
32  "<inputImage> <outputImage> <seedX> <seedY> <Sigma>",
33  "<SigmoidAlpha> <SigmoidBeta> <TimeThreshold>",
34  "<StoppingTime>",
35  )
36  sys.exit(1)
37 
38  inputFilename = args[1]
39  outputFilename = args[2]
40 
41  seedPosition = (int(args[3]), int(args[4]))
42 
43  sigma = float(args[5])
44  alpha = float(args[6])
45  beta = float(args[7])
46  timeThreshold = float(args[8])
47  stoppingTime = float(args[9])
48 
49  inputImage = sitk.ReadImage(inputFilename, sitk.sitkFloat32)
50 
52  smoothing.SetTimeStep(0.125)
53  smoothing.SetNumberOfIterations(5)
54  smoothing.SetConductanceParameter(9.0)
55  smoothingOutput = smoothing.Execute(inputImage)
56 
58  gradientMagnitude.SetSigma(sigma)
59  gradientMagnitudeOutput = gradientMagnitude.Execute(smoothingOutput)
60 
61  sigmoid = sitk.SigmoidImageFilter()
62  sigmoid.SetOutputMinimum(0.0)
63  sigmoid.SetOutputMaximum(1.0)
64  sigmoid.SetAlpha(alpha)
65  sigmoid.SetBeta(beta)
66  sigmoidOutput = sigmoid.Execute(gradientMagnitudeOutput)
67 
68  fastMarching = sitk.FastMarchingImageFilter()
69 
70  seedValue = 0
71  trialPoint = (seedPosition[0], seedPosition[1], seedValue)
72 
73  fastMarching.AddTrialPoint(trialPoint)
74 
75  fastMarching.SetStoppingValue(stoppingTime)
76 
77  fastMarchingOutput = fastMarching.Execute(sigmoidOutput)
78 
79  thresholder = sitk.BinaryThresholdImageFilter()
80  thresholder.SetLowerThreshold(0.0)
81  thresholder.SetUpperThreshold(timeThreshold)
82  thresholder.SetOutsideValue(0)
83  thresholder.SetInsideValue(255)
84 
85  result = thresholder.Execute(fastMarchingOutput)
86 
87  sitk.WriteImage(result, outputFilename)
88 
89  image_dict = {
90  "InputImage": inputImage,
91  "SpeedImage": sigmoidOutput,
92  "TimeCrossingMap": fastMarchingOutput,
93  "Segmentation": result,
94  }
95  return image_dict
96 
97 
98 if __name__ == "__main__":
99  return_dict = main(sys.argv)
itk::simple::SigmoidImageFilter
Computes the sigmoid function pixel-wise.
Definition: sitkSigmoidImageFilter.h:45
itk::simple::FastMarchingImageFilter
Solve an Eikonal equation using Fast Marching.
Definition: sitkFastMarchingImageFilter.h:68
itk::simple::GradientMagnitudeRecursiveGaussianImageFilter
Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaus...
Definition: sitkGradientMagnitudeRecursiveGaussianImageFilter.h:41
itk::simple::BinaryThresholdImageFilter
Binarize an input image by thresholding.
Definition: sitkBinaryThresholdImageFilter.h:49
itk::simple::CurvatureAnisotropicDiffusionImageFilter
This filter performs anisotropic diffusion on a scalar itk::Image using the modified curvature diffus...
Definition: sitkCurvatureAnisotropicDiffusionImageFilter.h:59
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::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...