ITK  4.8.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussianWithVectorInput/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.8.0"):
26  print("ITK 4.8.0 is required (see example documentation).")
27  sys.exit(1)
28 
29 if len(sys.argv) != 6:
30  print(
31  "Usage: " + sys.argv[0] +
32  " [InputFileName] [OutputFileName1X] [OutputFileName1Y]" +
33  " [OutputFileName2X] [OutputFileName2Y]")
34  sys.exit(1)
35 
36 inputFileName = sys.argv[1]
37 outputFileName1X = sys.argv[2]
38 outputFileName1Y = sys.argv[3]
39 outputFileName2X = sys.argv[4]
40 outputFileName2Y = sys.argv[5]
41 
42 filenames = [
43  outputFileName1X,
44  outputFileName1Y,
45  outputFileName2X,
46  outputFileName2Y]
47 
48 ImageDimension = 2
49 VectorDimension = 2
50 CovDimension = 4
51 
52 PixelType = itk.UC
53 ImageType = itk.Image[PixelType, ImageDimension]
54 FloatPixelType = itk.F
55 FloatImageType = itk.Image[FloatPixelType, ImageDimension]
56 VecPixelType = itk.Vector[FloatPixelType, VectorDimension]
57 VecImageType = itk.Image[VecPixelType, ImageDimension]
58 CovPixelType = itk.CovariantVector[FloatPixelType, CovDimension]
59 CovImageType = itk.Image[CovPixelType, ImageDimension]
60 
61 ReaderType = itk.ImageFileReader[ImageType]
62 reader = ReaderType.New()
63 reader.SetFileName(inputFileName)
64 
65 # Invert the input image
66 InvertType = itk.InvertIntensityImageFilter[ImageType, ImageType]
67 inverter = InvertType.New()
68 inverter.SetInput(reader.GetOutput())
69 
70 # Cast the image to double type.
71 CasterType = itk.CastImageFilter[ImageType, FloatImageType]
72 caster = CasterType.New()
73 caster2 = CasterType.New()
74 
75 # Create an image, were each pixel has 2 values: first value is the value
76 # coming from the input image, second value is coming from the inverted
77 # image
78 ComposeType = itk.ComposeImageFilter[FloatImageType, VecImageType]
79 composer = ComposeType.New()
80 caster.SetInput(reader.GetOutput())
81 composer.SetInput(0, caster.GetOutput())
82 caster2.SetInput(inverter.GetOutput())
83 composer.SetInput(1, caster2.GetOutput())
84 
85 # Apply the gradient filter on the two images, this will return and image
86 # with 4 values per pixel: two X and Y gradients
88  VecImageType, CovImageType]
89 gradientfilter = FilterType.New()
90 gradientfilter.SetInput(composer.GetOutput())
91 
92 # Set up the filter to select each gradient
93 IndexSelectionType = itk.VectorIndexSelectionCastImageFilter[
94  CovImageType, FloatImageType]
95 indexSelectionFilter = IndexSelectionType.New()
96 indexSelectionFilter.SetInput(gradientfilter.GetOutput())
97 
98 # Rescale for png output
99 RescalerType = itk.RescaleIntensityImageFilter[FloatImageType, ImageType]
100 rescaler = RescalerType.New()
101 rescaler.SetOutputMinimum(itk.NumericTraits[PixelType].min())
102 rescaler.SetOutputMaximum(itk.NumericTraits[PixelType].max())
103 rescaler.SetInput(indexSelectionFilter.GetOutput())
104 
105 WriterType = itk.ImageFileWriter[ImageType]
106 writer = WriterType.New()
107 writer.SetInput(rescaler.GetOutput())
108 
109 for i in range(4):
110  indexSelectionFilter.SetIndex(i)
111  writer.SetFileName(filenames[i])
112  writer.Update()