ITK  5.4.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 # 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 
20 if len(sys.argv) != 4:
21  print("Usage: " + sys.argv[0] + " <input_image> <output_image> <scale>")
22  sys.exit(1)
23 
24 input_file_name = sys.argv[1]
25 output_file_name = sys.argv[2]
26 scale = float(sys.argv[3])
27 
28 input_image = itk.imread(input_file_name)
29 input_size = itk.size(input_image)
30 input_spacing = itk.spacing(input_image)
31 input_origin = itk.origin(input_image)
32 Dimension = input_image.GetImageDimension()
33 
34 # We will scale the objects in the image by the factor `scale`; that is they
35 # will be shrunk (scale < 1.0) or enlarged (scale > 1.0). However, the number
36 # of pixels for each dimension of the output image will equal the corresponding
37 # number of pixels in the input image, with cropping or padding as necessary.
38 # Furthermore, the physical distance between adjacent pixels will be the same
39 # in the input and the output images. In contrast, if you want to change the
40 # resolution of the image without changing the represented physical size of the
41 # objects in the image, omit the transform and instead supply:
42 #
43 # output_size = [int(input_size[d] * scale) for d in range(Dimension)]
44 # output_spacing = [input_spacing[d] / scale for d in range(Dimension)]
45 # output_origin = [input_origin[d] + 0.5 * (output_spacing[d] - input_spacing[d])
46 # for d in range(Dimension)]
47 
48 output_size = input_size
49 output_spacing = input_spacing
50 output_origin = input_origin
51 scale_transform = itk.ScaleTransform[itk.D, Dimension].New()
52 scale_transform_parameters = scale_transform.GetParameters()
53 for i in range(len(scale_transform_parameters)):
54  scale_transform_parameters[i] = scale
55 scale_transform_center = [float(int(s / 2)) for s in input_size]
56 scale_transform.SetParameters(scale_transform_parameters)
57 scale_transform.SetCenter(scale_transform_center)
58 
59 interpolator = itk.LinearInterpolateImageFunction.New(input_image)
60 
61 resampled = itk.resample_image_filter(
62  input_image,
63  transform=scale_transform,
64  interpolator=interpolator,
65  size=output_size,
66  output_spacing=output_spacing,
67  output_origin=output_origin,
68 )
69 
70 itk.imwrite(resampled, output_file_name)
itk::ScaleTransform
Scale transformation of a vector space (e.g. space coordinates)
Definition: itkScaleTransform.h:45
itk::LinearInterpolateImageFunction::New
static Pointer New()
New
static Pointer New()