ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Registration/Common/RegisterImageToAnotherUsingLandmarks/Code.py
1 #!/usr/bin/env python3
2 
3 # =========================================================================
4 #
5 # Copyright NumFOCUS
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # =========================================================================*/
20 
21 import itk
22 
23 Dimension = 2
24 PixelType = itk.ctype("unsigned char")
25 ImageType = itk.Image[PixelType, Dimension]
26 
27 
28 def create_fixed_image():
29  start = [
30  0,
31  ] * Dimension
32  size = [
33  100,
34  ] * Dimension
35 
36  region = itk.ImageRegion[Dimension](start, size)
37 
38  ImageType = itk.Image[PixelType, Dimension]
39  image = ImageType.New()
40  image.SetRegions(region)
41  image.Allocate()
42  image.FillBuffer(0)
43 
44  image[11:20, 11:20] = 255
45 
46  itk.imwrite(image, "fixedPython.png")
47 
48  return image
49 
50 
51 def create_moving_image():
52  start = [
53  0,
54  ] * Dimension
55  size = [
56  100,
57  ] * Dimension
58 
59  region = itk.ImageRegion[Dimension](start, size)
60 
61  ImageType = itk.Image[PixelType, Dimension]
62  image = ImageType.New()
63  image.SetRegions(region)
64  image.Allocate()
65  image.FillBuffer(0)
66 
67  image[51:60, 51:60] = 100
68 
69  itk.imwrite(image, "movingPython.png")
70 
71  return image
72 
73 
74 fixed_image = create_fixed_image()
75 moving_image = create_moving_image()
76 
77 LandmarkPointType = itk.Point[itk.D, Dimension]
78 LandmarkContainerType = itk.vector[LandmarkPointType]
79 
80 fixed_landmarks = LandmarkContainerType()
81 moving_landmarks = LandmarkContainerType()
82 
83 fixed_point = LandmarkPointType()
84 moving_point = LandmarkPointType()
85 
86 fixed_point[0] = 10
87 fixed_point[1] = 10
88 moving_point[0] = 50
89 moving_point[1] = 50
90 fixed_landmarks.push_back(fixed_point)
91 moving_landmarks.push_back(moving_point)
92 
93 fixed_point[0] = 20
94 fixed_point[1] = 10
95 moving_point[0] = 60
96 moving_point[1] = 50
97 fixed_landmarks.push_back(fixed_point)
98 moving_landmarks.push_back(moving_point)
99 
100 fixed_point[0] = 20
101 fixed_point[1] = 20
102 moving_point[0] = 60
103 moving_point[1] = 60
104 fixed_landmarks.push_back(fixed_point)
105 moving_landmarks.push_back(moving_point)
106 
107 TransformInitializerType = itk.LandmarkBasedTransformInitializer[
108  itk.Transform[itk.D, Dimension, Dimension]
109 ]
110 transform_initializer = TransformInitializerType.New()
111 
112 transform_initializer.SetFixedLandmarks(fixed_landmarks)
113 transform_initializer.SetMovingLandmarks(moving_landmarks)
114 
115 transform = itk.Rigid2DTransform[itk.D].New()
116 transform_initializer.SetTransform(transform)
117 transform_initializer.InitializeTransform()
118 
119 output = itk.resample_image_filter(
120  moving_image,
121  transform=transform,
122  use_reference_image=True,
123  reference_image=fixed_image,
124  default_pixel_value=200,
125 )
126 
127 itk.imwrite(output, "outputPython.png")
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::Rigid2DTransform
Rigid2DTransform of a vector space (e.g. space coordinates)
Definition: itkRigid2DTransform.h:56
itk::LandmarkBasedTransformInitializer
Definition: itkLandmarkBasedTransformInitializer.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:88
itk::Transform
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:83
New
static Pointer New()