-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (80 loc) · 3.12 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import argparse
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
def generate_kernel(n):
kernel = np.ones((n, n), np.float32) / (n * n)
# print(f'Kenel:\n{kernel}\nSize: {n}')
return kernel
def draw_plot(img, title):
plt.figure(figsize=(
img.shape[1] / 100, img.shape[0] / 100))
plt.imshow(img)
plt.title(title)
plt.xticks([])
plt.yticks([])
def read_rgb_img(imgpath):
img = cv.imread(imgpath) # la imagen se lee como BGR
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB) # con esto la paso a RGB
return img_rgb
def get_channel(channel_index):
if channel_index == 0:
return 'RED'
elif channel_index == 1:
return 'GREEN'
elif channel_index == 2:
return 'GREEN'
def avgown(img, kernel, kernel_size):
n = kernel_size
height, width, _ = img.shape
result = np.zeros((height, width, 3), dtype=np.uint8)
# refactorizar
print(f"alto={height} - ancho={width}")
for i in range(height): # en el excel i es alto = 20
for j in range(width): # j es ancho = 18 (para el ejemplo de la imagen 20x18)
# print(f'i,j {i},{j}')
for c in range(3): # por cada canal
# print(f'pixel colr in chanel={get_channel(c)}: [{img[i,j,c]}]')
# print(f'pixel: [{img[i,j]}]')
pixel_value = 0
for ki in range(n): # para iterar kernel
for kj in range(n):
y = i - n // 2 + ki # y = alto
x = j - n // 2 + kj # x = ancho (quizzá aquí este la diferencia)
if 0 <= x < width and 0 <= y < height:
# print(f'Inside image: {img[y,x, c]}')
pixel_value += img[y, x, c] * kernel[ki, kj]
result[i, j, c] = int(pixel_value)
return result
def main():
parser = argparse.ArgumentParser(description='Average filter')
parser.add_argument('filter', choices=['avglib', 'avgown', 'avgboth'], help='Filter to apply')
parser.add_argument('a', type=int, help='Kernel size')
parser.add_argument('impath', type=str, help='image path')
args = parser.parse_args()
kernel = generate_kernel(args.a)
print(f'=================\n'
f'Filter: {args.filter}\n'
f'Kernel:\n'
f'({args.a}x{args.a})\n'
f'=================')
if args.filter == 'avglib':
img = read_rgb_img(args.impath)
print("shape original: ", img.shape)
print("img original: ", img[1][0])
result = cv.filter2D(img, -1, kernel)
# print("shape result : ", result.shape)
# print("img result: ", result[6][9])
elif args.filter == 'avgown':
print("Own implementation for mean's filter")
img = read_rgb_img(args.impath)
result = avgown(img, kernel, args.a)
# print(result)
elif args.filter == 'avgboth':
# TODO
print("TODO")
draw_plot(img, f'{args.filter} original ({args.a}x{args.a})')
draw_plot(result, f'{args.filter} result ({args.a}x{args.a})')
plt.show()
if __name__ == '__main__':
main()