ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussianWithVectorInput/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
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()
itk::CastImageFilter
Casts input pixels to output pixel type.
Definition: itkCastImageFilter.h:104
itk::InvertIntensityImageFilter
Invert the intensity of an image.
Definition: itkInvertIntensityImageFilter.h:97
itk::Version::GetITKVersion
static const char * GetITKVersion()
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::VectorIndexSelectionCastImageFilter
Extracts the selected index of the vector that is the input pixel type.
Definition: itkVectorIndexSelectionCastImageFilter.h:96
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itk::GradientRecursiveGaussianImageFilter
Computes the gradient of an image by convolution with the first derivative of a Gaussian.
Definition: itkGradientRecursiveGaussianImageFilter.h:59
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::ComposeImageFilter
ComposeImageFilter combine several scalar images into a multicomponent image.
Definition: itkComposeImageFilter.h:62
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86