-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_convolution.py
More file actions
120 lines (86 loc) · 4.26 KB
/
image_convolution.py
File metadata and controls
120 lines (86 loc) · 4.26 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Edge detection
scharr = np.array([[ -3-3j, 0-10j, +3 -3j],
[-10+0j, 0+ 0j, +10 +0j],
[ -3+3j, 0+10j, +3 +3j]])
sobel_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
sobel_y = np.array([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
edge_detection = np.array([[0, -1, 0],
[-1, 4, -1],
[0, -1, 0]])
edge_detection2 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
# Blur
mean_filter = (1 / 9) * np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
mean_filter_11 = (1 / 11**2) * np.ones((11, 11))
# Separable Kernels
mean_filter_33_x = (1 / 33) * np.ones((1, 33))
mean_filter_33_y = (1 / 33) * np.ones((33, 1))
# 33x33 for comparison
mean_filter_33_xy = (1 / (33*33)) * np.ones((33, 33))
gaussian_blur_3x3 = (1 / 16) * np.array([[1, 2, 1],
[2, 4, 2],
[1, 2, 1]])
gaussian_blur_5x5 = (1 / 256) * np.array([[1, 4, 6, 4, 1],
[4, 16, 24, 16, 4],
[6, 24, 36, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])
gaussian_blur_9x9 = np.array([[0.000814, 0.001918, 0.003538, 0.005108, 0.005774, 0.005108, 0.003538, 0.001918, 0.000814],
[0.001918, 0.00452, 0.008338, 0.012038, 0.013605, 0.012038, 0.008338, 0.004552, 0.001918],
[0.003538, 0.008338, 0.015378, 0.022203, 0.025094, 0.022203, 0.015378, 0.008338, 0.003538],
[0.005108, 0.012038, 0.022203, 0.032057, 0.036231, 0.032057, 0.022203, 0.012038, 0.005108],
[0.005774, 0.013605, 0.025094, 0.036231, 0.04095, 0.036231, 0.025094, 0.013605, 0.005774],
[0.005108, 0.012038, 0.022203, 0.032057, 0.036231, 0.032057, 0.022203, 0.012038, 0.005108],
[0.003538, 0.008338, 0.015378, 0.022203, 0.025094, 0.022203, 0.015378, 0.008338, 0.003538],
[0.001918, 0.00452, 0.008338, 0.012038, 0.013605, 0.012038, 0.008338, 0.004552, 0.001918],
[0.000814, 0.001918, 0.003538, 0.005108, 0.005774, 0.005108, 0.003538, 0.001918, 0.000814]])
# Sharpen
sharpen = np.array([[1, -2, 1],
[-2, 5, -2],
[1, -2, 1]])
sharpen2 = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
sharpen3 = np.array([[-1, -2, -1],
[-2, 13, -2],
[-1, -2, -1]])
def compare_images(img1, img2):
fig, (orig_img, new_img) = plt.subplots(1, 2, figsize=(15, 6))
orig_img.imshow(img1)
orig_img.set_title('Original Image')
orig_img.set_axis_off()
new_img.imshow(img2)
new_img.set_title('Modified Image')
new_img.set_axis_off()
plt.show()
def convolve(img, *args):
# Move the color channels to index 0, to make looping over them easier
img = img.transpose(2,0,1).astype(np.float32)
num_kernels = len(args)
channels, height, width = img.shape
# This array stores any intermediate steps in the convolution -- when there are multiple kernels
outputs = [np.zeros((channels, height, width)) for i in range(num_kernels + 1)]
outputs[0] = img
# Convolution
for i in range(1, num_kernels + 1):
for j in range(channels):
outputs[i][j] = signal.convolve2d(outputs[i-1][j], args[i-1], boundary='symm', mode='same')
# Reset the pixel values to 0-255, and return the array to its original shape
result = outputs[num_kernels] * 255
result = np.absolute(result.transpose(1,2,0)).astype(np.uint8)
return result
# Load the image
img = mpimg.imread("_______")
new_img = convolve(img, mean_filter_33_x, mean_filter_33_y)
compare_images(img, new_img)