ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussian/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.7.0"):
24  print("ITK 4.7.0 is required (see example documentation).")
25  sys.exit(1)
26 
27 parser = argparse.ArgumentParser(description="Apply Gradient Recursive Gaussian.")
28 parser.add_argument("input_image")
29 parser.add_argument("output_image_x")
30 parser.add_argument("output_image_y")
31 parser.add_argument("output_image_magnitude")
32 args = parser.parse_args()
33 
34 Dimension = 2
35 
36 filenames = []
37 filenames.append(args.output_image_x)
38 filenames.append(args.output_image_y)
39 
40 # Input and output are png files, use unsigned char
41 PixelType = itk.UC
42 ImageType = itk.Image[PixelType, Dimension]
43 # Float type for GradientRecursiveGaussianImageFilter
44 FloatPixelType = itk.F
45 FloatImageType = itk.Image[FloatPixelType, Dimension]
46 # The output of GradientRecursiveGaussianImageFilter
47 # are images of the gradient along X and Y, so the type of
48 # the output is a covariant vector of dimension 2 (X, Y)
49 CovPixelType = itk.CovariantVector[FloatPixelType, Dimension]
50 CovImageType = itk.Image[CovPixelType, Dimension]
51 
52 ReaderType = itk.ImageFileReader[ImageType]
53 reader = ReaderType.New()
54 reader.SetFileName(args.input_image)
55 
56 FilterType = itk.GradientRecursiveGaussianImageFilter[ImageType, CovImageType]
57 gradientFilter = FilterType.New()
58 gradientFilter.SetInput(reader.GetOutput())
59 
60 # Allows to select the X or Y output images
61 IndexSelectionType = itk.VectorIndexSelectionCastImageFilter[
62  CovImageType, FloatImageType
63 ]
64 indexSelectionFilter = IndexSelectionType.New()
65 indexSelectionFilter.SetInput(gradientFilter.GetOutput())
66 
67 # Rescale for png output
68 RescalerType = itk.RescaleIntensityImageFilter[FloatImageType, ImageType]
69 rescaler = RescalerType.New()
70 rescaler.SetOutputMinimum(itk.NumericTraits[PixelType].min())
71 rescaler.SetOutputMaximum(itk.NumericTraits[PixelType].max())
72 rescaler.SetInput(indexSelectionFilter.GetOutput())
73 
74 WriterType = itk.ImageFileWriter[ImageType]
75 writer = WriterType.New()
76 writer.SetInput(rescaler.GetOutput())
77 
78 # Write the X and Y images
79 for i in range(2):
80  writer.SetFileName(filenames[i])
81  indexSelectionFilter.SetIndex(i)
82  writer.Update()
83 
84 # Compute the magnitude of the vector and output the image
85 MagnitudeType = itk.VectorMagnitudeImageFilter[CovImageType, FloatImageType]
86 magnitudeFilter = MagnitudeType.New()
87 magnitudeFilter.SetInput(gradientFilter.GetOutput())
88 
89 # Rescale for png output
90 rescaler.SetInput(magnitudeFilter.GetOutput())
91 
92 writer.SetFileName(args.output_image_magnitude)
93 writer.SetInput(rescaler.GetOutput())
94 writer.Update()
itk::VectorMagnitudeImageFilter
Take an image of vectors as input and produce an image with the magnitude of those vectors.
Definition: itkVectorMagnitudeImageFilter.h:68
itk::Version::GetITKVersion
static const char * GetITKVersion()
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::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