-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
74 lines (53 loc) · 2.28 KB
/
code.py
File metadata and controls
74 lines (53 loc) · 2.28 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
'''
___OVERVIEW___
CODE FOR QUESTION 2 - HW 3
___SUMMARY___
Filterning (linear and non-linear)
___AUTHOR___
Amirhossein Daraie — 9733023
___DATE___
18/April/2021
29/01/1400
'''
import numpy as np
import matplotlib.pyplot as plt
import cv2
def filter(image ,mode="averaging", size=3, padding=1, stride=1):
image = np.pad(image, [(padding, padding), (padding, padding)], mode='symmetric')
kernel_height, kernel_width = (size,size)
padded_height, padded_width = image.shape
output_height = (padded_height - kernel_height) // stride + 1
output_width = (padded_width - kernel_width) // stride + 1
new_image = np.zeros((output_height, output_width)).astype(np.float32)
if mode == "averaging" :
kernel = np.ones((size,size))/size**2
for y in range(0, output_height):
for x in range(0, output_width):
new_image[y][x] = np.sum(image[y * stride:y * stride + kernel_height,
x * stride:x * stride + kernel_width] * kernel).astype(np.float32)
elif mode == "median":
kernel = np.ones((size,size))
for y in range(0, output_height):
for x in range(0, output_width):
new_image[y][x] = np.median(image[y * stride:y * stride + kernel_height,
x * stride:x * stride + kernel_width]).astype(np.float32)
return new_image
# load image
image = cv2.imread(r"../retina_image.jpg", flags=cv2.IMREAD_GRAYSCALE)
print(image)
# apply filters
# avg3x3 = filter(image, mode='averaging')
# med3x3 = filter(image, mode='median')
# fig, ax = plt.subplots(1,3,figsize=(6,3))
# st = fig.suptitle("Average and Median", fontsize="x-large")
# ax[0].title.set_text('original')
# ax[0].imshow(image,vmin=avg3x3.min(),vmax=avg3x3.max(), cmap='gray', interpolation='nearest')
# ax[1].title.set_text('averaging (3x3)')
# ax[1].imshow(avg3x3,vmin=avg3x3.min(),vmax=avg3x3.max(), cmap='gray', interpolation='nearest')
# ax[2].title.set_text('median (3x3)')
# ax[2].imshow(med3x3,vmin=med3x3.min(),vmax=med3x3.max(), cmap='gray', interpolation='nearest')
# for axi in ax.ravel():
# axi.axis('off')
# plt.tight_layout()
# # plt.savefig('9733023-2_image1.png', bbox_inches='tight')
# plt.show()