20 if len(sys.argv) != 11:
22 "Usage: " + sys.argv[0] +
"<InputFileName> <OutputFileName>"
23 " <seedX> <seedY> <InitialDistance> <Sigma> <SigmoidAlpha> "
24 "<SigmoidBeta> <PropagationScaling> <NumberOfIterations>")
27 inputFileName = sys.argv[1]
28 outputFileName = sys.argv[2]
29 seedPosX = int(sys.argv[3])
30 seedPosY = int(sys.argv[4])
32 initialDistance = float(sys.argv[5])
33 sigma = float(sys.argv[6])
34 alpha = float(sys.argv[7])
35 beta = float(sys.argv[8])
36 propagationScaling = float(sys.argv[9])
37 numberOfIterations = int(sys.argv[10])
38 seedValue = -initialDistance
42 InputPixelType = itk.F
43 OutputPixelType = itk.UC
45 InputImageType =
itk.Image[InputPixelType, Dimension]
46 OutputImageType =
itk.Image[OutputPixelType, Dimension]
51 reader = ReaderType.New()
52 reader.SetFileName(inputFileName)
55 InputImageType, InputImageType]
56 smoothing = SmoothingFilterType.New()
57 smoothing.SetTimeStep(0.125)
58 smoothing.SetNumberOfIterations(5)
59 smoothing.SetConductanceParameter(9.0)
60 smoothing.SetInput(reader.GetOutput())
63 InputImageType, InputImageType]
64 gradientMagnitude = GradientFilterType.New()
65 gradientMagnitude.SetSigma(sigma)
66 gradientMagnitude.SetInput(smoothing.GetOutput())
69 sigmoid = SigmoidFilterType.New()
70 sigmoid.SetOutputMinimum(0.0)
71 sigmoid.SetOutputMaximum(1.0)
72 sigmoid.SetAlpha(alpha)
74 sigmoid.SetInput(gradientMagnitude.GetOutput())
77 InputImageType, InputImageType]
78 fastMarching = FastMarchingFilterType.New()
81 InputImageType, InputImageType, InputPixelType]
82 geodesicActiveContour = GeoActiveContourFilterType.New()
83 geodesicActiveContour.SetPropagationScaling(propagationScaling)
84 geodesicActiveContour.SetCurvatureScaling(1.0)
85 geodesicActiveContour.SetAdvectionScaling(1.0)
86 geodesicActiveContour.SetMaximumRMSError(0.02)
87 geodesicActiveContour.SetNumberOfIterations(numberOfIterations)
88 geodesicActiveContour.SetInput(fastMarching.GetOutput())
89 geodesicActiveContour.SetFeatureImage(sigmoid.GetOutput())
92 InputImageType, OutputImageType]
93 thresholder = ThresholdingFilterType.New()
94 thresholder.SetLowerThreshold(-1000.0)
95 thresholder.SetUpperThreshold(0.0)
96 thresholder.SetOutsideValue(itk.NumericTraits[OutputPixelType].min())
97 thresholder.SetInsideValue(itk.NumericTraits[OutputPixelType].max())
98 thresholder.SetInput(geodesicActiveContour.GetOutput())
101 seedPosition[0] = seedPosX
102 seedPosition[1] = seedPosY
105 node.SetValue(seedValue)
106 node.SetIndex(seedPosition)
111 seeds.InsertElement(0, node)
113 fastMarching.SetTrialPoints(seeds)
114 fastMarching.SetSpeedConstant(1.0)
117 InputImageType, OutputImageType]
119 caster1 = CastFilterType.New()
120 caster2 = CastFilterType.New()
121 caster3 = CastFilterType.New()
122 caster4 = CastFilterType.New()
124 writer1 = WriterType.New()
125 writer2 = WriterType.New()
126 writer3 = WriterType.New()
127 writer4 = WriterType.New()
129 caster1.SetInput(smoothing.GetOutput())
130 writer1.SetInput(caster1.GetOutput())
131 writer1.SetFileName(
"GeodesicActiveContourImageFilterOutput1.png")
132 caster1.SetOutputMinimum(itk.NumericTraits[OutputPixelType].min())
133 caster1.SetOutputMaximum(itk.NumericTraits[OutputPixelType].max())
136 caster2.SetInput(gradientMagnitude.GetOutput())
137 writer2.SetInput(caster2.GetOutput())
138 writer2.SetFileName(
"GeodesicActiveContourImageFilterOutput2.png")
139 caster2.SetOutputMinimum(itk.NumericTraits[OutputPixelType].min())
140 caster2.SetOutputMaximum(itk.NumericTraits[OutputPixelType].max())
143 caster3.SetInput(sigmoid.GetOutput())
144 writer3.SetInput(caster3.GetOutput())
145 writer3.SetFileName(
"GeodesicActiveContourImageFilterOutput3.png")
146 caster3.SetOutputMinimum(itk.NumericTraits[OutputPixelType].min())
147 caster3.SetOutputMaximum(itk.NumericTraits[OutputPixelType].max())
150 caster4.SetInput(fastMarching.GetOutput())
151 writer4.SetInput(caster4.GetOutput())
152 writer4.SetFileName(
"GeodesicActiveContourImageFilterOutput4.png")
153 caster4.SetOutputMinimum(itk.NumericTraits[OutputPixelType].min())
154 caster4.SetOutputMaximum(itk.NumericTraits[OutputPixelType].max())
156 fastMarching.SetOutputSize(
157 reader.GetOutput().GetBufferedRegion().GetSize())
159 writer = WriterType.New()
160 writer.SetFileName(outputFileName)
161 writer.SetInput(thresholder.GetOutput())
165 "Max. no. iterations: " +
166 str(geodesicActiveContour.GetNumberOfIterations()) +
"\n")
169 str(geodesicActiveContour.GetMaximumRMSError()) +
"\n")
171 "No. elpased iterations: " +
172 str(geodesicActiveContour.GetElapsedIterations()) +
"\n")
173 print(
"RMS change: " + str(geodesicActiveContour.GetRMSChange()) +
"\n")
179 mapWriter = InternalWriterType.New()
180 mapWriter.SetInput(fastMarching.GetOutput())
181 mapWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput4.mha")
184 speedWriter = InternalWriterType.New()
185 speedWriter.SetInput(sigmoid.GetOutput())
186 speedWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput3.mha")
189 gradientWriter = InternalWriterType.New()
190 gradientWriter.SetInput(gradientMagnitude.GetOutput())
191 gradientWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput2.mha")
192 gradientWriter.Update()