Skip to content

Commit a4027f2

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

File tree

1 file changed

+31
-0
lines changed
  • src/Filtering/Convolution/ConvolveImageWithKernel

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 = 9
16+
if args.width:
17+
width = args.width
18+
19+
def CreateSmoothKernel(width):
20+
return np.ones([width, width], 'float32')
21+
22+
kernel = CreateSmoothKernel(width)
23+
kernel_image = itk.image_from_array(kernel)
24+
25+
# Convolve image with kernel.
26+
filteredImage = itk.convolution_image_filter(inputImage, kernel_image=kernel_image)
27+
28+
# Visualize the result using matplotlib
29+
import matplotlib.pyplot as plt
30+
plt.imshow(filteredImage)
31+
plt.show()

0 commit comments

Comments
 (0)