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 
21 import SimpleITK as sitk
22 import sys
23 
24 
25 def main(args):
26  if len(args) < 10:
27  print(
28  "Usage:",
29  "FastMarchingSegmentation",
30  "<inputImage> <outputImage> <seedX> <seedY> <Sigma>",
31  "<SigmoidAlpha> <SigmoidBeta> <TimeThreshold>",
32  "<StoppingTime>"
33  )
34  sys.exit(1)
35 
36  inputFilename = args[1]
37  outputFilename = args[2]
38 
39  seedPosition = (int(args[3]), int(args[4]))
40 
41  sigma = float(args[5])
42  alpha = float(args[6])
43  beta = float(args[7])
44  timeThreshold = float(args[8])
45  stoppingTime = float(args[9])
46 
47  inputImage = sitk.ReadImage(inputFilename, sitk.sitkFloat32)
48 
50  smoothing.SetTimeStep(0.125)
51  smoothing.SetNumberOfIterations(5)
52  smoothing.SetConductanceParameter(9.0)
53  smoothingOutput = smoothing.Execute(inputImage)
54 
56  gradientMagnitude.SetSigma(sigma)
57  gradientMagnitudeOutput = gradientMagnitude.Execute(smoothingOutput)
58 
59  sigmoid = sitk.SigmoidImageFilter()
60  sigmoid.SetOutputMinimum(0.0)
61  sigmoid.SetOutputMaximum(1.0)
62  sigmoid.SetAlpha(alpha)
63  sigmoid.SetBeta(beta)
64  sigmoidOutput = sigmoid.Execute(gradientMagnitudeOutput)
65 
66  fastMarching = sitk.FastMarchingImageFilter()
67 
68  seedValue = 0
69  trialPoint = (seedPosition[0], seedPosition[1], seedValue)
70 
71  fastMarching.AddTrialPoint(trialPoint)
72 
73  fastMarching.SetStoppingValue(stoppingTime)
74 
75  fastMarchingOutput = fastMarching.Execute(sigmoidOutput)
76 
77  thresholder = sitk.BinaryThresholdImageFilter()
78  thresholder.SetLowerThreshold(0.0)
79  thresholder.SetUpperThreshold(timeThreshold)
80  thresholder.SetOutsideValue(0)
81  thresholder.SetInsideValue(255)
82 
83  result = thresholder.Execute(fastMarchingOutput)
84 
85  sitk.WriteImage(result, outputFilename)
86 
87  image_dict = {"InputImage": inputImage,
88  "SpeedImage": sigmoidOutput,
89  "TimeCrossingMap": fastMarchingOutput,
90  "Segmentation": result,
91  }
92  return image_dict
93 
94 
95 if __name__ == "__main__":
96  return_dict = main(sys.argv)
itk::simple::SigmoidImageFilter
Computes the sigmoid function pixel-wise.
Definition: sitkSigmoidImageFilter.h:45
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::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::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