Skip to content

Commit

Permalink
ENH: Adding python sample for ConvolveImageWithKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
PranjalSahu committed Jun 6, 2022
1 parent 18a99c7 commit 45adbcb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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()
41 changes: 41 additions & 0 deletions src/Filtering/Convolution/ConvolveImageWithKernel/Code.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Results

Output In VTK Window

.. figure:: MatplotlibVisualizeConvolution.png
:scale: 70%

Output In Matplotlib

Code
----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
082d1d49439879f93946e39675c62a6732fbb1d17aeb7ecbc63c6ca7e350b34416d3764c355e4c1c00583fd8231eb79f01cc6c618d5cda7eee8a267df64db7a3

0 comments on commit 45adbcb

Please sign in to comment.