ITK  4.8.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussian/Code.py
1 #!/usr/bin/env python
2 
3 #=========================================================================
4 #
5 # Copyright Insight Software Consortium
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License")
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http:*www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 #=========================================================================
20 
21 import sys
22 import itk
23 
24 from distutils.version import StrictVersion as VS
25 if VS(itk.Version.GetITKVersion()) < VS("4.7.0"):
26  print("ITK 4.7.0 is required (see example documentation).")
27  sys.exit(1)
28 
29 if len(sys.argv) != 5:
30  print(
31  "Usage: " + sys.argv[0] +
32  " [InputFileName] [OutputFileNameX] [OutputFileNameY]" +
33  " [OutputFileNameMagnitude]")
34  sys.exit(1)
35 
36 inputFileName = sys.argv[1]
37 outputFileNameX = sys.argv[2]
38 outputFileNameY = sys.argv[3]
39 outputFileNameMagnitude = sys.argv[4]
40 
41 Dimension = 2
42 
43 filenames = []
44 filenames.append(outputFileNameX)
45 filenames.append(outputFileNameY)
46 
47 # Input and output are png files, use unsigned char
48 PixelType = itk.UC
49 ImageType = itk.Image[PixelType, Dimension]
50 # Float type for GradientRecursiveGaussianImageFilter
51 FloatPixelType = itk.F
52 FloatImageType = itk.Image[FloatPixelType, Dimension]
53 # The output of GradientRecursiveGaussianImageFilter
54 # are images of the gradient along X and Y, so the type of
55 # the output is a covariant vector of dimension 2 (X, Y)
56 CovPixelType = itk.CovariantVector[FloatPixelType, Dimension]
57 CovImageType = itk.Image[CovPixelType, Dimension]
58 
59 ReaderType = itk.ImageFileReader[ImageType]
60 reader = ReaderType.New()
61 reader.SetFileName(inputFileName)
62 
63 FilterType = itk.GradientRecursiveGaussianImageFilter[ImageType, CovImageType]
64 gradientFilter = FilterType.New()
65 gradientFilter.SetInput(reader.GetOutput())
66 
67 # Allows to select the X or Y output images
68 IndexSelectionType = itk.VectorIndexSelectionCastImageFilter[
69  CovImageType, FloatImageType]
70 indexSelectionFilter = IndexSelectionType.New()
71 indexSelectionFilter.SetInput(gradientFilter.GetOutput())
72 
73 # Rescale for png output
74 RescalerType = itk.RescaleIntensityImageFilter[FloatImageType, ImageType]
75 rescaler = RescalerType.New()
76 rescaler.SetOutputMinimum(itk.NumericTraits[PixelType].min())
77 rescaler.SetOutputMaximum(itk.NumericTraits[PixelType].max())
78 rescaler.SetInput(indexSelectionFilter.GetOutput())
79 
80 WriterType = itk.ImageFileWriter[ImageType]
81 writer = WriterType.New()
82 writer.SetInput(rescaler.GetOutput())
83 
84 # Write the X and Y images
85 for i in range(2):
86  writer.SetFileName(filenames[i])
87  indexSelectionFilter.SetIndex(i)
88  writer.Update()
89 
90 # Compute the magnitude of the vector and output the image
91 MagnitudeType = itk.VectorMagnitudeImageFilter[
92  CovImageType, FloatImageType]
93 magnitudeFilter = MagnitudeType.New()
94 magnitudeFilter.SetInput(gradientFilter.GetOutput())
95 
96 # Rescale for png output
97 rescaler.SetInput(magnitudeFilter.GetOutput())
98 
99 writer.SetFileName(outputFileNameMagnitude)
100 writer.SetInput(rescaler.GetOutput())
101 writer.Update()