ITK  5.4.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 # https://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 import argparse
20 
21 from distutils.version import StrictVersion as VS
22 
23 if VS(itk.Version.GetITKVersion()) < VS("4.8.0"):
24  print("ITK 4.8.0 is required (see example documentation).")
25  sys.exit(1)
26 
27 parser = argparse.ArgumentParser(
28  description="Apply Gradient Recursive Gaussian With Vector Input."
29 )
30 parser.add_argument("input_image")
31 parser.add_argument("output_image_1x")
32 parser.add_argument("output_image_1y")
33 parser.add_argument("output_image_2x")
34 parser.add_argument("output_image_2y")
35 args = parser.parse_args()
36 
37 filenames = [
38  args.output_image_1x,
39  args.output_image_1y,
40  args.output_image_2x,
41  args.output_image_2y,
42 ]
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(args.input_image)
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
83 FilterType = itk.GradientRecursiveGaussianImageFilter[VecImageType, CovImageType]
84 gradientfilter = FilterType.New()
85 gradientfilter.SetInput(composer.GetOutput())
86 
87 # Set up the filter to select each gradient
88 IndexSelectionType = itk.VectorIndexSelectionCastImageFilter[
89  CovImageType, FloatImageType
90 ]
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:100
itk::InvertIntensityImageFilter
Invert the intensity of an image.
Definition: itkInvertIntensityImageFilter.h:90
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:88
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
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:59
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:133
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88