Skip to content

Commit 56b3ce0

Browse files
authored
Merge pull request #453 from InsightSoftwareConsortium/rebase/convolve-image-with-kernel-python
ENH: Add Python example for ConvolveImageWithKernel
2 parents 3942f8d + e8817df commit 56b3ce0

6 files changed

Lines changed: 70 additions & 2 deletions

File tree

CMake/ITKSphinxExamplesMacros.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ endmacro()
100100
#
101101
# [OPTIONS myListOfOptions] // Options to pass to ImageCompareCommand.
102102
#
103+
# [NO_PYTHON_COMPARISON] // Skip the auto-added Python baseline comparison
104+
# // when the Python output is not pixel-comparable
105+
# // to the C++ baseline (e.g. a Matplotlib figure).
106+
#
103107
# [DEPENDS myListOfDependencies] // Other tests that the comparison test will
104108
# // depend on. By default, the dependency is assumed
105109
# // to be ${EXAMPLE_NAME}Test. For Python, the comparison
@@ -110,6 +114,7 @@ endmacro()
110114
function(compare_to_baseline)
111115
set(options
112116
PYTHON_ONLY
117+
NO_PYTHON_COMPARISON
113118
)
114119

115120
set(oneValueArgs
@@ -174,7 +179,7 @@ function(compare_to_baseline)
174179
)
175180
endif()
176181

177-
if(ITK_WRAP_PYTHON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/Code.py")
182+
if(ITK_WRAP_PYTHON AND NOT LOCAL_COMPARISON_NO_PYTHON_COMPARISON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/Code.py")
178183
set(python_test_name ${test_name}Python)
179184
get_filename_component(test_image_we "${test_image}" NAME_WE)
180185
get_filename_component(test_image_dir "${test_image}" DIRECTORY)

src/Filtering/Convolution/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ compare_to_baseline(EXAMPLE_NAME NormalizedCorrelationUsingFFTWithMaskImages
1616
add_example(ConvolveImageWithKernel)
1717
compare_to_baseline(EXAMPLE_NAME ConvolveImageWithKernel
1818
BASELINE_PREFIX ConvolveImageWithKernel
19+
# The Python example emits a raw convolved image, not the C++ QuickView
20+
# screenshot, so the two are not pixel-comparable.
21+
NO_PYTHON_COMPARISON
1922
)
2023

2124
# TODO: Fix building of ColorNormalizedCorrelation example

src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ install(TARGETS ${PROJECT_NAME}
4646
COMPONENT Runtime
4747
)
4848

49-
install(FILES Code.cxx CMakeLists.txt
49+
install(FILES Code.cxx Code.py CMakeLists.txt
5050
DESTINATION share/ITKSphinxExamples/Code/Filtering/Convolution/ConvolveImageWithKernel/
5151
COMPONENT Code
5252
)
@@ -57,3 +57,10 @@ add_test(NAME ConvolveImageWithKernelTest
5757
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}
5858
Yinyang.png
5959
10)
60+
61+
if(ITK_WRAP_PYTHON)
62+
add_test(NAME ConvolveImageWithKernelTestPython
63+
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
64+
${CMAKE_CURRENT_BINARY_DIR}/Yinyang.png
65+
10)
66+
endif()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import itk
2+
import numpy as np
3+
import argparse
4+
5+
parser = argparse.ArgumentParser(description="Convolve Image With Kernel")
6+
parser.add_argument("input_image")
7+
parser.add_argument("width", type=int)
8+
args = parser.parse_args()
9+
10+
PixelType = itk.F
11+
12+
# Read Image in float data type
13+
inputImage = itk.imread(args.input_image, PixelType)
14+
15+
width = 10
16+
if args.width:
17+
width = args.width
18+
19+
20+
def CreateSmoothKernel(width):
21+
return np.ones([width, width], "float32") / (width * width)
22+
23+
24+
kernel = CreateSmoothKernel(width)
25+
kernel_image = itk.image_from_array(kernel)
26+
27+
# Convolve image with kernel.
28+
filteredImage = itk.convolution_image_filter(inputImage, kernel_image=kernel_image)
29+
30+
# PNG requires an integer pixel type, so rescale the float result to 8-bit.
31+
rescaledImage = itk.rescale_intensity_image_filter(
32+
filteredImage, output_minimum=0, output_maximum=255
33+
)
34+
outputImage = rescaledImage.astype(itk.UC)
35+
36+
# Write the output Image
37+
itk.imwrite(outputImage, "ConvolveImageWithKernelPython.png")
38+
39+
# Visualize the result using matplotlib
40+
import matplotlib.pyplot as plt
41+
42+
plot_image = np.concatenate((inputImage, filteredImage), axis=1)
43+
plt.imshow(plot_image)
44+
plt.title("Visualize using Matplotlib, Kernel width = " + str(width))
45+
plt.show(block=False)
46+
plt.pause(3)
47+
plt.close()

src/Filtering/Convolution/ConvolveImageWithKernel/Documentation.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Results
2626

2727
Output In VTK Window
2828

29+
.. figure:: MatplotlibVisualizeConvolution.png
30+
:scale: 70%
31+
32+
Output In Matplotlib
33+
2934
Code
3035
----
3136

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bafkreiejmu6wjzs35rezwhe4htcehxslunbdju424td6zjy3giucxgczvu

0 commit comments

Comments
 (0)