20 parser = argparse.ArgumentParser(
21 description=
"Segment With Geodesic Active Contour Level Set."
23 parser.add_argument(
"input_image")
24 parser.add_argument(
"output_image")
25 parser.add_argument(
"seed_x", type=int)
26 parser.add_argument(
"seed_y", type=int)
27 parser.add_argument(
"initial_distance", type=float)
28 parser.add_argument(
"sigma", type=float)
29 parser.add_argument(
"sigmoid_alpha", type=float)
30 parser.add_argument(
"sigmoid_beta", type=float)
31 parser.add_argument(
"propagation_scaling", type=float)
32 parser.add_argument(
"number_of_iterations", type=int)
33 args = parser.parse_args()
35 seedValue = -args.initial_distance
39 InputPixelType = itk.F
40 OutputPixelType = itk.UC
42 InputImageType =
itk.Image[InputPixelType, Dimension]
43 OutputImageType =
itk.Image[OutputPixelType, Dimension]
48 reader = ReaderType.New()
49 reader.SetFileName(args.input_image)
52 InputImageType, InputImageType
54 smoothing = SmoothingFilterType.New()
55 smoothing.SetTimeStep(0.125)
56 smoothing.SetNumberOfIterations(5)
57 smoothing.SetConductanceParameter(9.0)
58 smoothing.SetInput(reader.GetOutput())
61 InputImageType, InputImageType
63 gradientMagnitude = GradientFilterType.New()
64 gradientMagnitude.SetSigma(args.sigma)
65 gradientMagnitude.SetInput(smoothing.GetOutput())
68 sigmoid = SigmoidFilterType.New()
69 sigmoid.SetOutputMinimum(0.0)
70 sigmoid.SetOutputMaximum(1.0)
71 sigmoid.SetAlpha(args.sigmoid_alpha)
72 sigmoid.SetBeta(args.sigmoid_beta)
73 sigmoid.SetInput(gradientMagnitude.GetOutput())
76 fastMarching = FastMarchingFilterType.New()
79 InputImageType, InputImageType, InputPixelType
81 geodesicActiveContour = GeoActiveContourFilterType.New()
82 geodesicActiveContour.SetPropagationScaling(args.propagation_scaling)
83 geodesicActiveContour.SetCurvatureScaling(1.0)
84 geodesicActiveContour.SetAdvectionScaling(1.0)
85 geodesicActiveContour.SetMaximumRMSError(0.02)
86 geodesicActiveContour.SetNumberOfIterations(args.number_of_iterations)
87 geodesicActiveContour.SetInput(fastMarching.GetOutput())
88 geodesicActiveContour.SetFeatureImage(sigmoid.GetOutput())
91 thresholder = ThresholdingFilterType.New()
92 thresholder.SetLowerThreshold(-1000.0)
93 thresholder.SetUpperThreshold(0.0)
96 thresholder.SetInput(geodesicActiveContour.GetOutput())
99 seedPosition[0] = args.seed_x
100 seedPosition[1] = args.seed_y
103 node.SetValue(seedValue)
104 node.SetIndex(seedPosition)
108 seeds.InsertElement(0, node)
110 fastMarching.SetTrialPoints(seeds)
111 fastMarching.SetSpeedConstant(1.0)
115 caster1 = CastFilterType.New()
116 caster2 = CastFilterType.New()
117 caster3 = CastFilterType.New()
118 caster4 = CastFilterType.New()
120 writer1 = WriterType.New()
121 writer2 = WriterType.New()
122 writer3 = WriterType.New()
123 writer4 = WriterType.New()
125 caster1.SetInput(smoothing.GetOutput())
126 writer1.SetInput(caster1.GetOutput())
127 writer1.SetFileName(
"GeodesicActiveContourImageFilterOutput1.png")
132 caster2.SetInput(gradientMagnitude.GetOutput())
133 writer2.SetInput(caster2.GetOutput())
134 writer2.SetFileName(
"GeodesicActiveContourImageFilterOutput2.png")
139 caster3.SetInput(sigmoid.GetOutput())
140 writer3.SetInput(caster3.GetOutput())
141 writer3.SetFileName(
"GeodesicActiveContourImageFilterOutput3.png")
146 caster4.SetInput(fastMarching.GetOutput())
147 writer4.SetInput(caster4.GetOutput())
148 writer4.SetFileName(
"GeodesicActiveContourImageFilterOutput4.png")
152 fastMarching.SetOutputSize(reader.GetOutput().GetBufferedRegion().GetSize())
154 writer = WriterType.New()
155 writer.SetFileName(args.output_image)
156 writer.SetInput(thresholder.GetOutput())
160 "Max. no. iterations: " + str(geodesicActiveContour.GetNumberOfIterations()) +
"\n"
162 print(
"Max. RMS error: " + str(geodesicActiveContour.GetMaximumRMSError()) +
"\n")
164 "No. elpased iterations: "
165 + str(geodesicActiveContour.GetElapsedIterations())
168 print(
"RMS change: " + str(geodesicActiveContour.GetRMSChange()) +
"\n")
174 mapWriter = InternalWriterType.New()
175 mapWriter.SetInput(fastMarching.GetOutput())
176 mapWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput4.mha")
179 speedWriter = InternalWriterType.New()
180 speedWriter.SetInput(sigmoid.GetOutput())
181 speedWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput3.mha")
184 gradientWriter = InternalWriterType.New()
185 gradientWriter.SetInput(gradientMagnitude.GetOutput())
186 gradientWriter.SetFileName(
"GeodesicActiveContourImageFilterOutput2.mha")
187 gradientWriter.Update()