-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMOSSEFunction
249 lines (171 loc) · 5.56 KB
/
MOSSEFunction
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
# =============================================================================
# %% Init Variables
# =============================================================================
# =============================================================================
# %% Import
# =============================================================================
import numpy as np
from numpy.fft import fft2
import cv2
# =============================================================================
# %% Defs
# =============================================================================
'''
function val = gaussC(x, y, sigma, center)
xc = center(1);
yc = center(2);
exponent = ((x-xc).^2 + (y-yc).^2)./(2*sigma);
val = (exp(-exponent));
end
'''
def gaussC(x,y,sigma,center_x,center_y):
xc = center_x
yc = center_y
exponent = ((x-xc)**2 + (y-yc)**2)/(2*sigma)
val = (exp(-exponent))
return val
'''
function img = preprocess(img)
[r,c] = size(img);
win = window2(r,c,@hann);
eps = 1e-5;
img = log(double(img)+1);
img = (img-mean(img(:)))/(std(img(:))+eps);
img = img.*win;
end
'''
def preprocess(img):
r,c,_ = frame.shape()
# win = window2(r,c,@hann);
win = window2(r,c)
eps = 1e-5
img = np.log(double(img)+1)
_ = img.flatten()
img = np.divide( (img-mean( _ ) ),(std( _ )+eps) )
# img = img.*win
img = np.multiply(img,win)
return img
'''
function w=window2(N,M,w_func)
wc=window(w_func,N);
wr=window(w_func,M);
[maskr,maskc]=meshgrid(wr,wc);
%maskc=repmat(wc,1,M); Old version
%maskr=repmat(wr',N,1);
w=maskr.*maskc;
end
'''
def window2(N,M):
wc = np.hanning(N)
wr = np.hanning(M)
[maskr,maskc] = np.mgrid(wr,wc)
# w=maskr.*mask;
w = np.multiply(maskr,maskc)
return w
'''
function img = rand_warp(img)
a = -180/16;
b = 180/16;
r = a + (b-a).*rand;
sz = size(img);
scale = 1-0.1 + 0.2.*rand;
% trans_scale = randi([-4,4], 1, 1);
img = imresize(imresize(imrotate(img, r), scale), [sz(1) sz(2)]);
'''
def rand_warp(img):
a = -180/16;
b = 180/16;
rand = np.random.rand(1)
r = a + np.multiply((b-a),rand)
sz_y, sz_x, _ = img.shape
rand = np.random.rand(1)
scale = 1-0.1 +np.multiply( 0.2,rand)
# trans_scale = randi([-4,4], 1, 1);
img = cv2.resize( cv2.resize( rotateImage(img, r), (0,0), fx=scale, fy=scale), (sz_y, sz_x ) )
return img
def rotateImage(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
# =============================================================================
# %% CODE
# =============================================================================
# get images
# select target
sigma = 100;
# Center of the target
center_x = rect[1]+rect[3]/2
center_y = rect[0]+rect[2]/2
# Get Sizes
# gsize = size(im);
h,w,_ = frame.shape()
# Create a Grid?
# [R,C] = ndgrid(1:gsize(1), 1:gsize(2));
[R,C] = np.mgrid[0:h,0:w]
# g = gaussC(R,C, sigma, center_x, center_y); # Function
g = gaussC(x,y,sigma,center_x,center_y)
# g = mat2gray(g);
g = np.array(g * 255.0, dtype = np.uint8)
threshed = cv2.adaptiveThreshold(g, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)
# randomly warp original image to create training set
'''
if (size(im,3) == 3)
img = rgb2gray(im);
end
'''
image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# img = imcrop(img, rect);
img = image[rect[0]:rect[2], rect[1]:rect[2]]
g_ = g[rect[0]:rect[2], rect[1]:rect[2]]
G = fft2(g_)
'''
height = size(g_,1);
width = size(g_,2);
'''
height, width, _ = g_.shape
# fi = preprocess(imresize(img, [height width]))
_ = cv2.resize(img, ( int(height), int(width) ) )
fi = preprocess( _ )
# Ai = (G.*np.conj(numpy.fft.fft2(fi)))
Ai = np.multiply(G,np.conj(fft2(fi)))
# Bi = (fft2(fi).*conj(fft2(fi)))
Bi = np.multiply( fft2(fi), np.conj( fft2(fi) ));
N = 128;
for i0 in range(N):
fi = preprocess(rand_warp(img))
Ai = Ai + np.multiply(G, np.conj( fft2(fi) ) )
Bi = Bi + np.multiply(fft2(fi) , np.conj(fft2(fi)) )
# MOSSE online training regimen
eta = 0.125;
# fig = figure('Name', 'MOSSE');
# mkdir(['results_' dataset]);
for i0 in range(0,len(img_files)):
# img = imread(img_files(i,:))
''' Read new frames '''
im = img;
# img = rgb2gray(img);
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if (i0 == 1):
Ai = np.multiply(eta,Ai)
Bi = np.multiply(eta,Bi)
else:
Hi = np.divide(Ai,B)
# fi = imcrop(img, rect);
img = image[rect[0]:rect[2], rect[1]:rect[2]]
fi = preprocess(cv2.resize(fi, (height, width)))
_ = image = cv2.cvtColor( ifft2( np.multiply(Hi,fft2(fi)) ), cv2.COLOR_BGR2GRAY)
# gi = uint8(255*mat2gray(ifft2(Hi.*fft2(fi))));
gi = 255*_
_ = gi.flatten()
maxval = max( _ )
P,Q = np.np.argmax( _ )
# [P, Q] = find(gi == maxval)
dx = mean(P)-height/2
dy = mean(Q)-width/2
rect = [rect[0]+dy, rect[1]+dx, width, height]
# fi = imcrop(img, rect)
fi = img[rect[0]:rect[2], rect[1]:rect[2]]
fi = preprocess( cv2.resize(fi, (height, width) ) )
Ai = np.multiply(eta, np.multiply(G,conj(fft2(fi)) ) + np.multiply((1-eta),Ai)
Bi = np.multiply( eta, np.multiply(fft2(fi), no.conj(fft2(fi)) ) ) + np.multiply( (1-eta), Bi)