Skip to content

Commit e7bfc2b

Browse files
committed
ENH: Adding python sample for ConvolveImageWithKernel
1 parent 6f0e16b commit e7bfc2b

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ install(TARGETS ConvolveImageWithKernel
3535
COMPONENT Runtime
3636
)
3737

38-
install(FILES Code.cxx CMakeLists.txt
38+
install(FILES Code.cxx Code.py CMakeLists.txt
3939
DESTINATION share/ITKSphinxExamples/Code/Filtering/Convolution/ConvolveImageWithKernel/
4040
COMPONENT Code
4141
)
@@ -46,3 +46,10 @@ add_test(NAME ConvolveImageWithKernelTest
4646
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ConvolveImageWithKernel
4747
Yinyang.png
4848
10)
49+
50+
if(ITK_WRAP_PYTHON)
51+
add_test(NAME ConvolveImageWithKernelTestPython
52+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
53+
${CMAKE_CURRENT_BINARY_DIR}/Yinyang.png
54+
10)
55+
endif()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
31+
# Visualize the result using matplotlib
32+
import matplotlib.pyplot as plt
33+
34+
plot_image = np.concatenate((inputImage, filteredImage), axis=1)
35+
plt.imshow(plot_image)
36+
plt.title("Visualize using Matplotlib, Kernel width = " + str(width))
37+
plt.show()

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+
082d1d49439879f93946e39675c62a6732fbb1d17aeb7ecbc63c6ca7e350b34416d3764c355e4c1c00583fd8231eb79f01cc6c618d5cda7eee8a267df64db7a3

0 commit comments

Comments
 (0)