forked from elijahcole/caltech-ee148-spring2020-hw01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matched_filter.py
167 lines (129 loc) · 6.31 KB
/
matched_filter.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
def filter_3d(img_arr, kernel):
kernel = 1/np.std(kernel) * (kernel - np.mean(kernel))
# calculate shifts
win_size_left = int((kernel.shape[0] - 1) / 2)
win_size_right = (kernel.shape[0] - 1) - win_size_left
total_win_size = win_size_left + win_size_right
# create padded image
padded_shape = (img_arr.shape[0] + total_win_size, img_arr.shape[1] + total_win_size, img_arr.shape[2])
img_arr_padded = np.zeros(shape=padded_shape)
img_arr_padded[win_size_left:-win_size_right, win_size_left:-win_size_right, :] = img_arr
# generate "convolved" image
convolved_image = np.zeros(shape=(img_arr.shape[0], img_arr.shape[1]))
for i in range(0, img_arr.shape[0]):
for j in range(0, img_arr.shape[1]):
print(i, j)
pi = i + win_size_left
pj = j + win_size_left
patch = img_arr_padded[(pi - win_size_left):(pi + win_size_right + 1),
(pj - win_size_left):(pj + win_size_right + 1), :]
npatch = 1/np.std(patch) * (patch - np.mean(patch))
convolved_image[i, j] = np.mean(npatch * kernel)
return convolved_image
def filter_2d(img_arr, kernel):
if len(img_arr.shape) == 2:
return filter_2d_one_channel(img_arr, kernel)
kernels = []
for i in range(3):
nkernel = 1/np.std(kernel[:, :, i]) * (kernel[:, :, i] - np.mean(kernel[:, :, i]))
kernels.append(nkernel)
# calculate shifts
win_size_left = int((kernel.shape[0] - 1) / 2)
win_size_right = (kernel.shape[0] - 1) - win_size_left
total_win_size = win_size_left + win_size_right
# create padded image
padded_shape = (img_arr.shape[0] + total_win_size, img_arr.shape[1] + total_win_size, img_arr.shape[2])
img_arr_padded = np.zeros(shape=padded_shape)
img_arr_padded[win_size_left:-win_size_right, win_size_left:-win_size_right, :] = img_arr
# generate "convolved" image
convolved_image = np.zeros(shape=img_arr.shape)
for i in range(0, img_arr.shape[0]):
for j in range(0, img_arr.shape[1]):
print(i, j)
pi = i + win_size_left
pj = j + win_size_left
patch = img_arr_padded[(pi - win_size_left):(pi + win_size_right + 1),
(pj - win_size_left):(pj + win_size_right + 1), :]
for k in range(3):
pk = patch[:, :, k]
npk = 1/np.std(pk) * (pk - np.mean(pk))
convolved_image[i, j, k] = np.mean(npk * kernels[k])
return convolved_image
def filter_2d_one_channel(img_arr, kernel):
print('one_channel')
kernel = np.mean(kernel, axis=2)
# calculate shifts
win_size_left = int((kernel.shape[0] - 1) / 2)
win_size_right = (kernel.shape[0] - 1) - win_size_left
total_win_size = win_size_left + win_size_right
# create padded image
padded_shape = (img_arr.shape[0] + total_win_size, img_arr.shape[1] + total_win_size)
img_arr_padded = np.zeros(shape=padded_shape)
img_arr_padded[win_size_left:-win_size_right, win_size_left:-win_size_right] = img_arr
# generate "convolved" image
convolved_image = np.zeros(shape=img_arr.shape)
for i in range(0, img_arr.shape[0]):
for j in range(0, img_arr.shape[1]):
pi = i + win_size_left
pj = j + win_size_left
patch = img_arr_padded[(pi - win_size_left):(pi + win_size_right + 1),
(pj - win_size_left):(pj + win_size_right + 1)]
if np.max(patch) != 0:
# denom_matrix = np.std(patch)
# npk = np.divide(1, denom_matrix, out=np.zeros_like(img_arr), where=denom_matrix != 0)
npk = 1 / np.std(patch) * (patch - np.mean(patch))
convolved_image[i, j] = np.mean(npk * kernel)
return convolved_image
def filter_1d(img_arr, kernel):
kernels = []
for i in range(3):
flat_kernel = kernel[:, :, i].flatten()/np.linalg.norm(kernel[:, :, i].flatten())
kernels.append(flat_kernel)
# calculate shifts
win_size_left = int((kernel.shape[0] - 1) / 2)
win_size_right = (kernel.shape[0] - 1) - win_size_left
total_win_size = win_size_left + win_size_right
# create padded image
padded_shape = (img_arr.shape[0] + total_win_size, img_arr.shape[1] + total_win_size, img_arr.shape[2])
img_arr_padded = np.zeros(shape=padded_shape)
img_arr_padded[win_size_left:-win_size_right, win_size_left:-win_size_right, :] = img_arr
# generate "convolved" image
convolved_image = np.zeros(shape=img_arr.shape)
for i in range(0, img_arr.shape[0]):
for j in range(0, img_arr.shape[1]):
print(i, j)
pi = i + win_size_left
pj = j + win_size_left
patch = img_arr_padded[(pi - win_size_left):(pi + win_size_right + 1),
(pj - win_size_left):(pj + win_size_right + 1), :]
for k in range(3):
patchk = patch[:, :, k].flatten()/np.linalg.norm(patch[:, :, k].flatten())
convolved_image[i, j, k] = kernels[k] @ patchk
return convolved_image
def smooth(img_arr, kernel):
# kernel = np.mean(kernel, axis=2)
# flat_kernel = kernel[:, :].flatten()/np.linalg.norm(kernel[:, :].flatten())
# calculate shifts
win_size_left = int((kernel.shape[0] - 1) / 2)
win_size_right = (kernel.shape[0] - 1) - win_size_left
total_win_size = win_size_left + win_size_right
# create padded image
padded_shape = (img_arr.shape[0] + total_win_size, img_arr.shape[1] + total_win_size)
img_arr_padded = np.zeros(shape=padded_shape)
img_arr_padded[win_size_left:-win_size_right, win_size_left:-win_size_right] = img_arr
# generate "convolved" image
convolved_image = np.zeros(shape=img_arr.shape)
for i in range(0, img_arr.shape[0]):
for j in range(0, img_arr.shape[1]):
# print(i, j)
pi = i + win_size_left
pj = j + win_size_left
patch = img_arr_padded[(pi - win_size_left):(pi + win_size_right + 1),
(pj - win_size_left):(pj + win_size_right + 1)]
if np.max(patch) == 0:
continue
# patchk = patch[:, :].flatten()/np.linalg.norm(patch[:, :].flatten())
# convolved_image[i, j] = flat_kernel @ patchk
convolved_image[i, j] = np.sum(patch * kernel)
return convolved_image