ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGrid/ResampleAnImage/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 if len(sys.argv) != 4:
21  print("Usage: " + sys.argv[0] + " <inputImage> <outputImage> <scale>")
22  sys.exit(1)
23 
24 inputImage = sys.argv[1]
25 outputImage = sys.argv[2]
26 scale = float(sys.argv[3])
27 
28 PixelType = itk.UC
29 ScalarType = itk.D
30 Dimension = 2
31 
32 ImageType = itk.Image[PixelType, Dimension]
33 
34 ReaderType = itk.ImageFileReader[ImageType]
35 reader = ReaderType.New()
36 reader.SetFileName(inputImage)
37 reader.Update()
38 
39 inputImage = reader.GetOutput()
40 
41 size = inputImage.GetLargestPossibleRegion().GetSize()
42 spacing = inputImage.GetSpacing()
43 
44 centralPixel = itk.Index[Dimension]()
45 centralPixel[0] = int(size[0] / 2)
46 centralPixel[1] = int(size[1] / 2)
47 centralPoint = itk.Point[ScalarType, Dimension]()
48 centralPoint[0] = centralPixel[0]
49 centralPoint[1] = centralPixel[1]
50 
51 scaleTransform = itk.ScaleTransform[ScalarType, Dimension].New()
52 
53 parameters = scaleTransform.GetParameters()
54 parameters[0] = scale
55 parameters[1] = scale
56 
57 scaleTransform.SetParameters(parameters)
58 scaleTransform.SetCenter(centralPoint)
59 
60 interpolatorType = itk.LinearInterpolateImageFunction[ImageType, ScalarType]
61 interpolator = interpolatorType.New()
62 
63 resamplerType = itk.ResampleImageFilter[ImageType, ImageType]
64 resampleFilter = resamplerType.New()
65 
66 resampleFilter.SetInput(inputImage)
67 resampleFilter.SetTransform(scaleTransform)
68 resampleFilter.SetInterpolator(interpolator)
69 resampleFilter.SetSize(size)
70 resampleFilter.SetOutputSpacing(spacing)
71 
72 WriterType = itk.ImageFileWriter[ImageType]
73 writer = WriterType.New()
74 writer.SetFileName(outputImage)
75 writer.SetInput(resampleFilter.GetOutput())
76 
77 writer.Update()
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::ScaleTransform
Scale transformation of a vector space (e.g. space coordinates)
Definition: itkScaleTransform.h:41
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::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86