-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf.py
executable file
·278 lines (242 loc) · 8.06 KB
/
cf.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
@author: onion-nikolay
"""
import numpy as np
import cv2 as cv
from fft import fft, ifft
from helpers import flattenList
TEST = {'mach'}
LINEAR_FILTERS = {'mach', 'minace'}
def corr(img, flt):
"""\n Calculate correlation of input image and filter. Fourier image of
filter is used. Be careful!
Parameters
----------
img : ndarray
Input image
flt : ndarray
Input filter Fourier image.
Returns
-------
corr : ndarray
"""
return ifft(fft(img)*np.conj(flt))
def corr_output(image, corr_filter, out='value'):
"""\n Calculate correlation and returns different parameters.
Parameters
----------
image : ndarray
corr_filter : ndarray
See cf.corr for more information
out : str
If 'value', returns correlation peak height. If 'coord', returns
CP height and coordinates, If full, returns CP height, coordinates and
CP image. Else raises error.
Returns
-------
output : number or list
"""
corr_output = np.abs(corr(image, corr_filter))
peak = np.max(corr_output)
coord = np.unravel_index(corr_output.argmax(), corr_output.shape)
if out is 'value':
return peak
elif out is 'coord':
return [peak, coord]
elif out is 'full':
return [peak, coord, corr_output]
else:
raise("key {} not found.".format(out))
def corr_output_holo(image, corr_filter, out='value'):
"""\n Like cf.corr_output, calculates correlation and returns
different parameters. But CP coordinates is limited by one angle.
Parameters
----------
image : ndarray
corr_filter : ndarray
See cf.corr for more information
out : str
If 'value', returns correlation peak height. If 'coord', returns
CP height and coordinates, If full, returns CP height, coordinates and
CP image. Else raises error.
Returns
-------
output : number or list
"""
corr_output_raw = np.abs(corr(image, corr_filter))
corr_output = corr_output_raw
_size = np.shape(corr_output)[0]/4
corr_output[3*_size/2:5*_size/2, 3*_size/2:5*_size/2] = 0
peak = np.max(corr_output[_size*2:, _size*2:])
coord = np.unravel_index(corr_output.argmax(), corr_output.shape)
if out is 'value':
return peak
elif out is 'coord':
return [peak, coord]
elif out is 'full':
return [peak, coord, corr_output_raw]
else:
raise("key {} not found.".format(out))
def synthesize(train_objects, train_object_labels, **kwargs):
"""\n Synthesize CF.
Parameters
----------
train_objects : list or lists of ndarray
train_object_labels : list of int
**kwargs
Use for filter_type and other parameters of filters.
Returns
-------
corr_filter : ndarray
"""
try:
filter_type = kwargs['filter_type']
except KeyError:
raise("filter type not found.")
true_objects = []
false_objects = []
for obj, label in zip(train_objects, train_object_labels):
if label == 1:
true_objects.append(obj)
else:
false_objects.append(obj)
true_objects = flattenList(true_objects)
false_objects = flattenList(false_objects)
try:
corr_filter = globals()[filter_type](true_objects, false_objects,
**kwargs)
return corr_filter
except ImportError:
print("Error! Filter {} not found! Return None.".format(filter_type))
return None
def predict(corr_filter, data_to_predict, threshold, return_class=True,
is_holo=False):
"""\n Calculate correlation peaks or classes for input images.
Parameters:
corr_filter : ndarray
data_to_predict : list of ndarray
Input images.
threshold : float
return_class : bool, default=True
If True, returns class (1 or 0). Else returns CP heights.
is_holo : bool, default=False
If True, cf.corr_output_holo uses for calculations, else
cf.corr_output.
Returns
-------
peaks : list of float or int
"""
peaks = []
for image in data_to_predict:
if is_holo:
peak = corr_output_holo(image, corr_filter)
else:
peak = corr_output(image, corr_filter)
peaks.append(peak)
if return_class:
return np.uint8(peaks > threshold)
else:
return peaks
def mach(true_objects, *false_objects, **kwargs):
"""\n MACH: h = [alpha*D_y + (1-alpha^2)^(1/2)S^0_x]^(-1)*m
UOTSDF: MACH(alpha=1)
Default: alpha=0.5
"""
try:
alpha = kwargs['alpha']
except KeyError:
alpha = 0.5
n = len(true_objects)
size = np.shape(true_objects[0])[0]
length = np.size(true_objects[0])
x_fft = __x2fftx(true_objects)
m = np.mean(x_fft, 1)
s = np.zeros(length, dtype=complex)
for i in range(n):
s += (x_fft[:, i] - m) * np.conj(x_fft[:, i] - m)
s = s / n
d = np.mean(x_fft*np.conj(x_fft), 1)
h_fft = np.zeros(length, dtype=complex)
for i in range(length):
h_fft[i] = ((alpha*s[i] + ((1-alpha**2)**0.5)*d[i])**(-1)) * m[i]
h_fft = h_fft / (n*length)
h_fft = np.reshape(h_fft, (size, size), order='C')
return h_fft
def minace(true_objects, *false_objects, **kwargs):
"""\n Doc in progress..."""
from numpy.linalg import inv
n = len(true_objects)
size = np.shape(true_objects[0])[0]
length = np.size(true_objects[0])
x_fft = __x2fftx(true_objects)
d = x_fft*np.conj(x_fft)
try:
alpha = kwargs['alpha']
except KeyError:
alpha = 0
try:
p = alpha*kwargs['noise_sp']
except KeyError:
p = alpha*np.ones((length))
try:
beta = kwargs['beta']
except KeyError:
beta = 1
p = np.reshape(p, (length, 1))
d = np.concatenate((beta*d, ((1-beta**2)**0.5)*p), axis=1)
t = np.max(d, 1)**-1
c = np.ones(n)
h_fft = __dot(t, x_fft)
h_fft = np.dot(np.conj(x_fft.T), h_fft)
h_fft = np.dot(inv(h_fft), c)
h_fft = np.dot(x_fft, h_fft)
h_fft = t * h_fft / (n*length)
h_fft = np.reshape(h_fft, (size, size), order='C')
return h_fft
def __x2fftx(x):
n = len(x)
x_size = np.shape(x[0])[0]
x_length = np.size(x[0])
x_fft = np.zeros((x_size, x_size, n), dtype=complex)
for count in range(n):
x_fft[:, :, count] = fft(x[count])
x_fft = np.reshape(x_fft, (x_length, n), order='C')
return x_fft
def __dot(vec, arr):
[length, n] = np.shape(arr)
res = np.zeros((length, n), dtype=complex)
for i in range(length):
for j in range(n):
res[i, j] = vec[i] * arr[i, j]
return res
def synthesizeHolo(input_image):
"""\n Synthesize Fourier hologram of input image.
"""
image_shape = np.shape(input_image)
holo_image = np.zeros((image_shape[0]*4, image_shape[1]*4),
dtype=complex)
holo_image[image_shape[0]/2:3*image_shape[0]/2,
image_shape[1]/2:3*image_shape[1]/2] = input_image
holo_image = np.real(fft(holo_image))
holo_image = holo_image - np.min(holo_image)
holo_image = holo_image / np.max(holo_image)
return holo_image
def restoreHolo(holo_image, show_image=True):
"""\n Restore Fourier hologram of input image.
Parameters
----------
holo_image : ndarray
show_image : bool, default=True
If True, shows image in new figure.
Returns
-------
restored_holo_image : ndarray
"""
restored_holo_image = np.abs(ifft(holo_image))
image_shape = np.shape(restored_holo_image)
restored_holo_image[image_shape[0]/2, image_shape[1]/2] = 0
restored_holo_image -= np.min(restored_holo_image)
restored_holo_image /= np.max(restored_holo_image)
if show_image:
cv.imshow("Restored Holo", restored_holo_image)
return restored_holo_image