ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGrid/ResampleAVectorImage/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.7.0"):
22  print("ITK 4.7.0 is required (see example documentation).")
23  sys.exit(1)
24 
25 if len(sys.argv) != 3:
26  print("Usage: " + sys.argv[0] + " [inputImage] [outputImage]")
27  sys.exit(1)
28 
29 inputImage = sys.argv[1]
30 outputImage = sys.argv[2]
31 
32 Dimension = 2
33 
34 PixelType = itk.RGBPixel[itk.UC]
35 ImageType = itk.Image[PixelType, Dimension]
36 
37 ReaderType = itk.ImageFileReader[ImageType]
38 reader = ReaderType.New()
39 reader.SetFileName(inputImage)
40 
41 filterType = itk.ResampleImageFilter[ImageType, ImageType]
42 resampleImageFilter = filterType.New()
43 
44 interpolatorType = itk.LinearInterpolateImageFunction[ImageType, itk.D]
45 interpolator = interpolatorType.New()
46 
47 resampleImageFilter.SetInterpolator(interpolator)
48 
49 transformType = itk.IdentityTransform[itk.D, Dimension]
50 transform = transformType.New()
51 
52 resampleImageFilter.SetTransform(transform)
53 
54 resampleImageFilter.SetDefaultPixelValue([50, 50, 50])
55 resampleImageFilter.SetOutputSpacing([0.5, 0.5])
56 resampleImageFilter.SetOutputOrigin([30, 40])
57 
58 resampleImageFilter.SetSize([300, 300])
59 resampleImageFilter.SetInput(reader.GetOutput())
60 
61 WriterType = itk.ImageFileWriter[ImageType]
62 writer = WriterType.New()
63 writer.SetFileName(outputImage)
64 writer.SetInput(resampleImageFilter.GetOutput())
65 writer.Update()
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::Version::GetITKVersion
static const char * GetITKVersion()
itk::IdentityTransform
Implementation of an Identity Transform.
Definition: itkIdentityTransform.h:50
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::LinearInterpolateImageFunction
Linearly interpolate an image at specified positions.
Definition: itkLinearInterpolateImageFunction.h:50
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itk::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: itkResampleImageFilter.h:90
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86