File: GeodesicActiveContourImageFilter.py

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,672 kB
  • ctags: 85,253
  • sloc: cpp: 458,133; ansic: 196,222; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 428; csh: 220; perl: 193; xml: 20
file content (141 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# GeodesicActiveContourImageFilter.py
# Translated by Charl P. Botha <http://cpbotha.net/> from the cxx original.
# Id

# example runs:
# ------------
# 1. Left ventricle:
# python GeodesicActiveContourImageFilter.py \
# ../Data/BrainProtonDensitySlice.png lventricle.png \
# 81 114 5 1 -0.5 3 2
#
# 2. White matter:
# python GeodesicActiveContourImageFilter.py \
# ../Data/BrainProtonDensitySlice.png wmatter.png \
# 56 92 5 1 -0.3 2 10
#
# See the ITK Software Guide, section 9.3.3 "Geodesic Active Contours
# Segmentation" as well as the CXX example for more comments.

import itk
from sys import argv, stderr
itk.auto_progress(2)

def main():
    if len(argv) < 10:
        errMsg = "Missing parameters\n" \
                 "Usage: %s\n" % (argv[0],) + \
                 " inputImage  outputImage\n" \
                 " seedX seedY InitialDistance\n" \
                 " Sigma SigmoidAlpha SigmoidBeta\n" \
                 " PropagationScaling\n"
        
        print >> stderr, errMsg
        return

    # We're going to build the following pipelines:
    # 1. reader -> smoothing -> gradientMagnitude -> sigmoid -> FI
    # 2. fastMarching -> geodesicActiveContour(FI) -> thresholder -> writer
    # The output of pipeline 1 is a feature image that is used by the
    # geodesicActiveContour object.  Also see figure 9.18 in the ITK
    # Software Guide.

    # we wan't to know what is happening
    # itk.auto_progress(True)
    
    InternalPixelType = itk.F
    Dimension = 2
    InternalImageType = itk.Image[InternalPixelType, Dimension]
    
    OutputPixelType = itk.UC
    OutputImageType = itk.Image[OutputPixelType, Dimension]
    
    reader = itk.ImageFileReader[InternalImageType].New(FileName=argv[1])
    # needed to give the size to the fastmarching filter
    reader.Update()
    
    smoothing = itk.CurvatureAnisotropicDiffusionImageFilter[InternalImageType, InternalImageType].New(reader,
		        TimeStep=0.125,
			NumberOfIterations=5,
			ConductanceParameter=9.0)
    
    gradientMagnitude = itk.GradientMagnitudeRecursiveGaussianImageFilter[InternalImageType, InternalImageType].New(smoothing,
                        Sigma=float(argv[6]))
    
    sigmoid = itk.SigmoidImageFilter[InternalImageType, InternalImageType].New(gradientMagnitude,
                        OutputMinimum=0.0,
			OutputMaximum=1.1,
			Alpha=float(argv[7]),
			Beta=float(argv[8]))
    
    seedPosition = itk.Index[2]()
    seedPosition.SetElement(0, int(argv[3]))
    seedPosition.SetElement(1, int(argv[4]))
	    
    node = itk.LevelSetNode[InternalPixelType, Dimension]()
    node.SetValue(-float(argv[5]))
    node.SetIndex(seedPosition)
    
    seeds = itk.VectorContainer[itk.UI, itk.LevelSetNode[InternalPixelType, Dimension]].New()
    seeds.Initialize()
    seeds.InsertElement(0, node)

    fastMarching = itk.FastMarchingImageFilter[InternalImageType, InternalImageType].New(sigmoid,
                        TrialPoints=seeds,
			SpeedConstant=1.0,
			OutputSize=reader.GetOutput().GetBufferedRegion().GetSize() )
    
    
    geodesicActiveContour = itk.GeodesicActiveContourLevelSetImageFilter[InternalImageType, InternalImageType, InternalPixelType].New(fastMarching, sigmoid,
                        PropagationScaling=float(argv[9]),
			CurvatureScaling=1.0,
			AdvectionScaling=1.0,
			MaximumRMSError=0.02,
			NumberOfIterations=800
			)
        
    thresholder = itk.BinaryThresholdImageFilter[InternalImageType, OutputImageType].New(geodesicActiveContour,
                        LowerThreshold=-1000,
			UpperThreshold=0,
			OutsideValue=0,
			InsideValue=255)

    writer = itk.ImageFileWriter[OutputImageType].New(thresholder, FileName=argv[2])
    
    
    
    
    def rescaleAndWrite(filter, fileName):
	caster = itk.RescaleIntensityImageFilter[InternalImageType, OutputImageType].New(filter,
	               OutputMinimum=0,
		       OutputMaximum=255)
	itk.write(caster, fileName)

			    
    rescaleAndWrite(smoothing, "GeodesicActiveContourImageFilterOutput1.png")
    rescaleAndWrite(gradientMagnitude, "GeodesicActiveContourImageFilterOutput2.png")
    rescaleAndWrite(sigmoid, "GeodesicActiveContourImageFilterOutput3.png")
    rescaleAndWrite(fastMarching, "GeodesicActiveContourImageFilterOutput4.png")

    writer.Update()

    
    print
    print "Max. no. iterations: %d" % (geodesicActiveContour.GetNumberOfIterations())
    print "Max. RMS error: %.3f" % (geodesicActiveContour.GetMaximumRMSError())
    print
    print "No. elapsed iterations: %d" % (geodesicActiveContour.GetElapsedIterations())
    print "RMS change: %.3f" % (geodesicActiveContour.GetRMSChange())
		    

    itk.write(fastMarching, "GeodesicActiveContourImageFilterOutput4.mha")
    itk.write(sigmoid, "GeodesicActiveContourImageFilterOutput3.mha")
    itk.write(gradientMagnitude, "GeodesicActiveContourImageFilterOutput2.mha")
		 

    
    
    

if __name__ == "__main__":
    main()