Skip to content

Commit d7928a9

Browse files
tbirdsodzenanz
authored andcommitted
ENH: Add ComputeMeanSquareBetweenTwoImages Python example
1 parent d248671 commit d7928a9

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/Registration/Common/ComputeMeanSquareBetweenTwoImages/CMakeLists.txt

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

17-
install(FILES Code.cxx CMakeLists.txt
17+
install(FILES Code.cxx Code.py CMakeLists.txt
1818
DESTINATION share/ITKSphinxExamples/Code/Registration/Common/ComputeMeanSquareBetweenTwoImages/
1919
COMPONENT Code
2020
)
@@ -25,3 +25,11 @@ add_test(NAME ComputeMeanSquareBetweenTwoImagesTest
2525
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ComputeMeanSquareBetweenTwoImages
2626
Yinyang.png
2727
Gourds.png)
28+
29+
30+
if(ITK_WRAP_PYTHON)
31+
add_test(NAME ComputeMeanSquareBetweenTwoImagesTestPython
32+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
33+
Yinyang.png
34+
Gourds.png)
35+
endif()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 sys
22+
23+
import itk
24+
25+
if len(sys.argv) < 3:
26+
raise Exception(f"Usage: {sys.argv[0]} imageFile1 imageFile2")
27+
28+
fixed_image = itk.imread(sys.argv[1], itk.F)
29+
moving_image = itk.imread(sys.argv[2], itk.F)
30+
31+
metric = itk.MeanSquaresImageToImageMetric[type(fixed_image), type(moving_image)].New()
32+
transform = itk.TranslationTransform[itk.D, fixed_image.GetImageDimension()].New()
33+
interpolator = itk.LinearInterpolateImageFunction[type(fixed_image), itk.D].New()
34+
35+
metric.SetFixedImage(fixed_image)
36+
metric.SetMovingImage(moving_image)
37+
metric.SetFixedImageRegion(fixed_image.GetLargestPossibleRegion())
38+
metric.SetTransform(transform)
39+
metric.SetInterpolator(interpolator)
40+
metric.Initialize()
41+
42+
params = itk.OptimizerParameters[itk.D]()
43+
params.SetSize(2)
44+
45+
for x in range(-10, 15, 5):
46+
params.SetElement(0, x)
47+
for y in range(-10, 15, 5):
48+
params.SetElement(1, y)
49+
print(f"{list(params)}: {metric.GetValue(params):.1f}")

src/Registration/Common/ComputeMeanSquareBetweenTwoImages/Documentation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ C++
6464
.. literalinclude:: Code.cxx
6565
:lines: 18-
6666

67+
Python
68+
...
69+
70+
.. literalinclude:: Code.py
71+
:lines: 21-
72+
6773
Classes demonstrated
6874
--------------------
6975

0 commit comments

Comments
 (0)