forked from elijahcole/caltech-ee148-spring2020-hw01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_matched_filter_kernel.py
113 lines (87 loc) · 3.13 KB
/
generate_matched_filter_kernel.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
104
105
106
107
108
109
110
111
112
113
import numpy as np
import matplotlib.pyplot as plt
import visualize as viz
import utilities
import glob
def search_for_kernel(img, kernel_save_path):
events = []
def onclick(event):
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata))
events.append(event)
kernel_names = glob.glob('../data/kernels/*')
base_num = 0
for kname in kernel_names:
try:
val = int(kname.rstrip('.npy').split('=')[-1])
print(val)
if val > base_num:
base_num = val
except ValueError:
pass
base_num += 1
print(base_num)
fig = plt.figure()
plt.imshow(np.asarray(img))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
viz.plt.show()
im_arr = np.copy(np.asarray(img))
print(im_arr.shape)
# im_mask = np.zeros(im_arr.shape, dtype=np.bool)
box_size = 6
boxes = []
for i, event in enumerate(events):
# WHY IS THIS SHIT ALWAYS INVERTED
x = int(event.xdata)
y = int(event.ydata)
print(x, y)
print(img.getpixel(xy=(x, y)))
box = im_arr[(y-box_size):(y+box_size+1), (x-box_size):(x+box_size+1), :]
boxes.append(box)
plt.imshow(box)
plt.show()
save_kernels(box, kernel_save_path, i + base_num)
# np.stack(boxes, axis=3)
# box_stack = np.stack(boxes, axis=3)
# avg_box = np.mean(box_stack, axis=3)
# save_kernels(avg_box, kernel_save_path, 'average')
fig.canvas.mpl_disconnect(cid)
def inspect_image(img):
events = []
img_arr = np.asarray(img)
def onclick(event):
ex = int(event.xdata)
ey = int(event.ydata)
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f, rgb=(%d, %d, %d)' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata, img_arr[ey, ex, 0], img_arr[ey, ex, 1], img_arr[ey, ex, 2]))
events.append(event)
fig = plt.figure()
plt.imshow(img_arr)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
viz.plt.show()
im_arr = np.copy(np.asarray(img))
print(im_arr.shape)
# im_mask = np.zeros(im_arr.shape, dtype=np.bool)
box_size = 6
for i, event in enumerate(events):
x = int(event.xdata)
y = int(event.ydata)
print(x, y)
print(img.getpixel(xy=(x, y)))
box = im_arr[(y-box_size):(y+box_size+1), (x-box_size):(x+box_size+1), :]
plt.imshow(box)
plt.show()
fig.canvas.mpl_disconnect(cid)
def save_kernels(box, kernel_save_path, i):
utilities.create_nonexistent_folder(kernel_save_path)
np.save(kernel_save_path + '/kernel=' + str(i), box)
path = '../data/hw01_preds/preds.json'
kernel_path = '../data/kernels'
image_base_path = '../data/RedLights2011_Medium'
image_paths = sorted(glob.glob(image_base_path + '/*'))
image_path = image_paths[9]
img = utilities.load_image(image_path)
img_arr = utilities.image_to_array(img)
search_for_kernel(img, kernel_path)