-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeanShift_Tracking1.m
75 lines (74 loc) · 2.18 KB
/
MeanShift_Tracking1.m
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
%% Mean-Shift Video Tracking
% by Sylvain Bernhardt
% July 2008
%% Description
% Tracks a patch 'T' in a video sequence 'Movie'
% using the Mean-Shift algorithm.
% f is the similiraty function between the original patch
% and the candidate ones along the video sequence.
%
% [x,y,loss,f,f_indx] = MeanShift_Tracking(q,I2,Lmap,...
% height,width,f_thresh,max_it,x0,y0,H,W,k,gx,gy,...
% f,f_indx,loss)
% with:
% (x,y) - the location of the target in the frame I2
% (f,f_indx) - storing the evolution of similarity
% loss - flag for target loss
% q - PDF of the reference target
% I2 - the next frame
% Lmap - length of colormap, also number of bins for PDF
% height,width - size of I2
% f_thresh - the similarity threshold
% max_it - the maximum number of iterations
% x0,y0 - the location of the target
% H,W - its size
% (k,gx,gy) - kernel mask and its gradients
function [x,y,loss,f,f_indx] = MeanShift_Tracking(q,I2,Lmap,...
height,width,f_thresh,max_it,x0,y0,H,W,k,gx,gy,f,f_indx,...
loss)
% Initialization in the next frame from the
% same location than in the current frame.
y = y0;
x = x0;
T2 = I2(y:y+H-1,x:x+W-1);
p = Density_estim(T2,Lmap,k,H,W,0);
% Number of iterations
step = 1;
% Computation of the similarity value
% between the two PDF.
[fi,w] = Simil_func(q,p,T2,k,H,W);
f = cat(2,f,fi);
% Applying Mean-shift algorithm
while f(f_indx)<f_thresh && step<max_it
step = step+1;
f_indx = f_indx+1;
num_x = 0;
num_y = 0;
den = 0;
for i = 1:H
for j=1:W
num_x = num_x+i*w(i,j)*gx(i,j);
num_y = num_y+j*w(i,j)*gy(i,j);
den = den+w(i,j)*norm([gx(i,j) gy(i,j)]);
end
end
% Displacement vector (dx,dy) on the gradient ascent
if den ~= 0
dx = round(num_x/den);
dy = round(num_y/den);
y = y+dy;
x = x+dx;
end
% Detection of target loss or out of frame boundaries
if (y<1 || y>height-H) || (x<1 || x>width-W)
loss = 1;
Target_Loss_Dialog_Box();
uiwait(Target_Loss_Dialog_Box);
break;
end
% Update the target
T2 = I2(y:y+H-1,x:x+W-1);
p = Density_estim(T2,Lmap,k,H,W,0);
[fi,w] = Simil_func(q,p,T2,k,H,W);
f = cat(2,f,fi);
end