-
Notifications
You must be signed in to change notification settings - Fork 5
/
generateResults.m
334 lines (254 loc) · 9.5 KB
/
generateResults.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function generateResults(self)
% generates the correct "res" structure
if isempty(self.savedTracks.id)
fish.helper.verbose('WARNING: cannot generate results!')
fish.helper.verbose('WARNING: not all fish detected. Maybe adjust "nfish" setting.');
return;
end
if ~isempty(self.savedTracksFull)
warning(['Will NOT process the full track structure. Switching of tracks are thus not ' ...
'considered. The full tracks results can be accessed in the field ' ...
'"savedTracksFull"'])
end
for f = fieldnames(self.savedTracks)'
if iscell(self.savedTracks.(f{1}))
d = length(size(self.savedTracks.(f{1}){1}));% at least 3
self.savedTracks.(f{1}) = cat(d,self.savedTracks.(f{1}){:});
end
end
nFrames = size(self.savedTracks.id,3)/self.nfish;
if self.currentFrame~=nFrames
fish.helper.verbose(['WARNING: %d frames got lost (premature abort while ' ...
'tracking?)'],self.currentFrame-nFrames);
end
self.currentFrame = nFrames;
self.res = [];
fishId2TrackId = self.fishId2TrackId(1:nFrames,:)';
self.res.swb = subGenerateTracks(fishId2TrackId);
self.res.swb.pos = subGeneratePos(self.res.swb);
tabs = self.tabs(1:nFrames,:);
% also generate dag
clear fishId2TrackId
[pos,dagf2t] = subGetPosFromDag();
% check for big overlaps and correct them with switch-based
dagf2t = subCorrectDagOverlaps(dagf2t);
%need to do again
self.res.dag = subGenerateTracks(dagf2t');
self.res.dag.pos = subGeneratePos(self.res.dag);
% correct the time for unique distance dt
dt = 1/self.videoHandler.frameRate;
tidx = round((tabs-tabs(1))/dt)+1;
frames = (1:nFrames)';
t = (0:tidx(end)-1)'*dt;
for f = {'swb','dag'}
for f2 = fieldnames(self.res.(f{1}).tracks)'
field = self.res.(f{1}).tracks.(f2{1});
sz = size(field);
sz(1) = length(t);
tmp = nan(sz);
assert(length(sz)<7)
tmp(tidx,:,:,:,:,:) = field;
self.res.(f{1}).tracks.(f2{1}) = tmp;
end
self.res.(f{1}).t = t;
self.res.(f{1}).tabs = nan(size(t));
self.res.(f{1}).tabs(tidx) = tabs;
self.res.(f{1}).iframe = nan(size(t));
self.res.(f{1}).iframe(tidx) = frames;
pos = self.res.(f{1}).pos;
tmp = nan(size(pos));
tmp(tidx,:,:) = pos;
self.res.(f{1}).pos = tmp;
end
function pos = subGeneratePos(res)
% gets a new pos from the re-ordered tracks. Do not use the original
% pos (which might contain Kalman predictions) but centerLine if available
if isfield(res.tracks,'centerLine')
cl = mean(res.tracks.centerLine,4);
ce = res.tracks.centroid;
idx = find(isnan(cl));
cl(idx) = ce(idx);
pos = permute(cl,[1,3,2]);
else
ce = res.tracks.centroid;
pos = permute(ce,[1,3,2]);
end
% delete beyond border pixels
posx = squeeze(pos(:,1,:));
posy = squeeze(pos(:,2,:));
sz = self.videoHandler.frameSize;
posx(posx>sz(2) | posx<1) = NaN;
posy(posy>sz(1) | posy<1) = NaN;
pos(:,1,:) = posx;
pos(:,2,:) = posy;
end
function df2t = subCorrectDagOverlaps(df2t)
% ASSUMES IDX MAT IS SAME AS ID MSK (no track deletion)
MINOVERLAP = ceil(self.videoHandler.frameRate/2);
PROBTHRES = 0;%self.maxClassificationProb*self.opts.tracks.probThresForFish;
eqmsk = bsxfun(@eq,df2t,permute(df2t,[1,3,2]));
n = size(eqmsk,1);
se = ones(MINOVERLAP,1);
tmp = imdilate(imerode(eqmsk(:,:),se),se);
%tmp = imerode(imdilate(eqmsk(:,:),se),se);
eqmsk = reshape(tmp,[],self.nfish,self.nfish);
idx = find(tril(ones(self.nfish),-1));
ieqmsk = eqmsk(:,idx);
noverlap = sum(ieqmsk,2);
doverlap = sum(eqmsk,3)>2; % double overlaps
% get the positions that were lost in DAG
lostmsk = bsxfun(@eq,1:self.nfish,permute(df2t,[1,3,2]));
tmp = imdilate(imerode(lostmsk(:,:),se),se);
%tmp = imerode(imdilate(tmp,se),se);
lostmsk = reshape(tmp,[],self.nfish,self.nfish);
lostmsk = all(~lostmsk,3);
% note that their might be overlaps in DAG too because we used
% the DAG switching methods... however, they should not be too
% long (except for fishupdate type of corrections...)
% ignore double and strange losses (more than 1) for now.
onelost = sum(lostmsk,2)==1;
[~,idxlost] = max(lostmsk,[],2);
%idxlost(~onelost) = self.nfish+1; % no need. only between start:stop
msk = bsxfun(@and,ieqmsk,onelost & all(~doverlap,2));
% get the cl prob of the lost id
indl = (1:n)' + (idxlost-1)*n;
idl = idxlost; % assume that track ID is same as IDX (correct with
% no deletion)
% get the cl prob of the overlapping id
[~,idxeq] = max(ieqmsk,[],2); % this is now fishID
subeq = fish.helper.i2s([self.nfish,self.nfish],idxeq);
findo = (1:n)' + (subeq(:,1)-1)*n;
ido = df2t(findo); % also idx in cl
indo = (1:n)' + (ido(:,1)-1)*n;
indo(isnan(indo)) = 1; % nan's will not enter anyway
% get prob in the original TrackID(idx) order
f = {'classProb'};
sz = size(self.savedTracks.(f{1}));
d = length(sz); % at least 3
tmp = permute(self.savedTracks.(f{1}),[d,2,1,3:d-1]);
tmp = reshape(tmp,[self.nfish,n,sz(2),sz(1),sz(3:d-1)]);
classProb = reshape(permute(tmp,[2,1,3:d+1]),[],self.nfish);
cll = classProb(indl,:);
%idl = res.swb.tracks.id(indl);
clo = classProb(indo,:);
%ido = res.dag.tracks.id(indo);
for i = 1:length(idx)
mski = msk(:,i);
[fid1,fid2] = ind2sub(self.nfish([1,1]),idx(i));
% find onsets and offsets
d = diff([0;mski;0]);
stop = find(d==-1)-1;
start = find(d==1);
% get prob data
accmsk = zeros(n,1);
accmsk(start) = 1;
accmsk = cumsum(accmsk);
accmsk(~mski) = length(start)+1;
mclo1 = accumarray(accmsk,clo(:,fid1),[length(start)+1,1],@mean);
mclo2 = accumarray(accmsk,clo(:,fid2),[length(start)+1,1],@mean);
mcll1 = accumarray(accmsk,cll(:,fid1),[length(start)+1,1],@mean);
mcll2 = accumarray(accmsk,cll(:,fid2),[length(start)+1,1],@mean);
s12 = mclo1 + mcll2;
s21 = mclo2 + mcll1;
order12 = s12(1:end-1) >= s21(1:end-1);
order21 = ~order12;
diffprob = abs(s12-s21);
probmsk = diffprob<PROBTHRES;
probmsk = probmsk | max(mcll1,mcll2)<self.maxClassificationProb*self.opts.tracks.probThresForFish;
order12(probmsk) = false;
order21(probmsk) = false;
% re-order the results. Just redefine the
msk12 = zeros(n+1,1);
msk12(start(order12)) = 1;
msk12(stop(order12)+1) = -1;
msk12 = cumsum(msk12);
idx12 = find(msk12(1:end-1));
msk21 = zeros(n+1,1);
msk21(start(order21)) = 1;
msk21(stop(order21)+1) = -1;
msk21 = cumsum(msk21);
idx21 = find(msk21(1:end-1));
df2t(idx12,fid1) = ido(idx12);
df2t(idx12,fid2) = idl(idx12);
df2t(idx21,fid1) = idl(idx21);
df2t(idx21,fid2) = ido(idx21);
end
end
function [res] = subGenerateTracks(f2t);
res = [];
trackIdMat = reshape(self.savedTracks.id,self.nfish,nFrames);
for j = 1:self.nfish
u = unique(trackIdMat(j,:));
n = find(~isnan(u));
L(j) = length(n);
U(1,j) = u(n(1));
end
if all(L==1)
% short-cut to avoid the loop
[~,Loc] = ismember(f2t,U);
else
% HOW CAN THIS DONE A BIT MORE EFFICIENTLY?
Loc = zeros(size(f2t));
for i = 1:nFrames
[~,Loc(:,i)] = ismember(f2t(:,i),trackIdMat(:,i));
end
end
% fill in the gaps
order = (1:self.nfish)';
msk = ~Loc;
idx = find(any(msk,1));
for i = 1:length(idx)
loc = Loc(:,idx(i));
rest = setdiff(order,loc(~~loc));
Loc(~loc,idx(i)) = rest;
end
fridx = ones(1,self.nfish)' * (1:nFrames);
idx = fish.helper.s2i(size(Loc),[Loc(:),fridx(:)]);
for f = fieldnames(self.savedTracks)'
if isempty(self.savedTracks.(f{1}))
continue;
end
% $$$ if strcmp(f{1},'stmInfo')
% $$$ continue;
% $$$ end
sz = size(self.savedTracks.(f{1}));
d = length(sz); % at least 3
trackdat = permute(self.savedTracks.(f{1}),[d,2,1,3:d-1]);
fishdat = reshape(trackdat(idx,:),[self.nfish,nFrames,sz(2),sz(1),sz(3:d-1)]);
res.tracks.(f{1}) = permute(fishdat,[2,1,3:d+1]);
end
fishid = (1:self.nfish)' * ones(1,nFrames);
fishid(msk) = NaN;
res.tracks.fishId = fishid';
end
function [postrace,trackIdxMat] = subGetPosFromDag(assignedFishId,force)
if nargin>0 && ~isempty(assignedFishId)
predFishIds = assignedFishId;
else
predFishIds = [self.tracks.predFishId]; % use predFish. Can do Better ?!?
end
if nargin<2
force = 0;
end
%self.daGraph.checkOverlap([],1);
%trackIdx and trackids SHOULD be the same! (if with no deletion)
% at laest assert for last tracks (if 1:nfish, all previous should be too)
assert(all([self.tracks.id] == 1:self.nfish));
% backtrace.
[postrace,trackIdxMat] = self.daGraph.backtrace(1:self.nfish,predFishIds);
postrace = permute(postrace,[1,3,2]);
mt = size(postrace,3);
t = self.currentFrame-mt+1:self.currentFrame;
if force
self.fishId2TrackId(t,:) = trackIdxMat;
self.pos(:,:,t) = postrace;
else
pos = self.pos(:,:,1:self.currentFrame);
pos(:,:,t) = postrace;
postrace = pos;
f2t = self.fishId2TrackId(1:self.currentFrame,:);
f2t(t,:) = trackIdxMat;
trackIdxMat = f2t;
end
end
end