Skip to content

Commit 9150fc9

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

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
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)