ITK  5.0.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussian/Code.py
1 #!/usr/bin/env python
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 import sys
18 import itk
19 
20 from distutils.version import StrictVersion as VS
21 if VS(itk.Version.GetITKVersion()) < VS("4.7.0"):
22  print("ITK 4.7.0 is required (see example documentation).")
23  sys.exit(1)
24 
25 if len(sys.argv) != 5:
26  print("Usage: " + sys.argv[0] +
27  " [InputFileName] [OutputFileNameX] [OutputFileNameY]" +
28  " [OutputFileNameMagnitude]")
29  sys.exit(1)
30 
31 inputFileName = sys.argv[1]
32 outputFileNameX = sys.argv[2]
33 outputFileNameY = sys.argv[3]
34 outputFileNameMagnitude = sys.argv[4]
35 
36 Dimension = 2
37 
38 filenames = []
39 filenames.append(outputFileNameX)
40 filenames.append(outputFileNameY)
41 
42 # Input and output are png files, use unsigned char
43 PixelType = itk.UC
44 ImageType = itk.Image[PixelType, Dimension]
45 # Float type for GradientRecursiveGaussianImageFilter
46 FloatPixelType = itk.F
47 FloatImageType = itk.Image[FloatPixelType, Dimension]
48 # The output of GradientRecursiveGaussianImageFilter
49 # are images of the gradient along X and Y, so the type of
50 # the output is a covariant vector of dimension 2 (X, Y)
51 CovPixelType = itk.CovariantVector[FloatPixelType, Dimension]
52 CovImageType = itk.Image[CovPixelType, Dimension]
53 
54 ReaderType = itk.ImageFileReader[ImageType]
55 reader = ReaderType.New()
56 reader.SetFileName(inputFileName)
57 
58 FilterType = itk.GradientRecursiveGaussianImageFilter[ImageType, CovImageType]
59 gradientFilter = FilterType.New()
60 gradientFilter.SetInput(reader.GetOutput())
61 
62 # Allows to select the X or Y output images
63 IndexSelectionType = itk.VectorIndexSelectionCastImageFilter[
64  CovImageType, FloatImageType]
65 indexSelectionFilter = IndexSelectionType.New()
66 indexSelectionFilter.SetInput(gradientFilter.GetOutput())
67 
68 # Rescale for png output
69 RescalerType = itk.RescaleIntensityImageFilter[FloatImageType, ImageType]
70 rescaler = RescalerType.New()
71 rescaler.SetOutputMinimum(itk.NumericTraits[PixelType].min())
72 rescaler.SetOutputMaximum(itk.NumericTraits[PixelType].max())
73 rescaler.SetInput(indexSelectionFilter.GetOutput())
74 
75 WriterType = itk.ImageFileWriter[ImageType]
76 writer = WriterType.New()
77 writer.SetInput(rescaler.GetOutput())
78 
79 # Write the X and Y images
80 for i in range(2):
81  writer.SetFileName(filenames[i])
82  indexSelectionFilter.SetIndex(i)
83  writer.Update()
84 
85 # Compute the magnitude of the vector and output the image
86 MagnitudeType = itk.VectorMagnitudeImageFilter[
87  CovImageType, FloatImageType]
88 magnitudeFilter = MagnitudeType.New()
89 magnitudeFilter.SetInput(gradientFilter.GetOutput())
90 
91 # Rescale for png output
92 rescaler.SetInput(magnitudeFilter.GetOutput())
93 
94 writer.SetFileName(outputFileNameMagnitude)
95 writer.SetInput(rescaler.GetOutput())
96 writer.Update()