ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/ImageFunction/ResampleSegmentedImage/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 itk
18 import argparse
19 
20 parser = argparse.ArgumentParser(description="Resample Segmented Image.")
21 parser.add_argument("input_image")
22 parser.add_argument("spacing_fraction", type=float)
23 parser.add_argument("sigma_fraction", type=float)
24 parser.add_argument("output_image_file_label_image_interpolator")
25 parser.add_argument("output_image_file_nearest_neighbor_interpolator")
26 args = parser.parse_args()
27 
28 input_image = itk.imread(args.input_image)
29 
30 resize_filter = itk.ResampleImageFilter.New(input_image)
31 
32 input_spacing = itk.spacing(input_image)
33 output_spacing = [s * args.spacing_fraction for s in input_spacing]
34 resize_filter.SetOutputSpacing(output_spacing)
35 
36 input_size = itk.size(input_image)
37 output_size = [
38  int(s * input_spacing[dim] / args.spacing_fraction)
39  for dim, s in enumerate(input_size)
40 ]
41 resize_filter.SetSize(output_size)
42 
43 gaussian_interpolator = itk.LabelImageGaussianInterpolateImageFunction.New(input_image)
44 sigma = [s * args.sigma_fraction for s in output_spacing]
45 gaussian_interpolator.SetSigma(sigma)
46 gaussian_interpolator.SetAlpha(3.0)
47 resize_filter.SetInterpolator(gaussian_interpolator)
48 
49 itk.imwrite(resize_filter, args.output_image_file_label_image_interpolator)
50 
51 nearest_neighbor_interpolator = itk.NearestNeighborInterpolateImageFunction.New(
52  input_image
53 )
54 resize_filter.SetInterpolator(nearest_neighbor_interpolator)
55 
56 itk.imwrite(resize_filter, args.output_image_file_nearest_neighbor_interpolator)
itk::LabelImageGaussianInterpolateImageFunction::New
static Pointer New()
itk::NearestNeighborInterpolateImageFunction::New
static Pointer New()
itk::ResampleImageFilter::New
static Pointer New()