SimpleITK  2.0.0rc1.post255
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 from __future__ import print_function
21 
22 import SimpleITK as sitk
23 import sys
24 import os
25 
26 if len(sys.argv) < 10:
27  print("Usage: {0} <inputImage> <outputImage> <seedX> <seedY> <Sigma> <SigmoidAlpha> <SigmoidBeta> <TimeThreshold>".format(sys.argv[0]))
28  sys.exit(1)
29 
30 inputFilename = sys.argv[1]
31 outputFilename = sys.argv[2]
32 
33 seedPosition = (int(sys.argv[3]), int(sys.argv[4]))
34 
35 sigma = float(sys.argv[5])
36 alpha = float(sys.argv[6])
37 beta = float(sys.argv[7])
38 timeThreshold = float(sys.argv[8])
39 stoppingTime = float(sys.argv[9])
40 
41 inputImage = sitk.ReadImage(inputFilename, sitk.sitkFloat32)
42 
43 print(inputImage)
44 
46 smoothing.SetTimeStep(0.125)
47 smoothing.SetNumberOfIterations(5)
48 smoothing.SetConductanceParameter(9.0)
49 smoothingOutput = smoothing.Execute(inputImage)
50 
52 gradientMagnitude.SetSigma(sigma)
53 gradientMagnitudeOutput = gradientMagnitude.Execute(smoothingOutput)
54 
55 sigmoid = sitk.SigmoidImageFilter()
56 sigmoid.SetOutputMinimum(0.0)
57 sigmoid.SetOutputMaximum(1.0)
58 sigmoid.SetAlpha(alpha)
59 sigmoid.SetBeta(beta)
60 sigmoid.DebugOn()
61 sigmoidOutput = sigmoid.Execute(gradientMagnitudeOutput)
62 
63 
64 fastMarching = sitk.FastMarchingImageFilter()
65 
66 seedValue = 0
67 trialPoint = (seedPosition[0], seedPosition[1], seedValue)
68 
69 
70 fastMarching.AddTrialPoint(trialPoint)
71 
72 fastMarching.SetStoppingValue(stoppingTime)
73 
74 fastMarchingOutput = fastMarching.Execute(sigmoidOutput)
75 
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)
itk::simple::SigmoidImageFilter
Computes the sigmoid function pixel-wise.
Definition: sitkSigmoidImageFilter.h:46
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< std::string > &fileNames, bool useCompression=false, int compressionLevel=-1)
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:69
itk::simple::GradientMagnitudeRecursiveGaussianImageFilter
Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaus...
Definition: sitkGradientMagnitudeRecursiveGaussianImageFilter.h:42
itk::simple::BinaryThresholdImageFilter
Binarize an input image by thresholding.
Definition: sitkBinaryThresholdImageFilter.h:50
itk::simple::CurvatureAnisotropicDiffusionImageFilter
Definition: sitkCurvatureAnisotropicDiffusionImageFilter.h:60