-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvolutionImpl1.py
More file actions
70 lines (53 loc) · 1.68 KB
/
ConvolutionImpl1.py
File metadata and controls
70 lines (53 loc) · 1.68 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
# This is a sample Python script.
# BUILDING BLOCKS TO LENIA
# https://www.youtube.com/watch?v=mSy4z8nDLno
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# array_2d = np.array([
# [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
# [1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# ])
image_path = 'C://Users//zhasi//Downloads//convolutiontest.png'
img_arr = Image.open(image_path)
gray_img = img_arr.convert('L')
garray_2d = np.array(gray_img)
array_2d = garray_2d / 255.0
# array_2d = np.random.random((100,100))
kernel1 = np.array([
[0, 1, 0],
[1, -4, 1],
[0, 1, 0]
])
def wrapped_convolve(array_2d, kernel):
result = np.zeros_like(array_2d)
for i in range(array_2d.shape[0]):
for j in range(array_2d.shape[1]):
conv_sum = 0
for m in range(kernel.shape[0]):
for n in range(kernel.shape[1]):
row_idx = (i - m) % array_2d.shape[0]
col_idx = (j - n) % array_2d.shape[1]
conv_sum += array_2d[row_idx, col_idx] * kernel[m, n]
result[i][j] = conv_sum
return result
plt.subplot(1, 3, 1)
plt.imshow(kernel1, cmap='Greys')
plt.colorbar()
plt.subplot(1, 3, 2)
plt.imshow(array_2d, cmap='Greys')
plt.colorbar()
plt.subplot(1, 3, 3)
plt.imshow(wrapped_convolve(array_2d, kernel1), cmap='Greys')
plt.colorbar()
plt.show()
if __name__ == '__main__':
print('hello')