Skip to content

Commit 035c21f

Browse files
authored
Merge pull request #304 from thewtex/landmark-python
2 parents 9ab3696 + 5f34106 commit 035c21f

File tree

4 files changed

+146
-9
lines changed

4 files changed

+146
-9
lines changed

src/Registration/Common/RegisterImageToAnotherUsingLandmarks/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ install(TARGETS RegisterImageToAnotherUsingLandmarks
1414
COMPONENT Runtime
1515
)
1616

17-
install(FILES Code.cxx CMakeLists.txt
17+
install(FILES Code.cxx CMakeLists.txt Code.py
1818
DESTINATION share/ITKSphinxExamples/Code/Registration/Common/RegisterImageToAnotherUsingLandmarks/
1919
COMPONENT Code
2020
)
2121

2222
enable_testing()
2323
add_test(NAME RegisterImageToAnotherUsingLandmarksTest
2424
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RegisterImageToAnotherUsingLandmarks)
25+
26+
if(ITK_WRAP_PYTHON)
27+
add_test(NAME RegisterImageToAnotherUsingLandmarksTestPython
28+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
29+
)
30+
endif()

src/Registration/Common/RegisterImageToAnotherUsingLandmarks/Code.cxx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ main(int itkNotUsed(argc), char * itkNotUsed(argv)[])
4040
ImageType::Pointer movingImage = ImageType::New();
4141
CreateMovingImage(movingImage);
4242

43-
using Rigid2DTransformType = itk::Rigid2DTransform<double>;
43+
using TransformType = itk::Rigid2DTransform<double>;
4444
using LandmarkBasedTransformInitializerType =
45-
itk::LandmarkBasedTransformInitializer<Rigid2DTransformType, ImageType, ImageType>;
45+
itk::LandmarkBasedTransformInitializer<TransformType, ImageType, ImageType>;
4646

4747
LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer =
4848
LandmarkBasedTransformInitializerType::New();
@@ -87,7 +87,7 @@ main(int itkNotUsed(argc), char * itkNotUsed(argv)[])
8787
landmarkBasedTransformInitializer->SetFixedLandmarks(fixedLandmarks);
8888
landmarkBasedTransformInitializer->SetMovingLandmarks(movingLandmarks);
8989

90-
Rigid2DTransformType::Pointer transform = Rigid2DTransformType::New();
90+
TransformType::Pointer transform = TransformType::New();
9191

9292
transform->SetIdentity();
9393
landmarkBasedTransformInitializer->SetTransform(transform);
@@ -97,12 +97,9 @@ main(int itkNotUsed(argc), char * itkNotUsed(argv)[])
9797
ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();
9898
resampleFilter->SetInput(movingImage);
9999
resampleFilter->SetTransform(transform);
100-
resampleFilter->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
101-
resampleFilter->SetOutputOrigin(fixedImage->GetOrigin());
102-
resampleFilter->SetOutputSpacing(fixedImage->GetSpacing());
103-
resampleFilter->SetOutputDirection(fixedImage->GetDirection());
100+
resampleFilter->SetUseReferenceImage(true);
101+
resampleFilter->SetReferenceImage(fixedImage);
104102
resampleFilter->SetDefaultPixelValue(200);
105-
resampleFilter->GetOutput();
106103

107104
// Write the output
108105
using WriterType = itk::ImageFileWriter<ImageType>;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
# http://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")

src/Registration/Common/RegisterImageToAnotherUsingLandmarks/Documentation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ Results
3636
Code
3737
----
3838

39+
Python
40+
......
41+
42+
.. literalinclude:: Code.py
43+
:language: python
44+
:lines: 1, 20-
45+
3946
C++
4047
...
4148

0 commit comments

Comments
 (0)