ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/Transform/DeformAVolumeWithAThinPlateSpline/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 argparse
18 import itk
19 import numpy as np
20 
21 parser = argparse.ArgumentParser(
22  description="Deform a Volume With a Thin Plate Spline."
23 )
24 parser.add_argument("source_landmarks")
25 parser.add_argument("target_landmarks")
26 parser.add_argument("input_image")
27 parser.add_argument("deformed_image")
28 parser.add_argument("checker_board_image")
29 args = parser.parse_args()
30 
31 Dimension = 3
32 thin_plate_spline = itk.ThinPlateSplineKernelTransform[itk.D, Dimension].New()
33 
34 source_landmarks_mesh = itk.meshread(args.source_landmarks)
35 # Cast points from float32 to float64
36 points = itk.array_from_vector_container(source_landmarks_mesh.GetPoints())
37 points = points.astype(np.float64)
38 source_landmarks = thin_plate_spline.GetSourceLandmarks()
39 source_landmarks.SetPoints(itk.vector_container_from_array(points.flatten()))
40 
41 target_landmarks_mesh = itk.meshread(args.target_landmarks)
42 # Cast points from float32 to float64
43 points = itk.array_from_vector_container(target_landmarks_mesh.GetPoints())
44 points = points.astype(np.float64)
45 target_landmarks = thin_plate_spline.GetTargetLandmarks()
46 target_landmarks.SetPoints(itk.vector_container_from_array(points.flatten()))
47 
48 thin_plate_spline.ComputeWMatrix()
49 
50 input_image = itk.imread(args.input_image)
51 
52 deformed_image = itk.resample_image_filter(
53  input_image,
54  use_reference_image=True,
55  reference_image=input_image,
56  transform=thin_plate_spline,
57 )
58 itk.imwrite(deformed_image, args.deformed_image)
59 
60 checker_board = itk.checker_board_image_filter(input_image, deformed_image)
61 itk.imwrite(checker_board, args.checker_board_image)
itk::ThinPlateSplineKernelTransform
Definition: itkThinPlateSplineKernelTransform.h:35
New
static Pointer New()