ITK  4.5.0
Insight Segmentation and Registration Toolkit
Segmentation/GeodesicActiveContourImageFilter.py
1 #==========================================================================
2 #
3 # Copyright Insight Software Consortium
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #==========================================================================*/
18 
19 # GeodesicActiveContourImageFilter.py
20 # Translated by Charl P. Botha <http://cpbotha.net/> from the cxx original.
21 # Id
22 
23 # NOTE: This example won't work if your ITK is older that 2004-02-26
24 
25 # example runs:
26 # ------------
27 # 1. Left ventricle:
28 # python GeodesicActiveContourImageFilter.py \
29 # ../Data/BrainProtonDensitySlice.png lventricle.png \
30 # 81 114 5 1 -0.5 3 2
31 #
32 # 2. White matter:
33 # python GeodesicActiveContourImageFilter.py \
34 # ../Data/BrainProtonDensitySlice.png wmatter.png \
35 # 56 92 5 1 -0.3 2 10
36 #
37 # See the ITK Software Guide, section 9.3.3 "Geodesic Active Contours
38 # Segmentation" as well as the CXX example for more comments.
39 
40 import InsightToolkit as itk
41 import sys
42 
43 def main():
44  if len(sys.argv) < 10:
45  errMsg = "Missing parameters\n" \
46  "Usage: %s\n" % (sys.argv[0],) + \
47  " inputImage outputImage\n" \
48  " seedX seedY InitialDistance\n" \
49  " Sigma SigmoidAlpha SigmoidBeta\n" \
50  " PropagationScaling\n"
51 
52  print >> sys.stderr, errMsg
53  return
54 
55  # We're going to build the following pipelines:
56  # 1. reader -> smoothing -> gradientMagnitude -> sigmoid -> FI
57  # 2. fastMarching -> geodesicActiveContour(FI) -> thresholder -> writer
58  # The output of pipeline 1 is a feature image that is used by the
59  # geodesicActiveContour object. Also see figure 9.18 in the ITK
60  # Software Guide.
61 
62  reader = itk.itkImageFileReaderF2_New()
63  reader.SetFileName(sys.argv[1])
64 
65  writer = itk.itkImageFileWriterUS2_New()
66  writer.SetFileName(sys.argv[2])
67 
68  smoothing = itk.itkCurvatureAnisotropicDiffusionImageFilterF2F2_New()
69 
70  gM = itk.itkGradientMagnitudeRecursiveGaussianImageFilterF2F2_New()
71  gradientMagnitude = gM
72 
73  sigmoid = itk.itkSigmoidImageFilterF2F2_New()
74  sigmoid.SetOutputMinimum( 0.0 )
75  sigmoid.SetOutputMaximum( 1.0 )
76 
77  fastMarching = itk.itkFastMarchingImageFilterF2F2_New()
78 
79  gAC = itk.itkGeodesicActiveContourLevelSetImageFilterF2F2_New()
80  geodesicActiveContour = gAC
81 
82  propagationScaling = float(sys.argv[9])
83 
84  geodesicActiveContour.SetPropagationScaling( propagationScaling );
85  geodesicActiveContour.SetCurvatureScaling( 1.0 );
86  geodesicActiveContour.SetAdvectionScaling( 1.0 );
87  geodesicActiveContour.SetMaximumRMSError( 0.02 );
88  geodesicActiveContour.SetNumberOfIterations( 800 );
89 
90  smoothing.SetInput( reader.GetOutput() );
91  gradientMagnitude.SetInput( smoothing.GetOutput() );
92  sigmoid.SetInput( gradientMagnitude.GetOutput() );
93 
94  geodesicActiveContour.SetInput( fastMarching.GetOutput() );
95  geodesicActiveContour.SetFeatureImage( sigmoid.GetOutput() );
96 
97  thresholder = itk.itkBinaryThresholdImageFilterF2US2_New()
98  thresholder.SetLowerThreshold( -1000.0 );
99  thresholder.SetUpperThreshold( 0.0 );
100  thresholder.SetOutsideValue( 0 );
101  thresholder.SetInsideValue( 65535 );
102 
103  thresholder.SetInput( geodesicActiveContour.GetOutput() );
104  writer.SetInput( thresholder.GetOutput() );
105 
106  smoothing.SetTimeStep( 0.125 );
107  smoothing.SetNumberOfIterations( 5 );
108  smoothing.SetConductanceParameter( 3.0 );
109 
110  sigma = float(sys.argv[6])
111  gradientMagnitude.SetSigma(sigma);
112 
113  alpha = float(sys.argv[7])
114  beta = float(sys.argv[8])
115 
116  sigmoid.SetAlpha(alpha)
117  sigmoid.SetBeta(beta)
118 
119  # same as image
120  seedPosition = itk.itkIndex2()
121  seedPosition.SetElement(0, int(sys.argv[3]))
122  seedPosition.SetElement(1, int(sys.argv[4]))
123 
124  initialDistance = float(sys.argv[5])
125  seedValue = - initialDistance
126  node = itk.itkLevelSetNodeF2()
127  node.SetValue(seedValue)
128  node.SetIndex(seedPosition)
129 
130  seeds = itk.itkNodeContainerF2_New()
131  seeds.Initialize()
132  seeds.InsertElement(0, node)
133 
134  fastMarching.SetTrialPoints(seeds.GetPointer())
135 
136  fastMarching.SetSpeedConstant(1.0)
137 
138  caster1 = itk.itkRescaleIntensityImageFilterF2US2_New()
139  writer1 = itk.itkImageFileWriterUS2_New()
140  caster1.SetInput( smoothing.GetOutput() );
141  writer1.SetInput( caster1.GetOutput() );
142  writer1.SetFileName("GeodesicActiveContourImageFilterOutput1.png");
143  caster1.SetOutputMinimum( 0 );
144  caster1.SetOutputMaximum( 65535 );
145  writer1.Update();
146 
147  caster2 = itk.itkRescaleIntensityImageFilterF2US2_New()
148  writer2 = itk.itkImageFileWriterUS2_New()
149  caster2.SetInput( gradientMagnitude.GetOutput() );
150  writer2.SetInput( caster2.GetOutput() );
151  writer2.SetFileName("GeodesicActiveContourImageFilterOutput2.png");
152  caster2.SetOutputMinimum( 0 );
153  caster2.SetOutputMaximum( 65535 );
154  writer2.Update();
155 
156  caster3 = itk.itkRescaleIntensityImageFilterF2US2_New()
157  writer3 = itk.itkImageFileWriterUS2_New()
158  caster3.SetInput( sigmoid.GetOutput() );
159  writer3.SetInput( caster3.GetOutput() );
160  writer3.SetFileName("GeodesicActiveContourImageFilterOutput3.png");
161  caster3.SetOutputMinimum( 0 );
162  caster3.SetOutputMaximum( 65535 );
163  writer3.Update();
164 
165  caster4 = itk.itkRescaleIntensityImageFilterF2US2_New()
166  writer4 = itk.itkImageFileWriterUS2_New()
167  caster4.SetInput( fastMarching.GetOutput() );
168  writer4.SetInput( caster4.GetOutput() );
169  writer4.SetFileName("GeodesicActiveContourImageFilterOutput4.png");
170  caster4.SetOutputMinimum( 0 );
171  caster4.SetOutputMaximum( 65535 );
172 
173  fastMarching.SetOutputSize(
174  reader.GetOutput().GetBufferedRegion().GetSize())
175 
176  writer.Update()
177 
178  # Print out some useful information
179  print "\n"
180  print "Max. no. iterations: %d" % (gAC.GetNumberOfIterations())
181  print "Max. RMS error: %.3f" % (gAC.GetMaximumRMSError())
182  print "No. elapsed iterations: %d" % (gAC.GetElapsedIterations())
183  print "RMS change: %.3f" % (gAC.GetRMSChange())
184 
185  writer4.Update()
186 
187  mapWriter = itk.itkImageFileWriterF2_New()
188  mapWriter.SetInput(fastMarching.GetOutput())
189  mapWriter.SetFileName("GeodesicActiveContourImageFilterOutput4.mha")
190  mapWriter.Update()
191 
192  speedWriter = itk.itkImageFileWriterF2_New()
193  speedWriter.SetInput( sigmoid.GetOutput() )
194  speedWriter.SetFileName("GeodesicActiveContourImageFilterOutput3.mha")
195  speedWriter.Update()
196 
197  gradientWriter = itk.itkImageFileWriterF2_New()
198  gradientWriter.SetInput( gradientMagnitude.GetOutput() )
199  gradientWriter.SetFileName("GeodesicActiveContourImageFilterOutput2.mha")
200  gradientWriter.Update();
201 
202 if __name__ == "__main__":
203  main()