-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtagtracker.m
More file actions
89 lines (73 loc) · 2.65 KB
/
Copy pathtagtracker.m
File metadata and controls
89 lines (73 loc) · 2.65 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
function [ annotations ] = tagtracker(annotations, outpath)
%TAGTRACKER Defines tag tracks and updates a tag annotations file
% Assigns tracks to sets of tag images listed in an annotations array
% structure. Tag tracks are determined by the euclidean distance between
% feature vectors of x- and y-coordinates and bounding box area.
%
% SYNTAX
% [ annotations ] = tagtracker(annotations, outpath)
%
% DESCRIPTION
% [ annotations ] = tagtracker(annotations, outpath) defines tag tracks
% in the specified annotations array structure and updates
% tag_annotations.mat in the specified outpath.
%
% AUTHOR
% Blair J. Rossetti
%
% DATE LAST MODIFIED
% 2016-05-10
% parameters
t = 0.5;
% remove preexisting trackid field
if isfield(annotations, 'trackid')
annotations = rmfield(annotations, 'trackid');
end
% initialize track structure with tracks in first frame
times = unique([annotations.time]);
for i = 1:find([annotations.time] == times(1), 1, 'last')
annotations(i).trackid = i;
end
%loop through each time point
for i = times(2:end)
% get current tag indices
tagIdx = find([annotations.time] == i);
% get track within 1 sec of current time
trackIdx = find([annotations.time] > i-t & [annotations.time] < i);
% create new track if none found else detect match
if isempty(trackIdx)
for j = tagIdx
annotations(j).trackid = max([annotations.trackid])+1;
end
else
% get last frame of each track
tracks = annotations(trackIdx);
[~, trackIdx] = unique(flip([tracks.trackid]));
trackIdx = length(tracks)-trackIdx+1;
tracks = tracks(trackIdx);
% create feature matrix for tracks
trackMat = [vertcat(tracks.centroid), vertcat(tracks.area)];
% create feature matrix for current tags
tags = annotations(tagIdx);
tagMat = [vertcat(tags.centroid), vertcat(tags.area)];
% match features
idxPair = matchFeatures(trackMat, tagMat, 'Unique', true);
% assign unmatched tags
umIdx = true(1, length(tags));
umIdx(idxPair(:,2)) = false;
umIdx = find(umIdx);
tracknum = max([annotations.trackid]);
for j = umIdx
tracknum = tracknum + 1;
tags(j).trackid = tracknum;
end
% assign matched tags
if ~isempty(idxPair)
[tags(idxPair(:,2)).trackid] = tracks(idxPair(:,1)).trackid;
end
[annotations(tagIdx).trackid] = tags.trackid;
end %if-else
end %for
% save tag annotations
save(fullfile(outpath, 'tags', 'tag_annotations.mat'), 'annotations');
end %function