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