-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolution.py
More file actions
55 lines (45 loc) · 1.66 KB
/
convolution.py
File metadata and controls
55 lines (45 loc) · 1.66 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
import numpy as np
import matplotlib.pyplot as plt
# let img1 be an image with no features
img1 = np.array([np.array([200, 200]), np.array([200, 200])])
print(img1)
img2 = np.array([np.array([200, 200]), np.array([0, 0])])
img3 = np.array([np.array([200, 0]), np.array([200, 0])])
kernel_horizontal = np.array([np.array([2, 2]), np.array([-2, -2])])
print(kernel_horizontal, 'is a kernel for detecting horizontal edges')
kernel_vertical = np.array([np.array([2, -2]), np.array([2, -2])])
print(kernel_vertical, 'is a kernel for detecting vertical edges')
# We will apply the kernels on the images by
# elementwise multiplication followed by summation
def apply_kernel(img, kernel):
return np.sum(np.multiply(img, kernel))
# Visualizing img1
plt.imshow(img1)
plt.axis('off')
plt.title('img1')
plt.show()
# Checking for horizontal and vertical features in image1
print('Horizontal edge confidence score:', apply_kernel(img1,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img1,
kernel_vertical))
# Visualizing img2
plt.imshow(img2)
plt.axis('off')
plt.title('img2')
plt.show()
# Checking for horizontal and vertical features in image2
print('Horizontal edge confidence score:', apply_kernel(img2,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img2,
kernel_vertical))
# Visualizing img3
plt.imshow(img3)
plt.axis('off')
plt.title('img3')
plt.show()
# Checking for horizontal and vertical features in image3
print('Horizontal edge confidence score:', apply_kernel(img3,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img3,
kernel_vertical))