Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMake/ITKSphinxExamplesMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ endmacro()
#
# [OPTIONS myListOfOptions] // Options to pass to ImageCompareCommand.
#
# [NO_PYTHON_COMPARISON] // Skip the auto-added Python baseline comparison
# // when the Python output is not pixel-comparable
# // to the C++ baseline (e.g. a Matplotlib figure).
#
# [DEPENDS myListOfDependencies] // Other tests that the comparison test will
# // depend on. By default, the dependency is assumed
# // to be ${EXAMPLE_NAME}Test. For Python, the comparison
Expand All @@ -110,6 +114,7 @@ endmacro()
function(compare_to_baseline)
set(options
PYTHON_ONLY
NO_PYTHON_COMPARISON
)

set(oneValueArgs
Expand Down Expand Up @@ -174,7 +179,7 @@ function(compare_to_baseline)
)
endif()

if(ITK_WRAP_PYTHON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/Code.py")
if(ITK_WRAP_PYTHON AND NOT LOCAL_COMPARISON_NO_PYTHON_COMPARISON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/Code.py")
set(python_test_name ${test_name}Python)
get_filename_component(test_image_we "${test_image}" NAME_WE)
get_filename_component(test_image_dir "${test_image}" DIRECTORY)
Expand Down
3 changes: 3 additions & 0 deletions src/Filtering/Convolution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ compare_to_baseline(EXAMPLE_NAME NormalizedCorrelationUsingFFTWithMaskImages
add_example(ConvolveImageWithKernel)
compare_to_baseline(EXAMPLE_NAME ConvolveImageWithKernel
BASELINE_PREFIX ConvolveImageWithKernel
# The Python example emits a raw convolved image, not the C++ QuickView
# screenshot, so the two are not pixel-comparable.
NO_PYTHON_COMPARISON
)

# TODO: Fix building of ColorNormalizedCorrelation example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ install(TARGETS ${PROJECT_NAME}
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 @@ -57,3 +57,10 @@ add_test(NAME ConvolveImageWithKernelTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}
Yinyang.png
10)

if(ITK_WRAP_PYTHON)
add_test(NAME ConvolveImageWithKernelTestPython
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${CMAKE_CURRENT_BINARY_DIR}/Yinyang.png
10)
endif()
47 changes: 47 additions & 0 deletions src/Filtering/Convolution/ConvolveImageWithKernel/Code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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)

# PNG requires an integer pixel type, so rescale the float result to 8-bit.
rescaledImage = itk.rescale_intensity_image_filter(
filteredImage, output_minimum=0, output_maximum=255
)
outputImage = rescaledImage.astype(itk.UC)

# Write the output Image
itk.imwrite(outputImage, "ConvolveImageWithKernelPython.png")

# 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 @@
bafkreiejmu6wjzs35rezwhe4htcehxslunbdju424td6zjy3giucxgczvu
Loading