-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnearestRT.m
77 lines (61 loc) · 2.08 KB
/
nearestRT.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
function [ ids ] = nearestRT( data, w )
% Solves tracking problem in the easiest way - just selecting the nearest
% rect from the next frame. Distance is measured using coordinates, scale,
% velocity and hog with some weights w.
%
% Inputs:
% data - struct that can be read from data.mat or generated with
% reduceDataFPS
% w = [xy; scale; velocity; hog] is a vector (4x1) of boosting weights
%
% Outputs:
% ids - cell array, with size data.nFrames, each element is a
% vector of integers, representing an id of the corresponding
% rectangle from the data.
% how far away should rectangle jump to be considered as a different one
divergeDistance = 200;
idCounter = 1; % id for each new rectangle
ids = {};
% the first frame
frameData = data.Frames(1);
nObjs = frameData.nObjects;
ids{1} = (1:nObjs)';
idCounter = 1+nObjs;
% loop through all the frames
for frame=2:data.nFrames
dmat = getDistMatrix(data, frame, ids, w);
% get min distances
[d, ix] = min(dmat);
% actually, this line was an essence of the whole method
frameData = data.Frames(frame);
n = frameData.nObjects;
result = zeros(n,1);
prev = ids{frame-1};
% solve conflicts
u = unique(ix);
if numel(u) < numel(d)
h = histc(ix,u); % count occurrences
for i=find(h>1) % Gotcha! here is ours conflict
same = find(ix==u(i))'; % rects with the same index
[~,m] = min(d(same)); % the closest of them
same = removerows(same,'ind',m); % rows to remove
ix = removerows(ix','ind',same)'; % remove them
d = removerows(d','ind',same)'; % remove
prev = removerows(prev,'ind',same); % remove
end
end
% assign previous ids
result(ix) = prev;
% check if it is too far away
for i=find(d>divergeDistance)
result(ix(i)) = idCounter;
idCounter = idCounter+1;
end
% create new ids
for i=find(result==0)
result(i) = idCounter;
idCounter = idCounter+1;
end
ids{frame} = result;
end;
end