-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocalSearchRT.m
103 lines (89 loc) · 2.43 KB
/
localSearchRT.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
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
function [ ids ] = localSearchRT( data, w )
% Solves tracking problem using frame-wise local search through all the
% permutations
%
% 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.
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);
%-------- search for the right permutation
[m,n] = size(dmat);
%n = size(dmat,2);
% first guess
ix = zeros(n, 1);
if n<m
ix = 1:n;
else
ix(1:m) = 1:m;
end
cost = costFunction(dmat, ix);
continueFlag = 1;
while continueFlag
continueFlag = 0;
% test all the swaps
for i=1:n
for j=1:n
nix = ix; % new ix
nix([i,j]) = nix([j,i]); % swap
newCost = costFunction(dmat, nix);
if newCost < cost
ix = nix;
cost = newCost;
continueFlag = 1;
end
end
end
% test zeroing
for i=1:n
nix = ix; % new ix
nix(i) = 0;
newCost = costFunction(dmat, nix);
if newCost < cost
ix = nix;
cost = newCost;
continueFlag = 1;
end
end
end
result = zeros(m,1);
prev = ids{frame-1};
% assign previous ids
nz = find(ix>0);
result(ix(nz)) = prev(nz);
% create new ids
for i=find(result==0)
result(i) = idCounter;
idCounter = idCounter+1;
end
ids{frame} = result;
end;
end
% dmat(i,j) = dist(next(i), prev(j))
% perm - permutation: result(perm)=prev
function cost = costFunction(dmat, perm)
% how far away should rectangle jump to be considered as a different one
divergeDistance = 1000;
cost = 0;
for i=1:numel(perm)
if perm(i)==0
cost = cost + divergeDistance;
else
cost = cost + dmat(perm(i),i);
end
end
end