-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartSequence.m
More file actions
163 lines (120 loc) · 3.78 KB
/
startSequence.m
File metadata and controls
163 lines (120 loc) · 3.78 KB
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
function R = startSequence(videoFile)
% Determines the position of the object in each frame
% videoFile: The input video
% objcentre: The centre of object that we are trying to track
% objsize: The bounding rectangle
ncolors = 8 * 8 * 8;
eps = 0.000001;
reader = VideoReader(videoFile);
%Comment this line if no offset is required
reader.CurrentTime = 180;
nrframes = ceil(reader.FrameRate * reader.Duration);
R = zeros(2, nrframes);
%Get the first frame and calculate the probabilty
frame = readFrame(reader);
imshow(frame);
rect = getrect;
rect = round(rect);
%Get the bounding rectangle
c = rect(1);
r = rect(2);
width = rect(3);
height = rect(4);
if rem(width, 2) == 0
width = width - 1;
end
if rem(height, 2) == 0
height = height - 1;
end
%Get the number of frames to track
n_track = input('Number of frames to track: ');
objcentre = round([r + height / 2, c + width / 2]);
objsize = [height, width];
y = objcentre;
frame = ReduceColor(frame, ncolors);
kernel = MyKernel(objsize);
q_dist = Probability(frame, objcentre, objsize, kernel, ncolors);
i = 1;
figure;
track_size = objsize;
iter_vec = zeros(n_track, 1);
bhat_vec = zeros(n_track, 1);
while (reader.hasFrame())
%Determine the p-distribution
origframe = readFrame(reader);
frame = ReduceColor(origframe, ncolors);
best_rho = 0;
%If scaling is not required, replace with 0:0
for scale = -10:10
track_size_test = track_size + track_size * scale / 100;
track_size_test = round(track_size_test);
track_size_test = bitor(track_size_test, 1);
kernel_new = MyKernel(track_size_test);
iter = 0;
while 1
[p_dist, box] = Probability(frame, y, track_size_test, kernel_new, ncolors);
rho = Bhattacharya(q_dist, p_dist);
weights = getWeights(box, p_dist, q_dist);
%Get the new location of the target
[gridc, gridr] = getObjGrid(track_size_test);
newc = gridc .* weights;
newr = gridr .* weights;
newc = sum(newc(:));
newr = sum(newr(:));
cdash = newc / sum(weights(:));
rdash = newr / sum(weights(:));
y1 = round(y + [rdash cdash]);
while 1
p_dist1 = Probability(frame, y1, track_size_test, kernel_new, ncolors);
rho1 = Bhattacharya(q_dist, p_dist1);
if rho1 < rho
y2 = round((y1 + y) * 0.5);
else
break;
end
if norm(y2 - y1) < eps
break;
end
y1 = y2;
end
if norm(y1 - y) < eps
break;
end
y = y1;
iter = iter + 1;
end
if rho1 > best_rho
best_rho = rho1;
best_y = y;
best_track_size = track_size_test;
best_iter = iter;
end
end
y = best_y;
track_size = best_track_size;
iter = best_iter;
rho1 = best_rho;
iter_vec(i) = iter;
bhat_vec(i) = sqrt(1 - rho1);
R(:, i) = y;
shape_top = round(y(1) - track_size(1) / 2);
shape_left = round(y(2) - track_size(2) / 2);
origframe = insertShape(origframe, 'rectangle', [shape_left, shape_top, track_size(2), track_size(1)], 'LineWidth', 2);
imshow (origframe);
pause(0.005);
i = i + 1;
if i > n_track
break;
end
end
figure;
area(bhat_vec);
title('The distance values.');
xlabel('Frame');
ylabel('distance');
figure;
area(iter_vec);
title('The number of iterations.');
xlabel('Frame');
ylabel('Iterations');
end