-
Notifications
You must be signed in to change notification settings - Fork 1
/
convolutionKernel.py
88 lines (78 loc) · 2.91 KB
/
convolutionKernel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import numpy
import sys
import math
def gaussian(x, y, sigma):
return 1/(2 * math.pi * (sigma**2)) * math.exp(-(x**2 + y**2)/(2*(sigma**2)))
def gaussianBlurKernel(kernelSize, sigma):
convolutionKernel = numpy.empty((kernelSize, kernelSize))
for i in range(kernelSize):
for j in range(kernelSize):
x = i - math.floor(kernelSize/2)
y = j - math.floor(kernelSize/2)
convolutionKernel[i, j] = gaussian(x, y, sigma)
return convolutionKernel
# Read arguments (image filename and kernel entries)
filename = sys.argv[1]
kernelArguments = sys.argv[2:]
# Load image
image = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
# Construct convolution kernel
convolutionKernel = None
if (kernelArguments[0] == "box-blur"):
kernelSizeFloat = float(kernelArguments[1])
kernelSize = int(kernelSizeFloat)
if (kernelSize != kernelSizeFloat or kernelSize < 1):
sys.exit("Kernel size must be a natural number")
convolutionKernel = numpy.ones((kernelSize, kernelSize))
elif (kernelArguments[0] == "normalized-box-blur"):
kernelSizeFloat = float(kernelArguments[1])
kernelSize = int(kernelSizeFloat)
if (kernelSize != kernelSizeFloat or kernelSize < 1):
sys.exit("Kernel size must be a natural number")
entryValue = 1 / (kernelSize ** 2)
convolutionKernel = numpy.full((kernelSize, kernelSize), entryValue)
elif (kernelArguments[0] == "gaussian-blur"):
sigma = float(kernelArguments[2])
kernelSizeFloat = float(kernelArguments[1])
kernelSize = int(kernelSizeFloat)
if (kernelSize != kernelSizeFloat or kernelSize < 1):
sys.exit("Kernel size must be a natural number")
convolutionKernel = gaussianBlurKernel(kernelSize, sigma)
elif (kernelArguments[0] == "normalized-gaussian-blur"):
sigma = float(kernelArguments[2])
kernelSizeFloat = float(kernelArguments[1])
kernelSize = int(kernelSizeFloat)
if (kernelSize != kernelSizeFloat or kernelSize < 1):
sys.exit("Kernel size must be a natural number")
convolutionKernel = gaussianBlurKernel(kernelSize, sigma)
entrySum = 0
for i in range(kernelSize):
for j in range(kernelSize):
entrySum += convolutionKernel[i, j]
for i in range(kernelSize):
for j in range(kernelSize):
convolutionKernel[i, j] /= entrySum
else:
kernelSizeFloat = math.sqrt(len(kernelArguments))
kernelSize = int(kernelSizeFloat)
if (kernelSize == 0 or (kernelSize != kernelSizeFloat)):
sys.exit("Kernel must be square")
convolutionKernel = numpy.empty((kernelSize, kernelSize))
for i, n in enumerate(kernelArguments):
convolutionKernel[math.floor(i / kernelSize), i % kernelSize] = n
print("Convolution kernel:")
print(convolutionKernel)
# Apply convolution kernel
convolutedImage = cv2.filter2D(image, -1, convolutionKernel)
# GUI
# S: Save
# X: Close window
cv2.imshow("Original image", image)
cv2.imshow("Applied convolution kernel", convolutedImage)
key = None
while (key != 0x78):
key = cv2.waitKey(0)
if (key == 0x73):
cv2.imwrite("appliedConvolutionKernel.png", convolutedImage)
cv2.destroyAllWindows()