-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializeTracking.m
776 lines (548 loc) · 21.1 KB
/
initializeTracking.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
function initializeTracking(vid_path,v,currDataPath,method,varargin)
%% Parameters
% Currently only support grayscale mode
clrMode = 'gray';
% Predator & prey colors
pyClr = [41 171 226]./255;
pdClr = [241 90 36]./255;
% Level to brighten the movie
bLevel = 0.5;
%% Create data files for analysis by kineBox
% Check for path in data dir
if isempty(dir(currDataPath))
% Make directory, if not there
mkdir(currDataPath);
end
%% Preliminary questions
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
ButtonName = questdlg('Which kind of experiment?', ...
'','Single body', 'Pred-prey', 'Cancel', 'Single body');
if strcmp(ButtonName,'Single body')
iC.expType = 'single';
elseif strcmp(ButtonName,'Pred-prey')
iC.expType = 'pred prey';
else
return
end
% Frame rate & interval for analysis
if isfield(v,'FrameRate')
iC.frameRate = v.FrameRate;
answer = inputdlg({'Interval btwn frames for analysis (frames)'},'',1,{'1'});
% Interval between tracked frames
iC.frInterval = str2num(answer{1});
else
answer = inputdlg({'Interval btwn frames for analysis (frames)',...
'Frame rate (fps)'},'',1,{'1','29.97'});
iC.frInterval = str2num(answer{1});
iC.frameRate = str2num(answer{2});
end
% Manual analysis
bName = questdlg('Any intervals to analyze manually?', ...
'','Yes', 'No', 'Cancel', 'Yes');
if strcmp(bName,'Cancel') || isempty(bName)
return
elseif strcmp(bName,'Yes')
% Get number of intervals
answer = inputdlg({'How many intervals?'},'',1,{'1'});
numInt = str2num(answer{1});
% Start with empty data
iC.man(1).start = [];
iC.man(1).end = [];
if strcmp(iC.expType,'pred prey')
iC.man(2).start = [];
iC.man(2).end = [];
end
% Loop thru intervals
for i = 1:numInt
if strcmp(iC.expType,'pred prey')
answer = inputdlg({'Start frame?','End frame','Pred (P), Prey (E), or both (PE)?'},...
['Interval ' num2str(i)],1,{'','','P'});
% Log predator interval
if strcmp(answer{3},'P') || strcmp(answer{3},'p')
iC.man(1).start = [iC.man(1).start; str2num(answer{1})];
iC.man(1).end = [iC.man(1).end; str2num(answer{2})];
% Log prey interval
elseif strcmp(answer{3},'E') || strcmp(answer{3},'e')
iC.man(2).start = [iC.man(2).start; str2num(answer{1})];
iC.man(2).end = [iC.man(2).end; str2num(answer{2})];
% Log interval for both predator and prey
elseif strcmp(answer{3},'PE') || strcmp(answer{3},'pe')||...
strcmp(answer{3},'EP') || strcmp(answer{3},'ep')
iC.man(1).start = [iC.man(1).start; str2num(answer{1})];
iC.man(1).end = [iC.man(1).end; str2num(answer{2})];
iC.man(2).start = [iC.man(2).start; str2num(answer{1})];
iC.man(2).end = [iC.man(2).end; str2num(answer{2})];
else
error('Input not recognized');
end
else
answer = inputdlg({'Start frame?','End frame'},...
['Interval ' num2str(i)],1,{'','','P'});
% Log interval
iC.man(1).start = [iC.man(1).start; str2num(answer{1})];
iC.man(1).end = [iC.man(1).end; str2num(answer{2})];
end
end
elseif strcmp(bName,'No')
iC.man(1).start = nan;
iC.man(1).end = nan;
end
clear bName answer
end
%% Choose movie duration
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
% Make figure
f = figure;
% Default first and end frames
firstFrame = v.UserData.FirstFrame;
lastFrame = v.UserData.LastFrame;
% Loop for multiple tries at frame numbers
while true
% Get images
im1 = getFrame(vid_path,v,firstFrame,0,'rgb');
im2 = getFrame(vid_path,v,lastFrame,0,'rgb');
% Plot candidate frames
subplot(1,2,1)
imshow(im1,'InitialMag','fit')
title(['First frame (' num2str(firstFrame) ')'])
subplot(1,2,2)
imshow(im2,'InitialMag','fit')
title(['Last frame (' num2str(lastFrame) ')'])
% Ask for approval on frames
an = questdlg('Are these good starting and ending frames?','','Yes',...
'No','Cancel','Yes');
if strcmp(an,'Yes')
close(f)
break
elseif strcmp(an,'No')
prompt={'Start frame num:','Last frame num:'};
name='Choose clip duration';
numlines=1;
defaultanswer={num2str(firstFrame),num2str(lastFrame)};
answer = inputdlg(prompt,name,numlines,defaultanswer);
if isempty(answer)
return
end
firstFrame = str2num(answer{1});
lastFrame = str2num(answer{2});
else
return
end
end
% Store start and end frames
iC.startFrame = firstFrame;
iC.endFrame = lastFrame;
clear im1 im2 f prompt name numlines answer firstFrame lastFrame
end
%% Determine whether to invert image
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
im = getFrame(vid_path,v,iC.startFrame,0,'gray');
f = figure;
imshow(im,'InitialMag','fit')
ButtonName = questdlg('Is the foreground body dark or light?', ...
'','Dark', 'Light', 'Cancel', 'Dark');
if strcmp(ButtonName,'Dark')
iC.invert = 0;
elseif strcmp(ButtonName,'Light')
iC.invert = 1;
else
return
end
clear buttonName im
close(f)
end
%% Tank mask
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
im = getFrame(vid_path,v,iC.startFrame,iC.invert,'gray');
f = figure;
imshow(im,'InitialMag','fit')
ButtonName = questdlg('Were the experiments run in an arena?', ...
'','Yes, elliptical', 'Yes, rectangular','No', 'Yes, elliptical');
if strcmp(ButtonName,'Yes, elliptical')
% Boundaries of tank
disp(' ')
disp('Select elliptical boundaries of the tank')
disp(' ')
[iC.xTank,iC.yTank] = imInteract(im,'ellipse');
elseif strcmp(ButtonName,'Yes, rectangular')
% Boundaries of tank
disp(' ')
disp('Select rectangular boundaries of the tank')
disp(' ')
%TODO: Make this mode:
[iC.xTank,iC.yTank] = imInteract(im,'rectangle');
elseif strcmp(ButtonName,'No')
iC.xTank = [];
iC.yTank = [];
else
return
end
close(f)
clear buttonName im
end
%% Calculate mean image
if isempty(dir([currDataPath filesep 'meanImageData.mat']))
% Calculate mean image
imMean = motionImage(vid_path,v,'mean bright','none',iC.invert);
if iC.invert==1
imMean = imcomplement(imMean);
end
% Save mean image data
save([currDataPath filesep 'meanImageData'],'imMean');
else
% Load imMean
load([currDataPath filesep 'meanImageData.mat'])
end
%% Determine contrast difference between blobs and mean image
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
% Frames for plot
visFrames = round(linspace(iC.startFrame,iC.endFrame,9));
% Make figure
f = figure;
% Render 9 frames with current settings
for i = 1:9
% Get image, subtract around tank
im{i} = getFrame(vid_path,v,visFrames(i),iC.invert,'gray');
im{i} = applyMask(im{i},iC.xTank,iC.yTank);
subplot(3,3,i)
imshow(im{i},'InitialMag','fit')
title(['Frame ' num2str(visFrames(i))])
end
brighten(bLevel)
% Index for run thru loop
runNum = 1;
% Loop thru for each body
while true
% Starting value
propDiff = 0.15;
disp(' ')
if strcmp(iC.expType,'pred prey') && runNum==1
disp('PREDATOR ----------------------------')
clr = pdClr;
elseif strcmp(iC.expType,'pred prey')
disp('PREY --------------------------------')
clr = pyClr;
else
clr = [0 1 0];
end
disp('Interactively adjust proportional difference and area factor');
disp(' ')
disp('Up arrow : More inclusive in contrast')
disp('Down arrow : Less inclusive in contrast')
disp(' ')
while true
% Adjust blob areas
for i = 1:9
% Find blob at cX,cY
[props,bwOut] = findBlobs(im{i},imMean,propDiff,'all');
figure(f);
subplot(3,3,i)
hold on
% Make a truecolor all-green image, make non-blobs invisible
clrField = cat(3, clr(1).*ones(size(im{i})), ...
clr(2).*ones(size(im{i})), ...
clr(3).*ones(size(im{i})));
h(i) = imshow(clrField,'InitialMag','fit');
%brighten(bLevel)
set(h(i), 'AlphaData', bwOut)
hold off
end
% Interactive mode
[x,y,b] = ginput(1);
% Up arrow
if b==30
propDiff = propDiff/1.25;
% Down arrow
elseif b==31
propDiff = propDiff*1.25;
elseif isempty(b)
iC.propDiff(runNum,1) = propDiff;
break
end
disp([' propDiff = ' num2str(propDiff)])
delete(h)
end
% If first of 2 iterations
if strcmp(iC.expType,'pred prey') && runNum==1
runNum = 2;
% Otherwise, leave
else
close(f)
clear frames im
break
end
end
clear b bwOut clr clrField f h i
end
%% Animal position(s)
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
% First image
im = getFrame(vid_path,v,iC.startFrame,iC.invert,'rgb');
% Single body tracking -----------
if strcmp(method,'single body') || strcmp(method,'single body, with arms')
% Initial position
disp(' ')
disp('Select animal to be tracked')
[iC.x,iC.y] = imInteract(im,'points',1);
% Radius
disp(' ')
disp('Select roi radius')
iC.r = imInteract(im,'radius',iC.x,iC.y);
% Two body tracking (i.e. predator-prey) ---
elseif strcmp(method,'pred prey')
% Initial position of predator
disp(' ')
disp('Select predator centroid')
[iC.x,iC.y] = imInteract(im,'points',1);
% Radius for predator
disp(' ')
disp('Select roi radius for predator')
iC.r = imInteract(im,'radius',iC.x,iC.y);
% Initial position of prey
disp(' ')
disp('Select prey centroid')
[iC.x(2),iC.y(2)] = imInteract(im,'points',1);
% Radius for prey
disp(' ')
disp('Select roi radius for prey')
iC.r(2) = imInteract(im,'radius',iC.x(2),iC.y(2));
end
% Arms
if strcmp(method,'single body, with arms')
% Prompt for arms
answer = inputdlg({'Number of arms'},'',1,{'5'});
% Log response
iC.armNum = str2num(answer{1});
disp(' ')
disp('Select tip of arms centroid')
% Capture coordinates of arm tips
[iC.xArms,iC.yArms] = imInteract(im,'points',iC.armNum);
% Radius around mouth
disp(' ')
disp('Select radius around body area (without arms)')
iC.rMouth = imInteract(im,'radius',iC.x,iC.y);
end
clear im
end
%% Determine area range
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
% Frames for plot
visFrames = round(linspace(iC.startFrame,iC.endFrame,9));
% Make figure
f = figure;
% Render 9 frames with current settings
for i = 1:9
% Get image, subtract around tank
im{i} = getFrame(vid_path,v,visFrames(i),iC.invert,'gray');
im{i} = applyMask(im{i},iC.xTank,iC.yTank);
subplot(3,3,i)
imshow(im{i},'InitialMag','fit')
title(['Frame ' num2str(visFrames(i))])
end
brighten(bLevel)
% Index for run thru loop
runNum = 1;
% Loop thru for each body
while true
% Find blob at cX,cY
[propBod,bwOut] = findBlobs(im{1},imMean,propDiff,'coord advanced',...
iC.x(runNum),iC.y(runNum));
bodArea = propBod.Area;
% Default area range
minArea = bodArea/4;
maxArea = bodArea*10;
disp(' ')
if strcmp(iC.expType,'pred prey') && runNum==1
disp('PREDATOR ----------------------------')
clr = pdClr;
elseif strcmp(iC.expType,'pred prey')
disp('PREY --------------------------------')
clr = pyClr;
else
clr = [0 1 0];
end
disp('Interactively adjust area bounds');
disp(' ')
disp('Up arrow : Increase max area')
disp('Down arrow : Decrease max area')
disp('Right arrow : Increase min area')
disp('Left arrow : Decrease min area')
disp(' ')
while true
% Adjust green areas
for i = 1:9
% Find blob at cX,cY
[props,bwOut] = findBlobs(im{i},imMean,propDiff,'all',...
minArea,maxArea);
figure(f);
subplot(3,3,i)
hold on
% Make a truecolor all-green image, make non-blobs invisible
clrField = cat(3, clr(1).*ones(size(im{i})), ...
clr(2).*ones(size(im{i})), ...
clr(3).*ones(size(im{i})));
h(i) = imshow(clrField,'InitialMag','fit');
%brighten(bLevel)
set(h(i), 'AlphaData', bwOut)
hold off
end
% Interactive mode
[x,y,b] = ginput(1);
% Up arrow
if b==30
maxArea = round(maxArea + 0.2*maxArea);
% Down arrow
elseif b==31
maxArea = max([1 round(maxArea - 0.2*maxArea)]);
% Left arrow
elseif b==28
minArea = max([1 round(minArea - 0.2*bodArea)]);
% Right arrow
elseif b==29
minArea = round(minArea + 0.2*bodArea);
elseif isempty(b)
iC.minArea(runNum,1) = minArea;
iC.maxArea(runNum,1) = maxArea;
break
end
disp(['Body area = ' num2str(bodArea) ' min = ' num2str(minArea) ' max = ' num2str(maxArea)])
delete(h)
end
% If first of 2 iterations
if strcmp(iC.expType,'pred prey') && runNum==1
runNum = 2;
% Otherwise, leave
else
close(f)
clear frames im
break
end
end
clear runNum minArea maxArea h i x y b clr props bwOut visFrames
end
%% Calibration
if isempty(dir([currDataPath filesep 'Initial conditions.mat']))
% First image
im = getFrame(vid_path,v,iC.startFrame,iC.invert,'rgb');
f = figure;
imshow(im,'InitialMag','fit')
ButtonName = questdlg('Are there calibration landmarks in view?', ...
'','Yes', 'No', 'Cancel', 'Yes');
if strcmp(ButtonName,'Yes')
% Calibration
disp(' ')
disp('Select two points for spatial calibration')
[xCal,yCal] = imInteract(im,'points',2);
answer = inputdlg('Distance between 2 points (m)?','',1,{'0.5'});
iC.xCalPts = xCal;
iC.yCalPts = yCal;
iC.calconst = str2num(answer{1}) / hypot(diff(xCal),diff(yCal));
elseif strcmp(ButtonName,'No')
iC.xCal = [];
iC.yCal = [];
else
return
end
clear ButtonName xCal yCal answer
close(f)
end
%% Load/save initial conditions, if present
if ~isempty(dir([currDataPath filesep 'Initial conditions.mat']))
load([currDataPath filesep 'Initial conditions'])
else
% Save initial conditions
save([currDataPath filesep 'Initial conditions'],'iC')
end
%% Make data files
if strcmp(method,'single body') && isempty(dir([currDataPath filesep 'Body course.mat']))
% Make empty Centroid structure
Body.frames = [iC.startFrame:iC.endFrame]';
Body.tform{1} = affine2d(eye(3));
Body.x = nan(size(Body.frames));
Body.y = nan(size(Body.frames));
Body.ang = nan(size(Body.frames));
%Body.y_flip = nan(size(Body.frames));
% Initial values
Body.x(1) = iC.x;
Body.y(1) = iC.y;
Body.ang(1) = 0;
% Save data
save([currDataPath filesep 'Body'],'Body')
elseif strcmp(method,'body arm') && ...
isempty(dir([currDataPath filesep 'Body arm.mat']))
% Make empty Centroid structure
Body.frames = [iC.startFrame:iC.endFrame]';
Body.tform{1} = affine2d(eye(3));
Body.x = nan(size(Body.frames));
Body.y = nan(size(Body.frames));
Body.ang = nan(size(Body.frames));
%Body.y_flip = nan(size(Body.frames));
% Initial values
Body.x(1) = iC.x;
Body.y(1) = iC.y;
Body.ang(1) = 0;
% Save data
save([currDataPath filesep 'Body arm'],'Body')
% Two body tracking (i.e. predator-prey) ---
elseif strcmp(method,'pred prey') && ...
isempty(dir([currDataPath filesep 'Body.mat']))
% Make empty Centroid structure
Body.frames = [iC.startFrame:iC.endFrame]';
Body.tform{1,1} = affine2d(eye(3));
Body.tform{1,2} = affine2d(eye(3));
Body.x = nan(length(Body.frames),2);
Body.y = nan(length(Body.frames),2);
Body.ang = nan(length(Body.frames),2);
%Body.y_flip = nan(size(Body.frames));
% Initial values
Body.x(1,:) = iC.x;
Body.y(1,:) = iC.y;
Body.ang = [0 0];
% Save data
save([currDataPath filesep 'Body'],'Body')
end
function clipInfo = selectDuration(vid_path,v)
% Interactively prompts to select a duration for analysis
% Make figure
f = figure;
% Default first and end frames
firstFrame = v.UserData.FirstFrame;
lastFrame = v.UserData.LastFrame;
% Loop for multiple tries at frame numbers
while true
% Get images
%im1 = getFrame(vid_path,v,firstFrame,0,'green screen',imMean);
%im1 = getFrame(vid_path,v,firstFrame,0,'rgb',imMean);
im1 = getFrame(vid_path,v,firstFrame,0,'rgb');
im2 = getFrame(vid_path,v,lastFrame,0,'rgb');
%im2 = getFrame(vid_path,v,lastFrame,0,'gray',imMean);
% Plot candidate frames
subplot(1,2,1)
imshow(im1,'InitialMag','fit')
title(['First frame (' num2str(firstFrame) ')'])
subplot(1,2,2)
imshow(im2,'InitialMag','fit')
title(['Last frame (' num2str(lastFrame) ')'])
% Ask for approval on frames
an = questdlg('Are these good starting and ending frames?','','Yes',...
'No','Cancel','Yes');
if strcmp(an,'Yes')
close(f)
break
elseif strcmp(an,'No')
prompt={'Start frame num:','Last frame num:'};
name='Choose clip duration';
numlines=1;
defaultanswer={num2str(firstFrame),num2str(lastFrame)};
answer = inputdlg(prompt,name,numlines,defaultanswer);
if isempty(answer)
return
end
firstFrame = str2num(answer{1});
lastFrame = str2num(answer{2});
else
return
end
end
clipInfo.startFrame = firstFrame;
clipInfo.endFrame = lastFrame;