diff --git a/src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt b/src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt index 7e2bfbae7..155660f61 100644 --- a/src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt +++ b/src/Filtering/Convolution/ConvolveImageWithKernel/CMakeLists.txt @@ -35,7 +35,7 @@ install(TARGETS ConvolveImageWithKernel COMPONENT Runtime ) -install(FILES Code.cxx CMakeLists.txt +install(FILES Code.cxx Code.py CMakeLists.txt DESTINATION share/ITKSphinxExamples/Code/Filtering/Convolution/ConvolveImageWithKernel/ COMPONENT Code ) @@ -46,3 +46,10 @@ add_test(NAME ConvolveImageWithKernelTest COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ConvolveImageWithKernel Yinyang.png 10) + +if(ITK_WRAP_PYTHON) + add_test(NAME ConvolveImageWithKernelTestPython + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py + ${CMAKE_CURRENT_BINARY_DIR}/Yinyang.png + 10) +endif() diff --git a/src/Filtering/Convolution/ConvolveImageWithKernel/Code.py b/src/Filtering/Convolution/ConvolveImageWithKernel/Code.py new file mode 100644 index 000000000..f9d8666a2 --- /dev/null +++ b/src/Filtering/Convolution/ConvolveImageWithKernel/Code.py @@ -0,0 +1,41 @@ +import itk +import numpy as np +import argparse + +parser = argparse.ArgumentParser(description="Convolve Image With Kernel") +parser.add_argument("input_image") +parser.add_argument("width", type=int) +args = parser.parse_args() + +PixelType = itk.F + +# Read Image in float data type +inputImage = itk.imread(args.input_image, PixelType) + +width = 10 +if args.width: + width = args.width + + +def CreateSmoothKernel(width): + return np.ones([width, width], "float32") / (width * width) + + +kernel = CreateSmoothKernel(width) +kernel_image = itk.image_from_array(kernel) + +# Convolve image with kernel. +filteredImage = itk.convolution_image_filter(inputImage, kernel_image=kernel_image) + +# Write the output Image +itk.imwrite("ConvolveImageWithKernelPython.png", filteredImage) + +# Visualize the result using matplotlib +import matplotlib.pyplot as plt + +plot_image = np.concatenate((inputImage, filteredImage), axis=1) +plt.imshow(plot_image) +plt.title("Visualize using Matplotlib, Kernel width = " + str(width)) +plt.show(block=False) +plt.pause(3) +plt.close() diff --git a/src/Filtering/Convolution/ConvolveImageWithKernel/Documentation.rst b/src/Filtering/Convolution/ConvolveImageWithKernel/Documentation.rst index bbc825b99..10f7123ff 100644 --- a/src/Filtering/Convolution/ConvolveImageWithKernel/Documentation.rst +++ b/src/Filtering/Convolution/ConvolveImageWithKernel/Documentation.rst @@ -26,6 +26,11 @@ Results Output In VTK Window +.. figure:: MatplotlibVisualizeConvolution.png + :scale: 70% + + Output In Matplotlib + Code ---- diff --git a/src/Filtering/Convolution/ConvolveImageWithKernel/MatplotlibVisualizeConvolution.png.sha512 b/src/Filtering/Convolution/ConvolveImageWithKernel/MatplotlibVisualizeConvolution.png.sha512 new file mode 100644 index 000000000..eafd978ed --- /dev/null +++ b/src/Filtering/Convolution/ConvolveImageWithKernel/MatplotlibVisualizeConvolution.png.sha512 @@ -0,0 +1 @@ +082d1d49439879f93946e39675c62a6732fbb1d17aeb7ecbc63c6ca7e350b34416d3764c355e4c1c00583fd8231eb79f01cc6c618d5cda7eee8a267df64db7a3