forked from kristinbranson/FlyTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.m
3289 lines (3071 loc) · 120 KB
/
visualizer.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Visualize tracked videos, correct identities, and annotate behavior.
%
% To run interface, use:
%
% visualizer
%
function visualizer()
% files
files.video_dir = '.'; % default video directory
parentdir = fileparts(mfilename('fullpath'));
defpath = fullfile(parentdir,'action_list.txt');
files.action_list_path = defpath; % contains default list of actions
% add tracker to path if its not there already
check = which('is_atomic_detection');
if isempty(check)
addpath(genpath(parentdir));
end
% data
vinfo = []; % video data
trk = []; % tracking data
seg = []; % segmentation data
feat = []; % feature data
actions = []; % action data
swap = []; % swap data
% other variables
fig = []; % figure variables
state = []; % state variables
bool = []; % boolean variables
quest = []; % questions and answers
% initialize variables
initVars();
% interface handles
handles = [];
% initialize interface
initInterface();
beep off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Interface setup functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function initInterface()
% --- MAIN WINDOW
scrsz = get(0,'ScreenSize');
sub_width = 600;
sub_height = sub_width/4*3;
fig_height = sub_height + 300;
fig_width = sub_width + 400;
limit = scrsz(3:4)*.9;
scale = 1;
if fig_width > limit(1) || fig_height > limit(2)
scale = min(limit(1)/fig_width, limit(2)/fig_height);
fig_height = round(fig_height*scale);
fig_width = round(fig_width*scale);
end
sub_height = round(sub_height*scale);
fig.sub_height = round(sub_height*scale);
fig.sub_width = round(sub_width*scale);
fig.scale = scale;
fig.height = fig_height;
fig.width = fig_width;
figure_name = sprintf('FlyTracker-%s: Visualizer', flytracker_version_string()) ;
fig_h = figure('Name',figure_name,'NumberTitle','off',...
'Position',[scrsz(3)/2-fig_width/2 scrsz(4)/2-fig_height/2 fig_width fig_height],...
'Resize','on','Menubar', 'none',...
'WindowButtonDownFcn',@mousePressed,...
'WindowButtonUpFcn',@mouseReleased,...
'KeyPressFcn',@keyPressed,...
'Color',.94*[1 1 1]);
set(fig_h,'CloseRequestFcn',@myClose);
fs = 72/get(0,'ScreenPixelsPerInch'); % scale fonts to resemble the mac
fs = fs*scale;
fig.fs = fs;
figclr = get(fig_h,'color');
fig.color = figclr;
fig.zoom_imgs = getIcons(figclr);
% Menuitems (Video, Track, GroundTruth, Prediction)
vid_h = uimenu(fig_h,'Label','Video');
uimenu(vid_h,'Label','Open','Callback',@openVideo);
trk_h = uimenu(fig_h,'Label','Track','enable','off');
uimenu(trk_h,'Label','Open','Callback',@openTrk);
trkxls_h = uimenu(trk_h,'Label','Save as xls','enable','off','Callback',@saveTrkXls);
gtm_h = uimenu(fig_h,'Label','GroundTruth','enable','off');
uimenu(gtm_h,'Label','Open','Callback',@openGTlabel);
uimenu(gtm_h,'Label','New','Callback',@newGTlabel);
gtxls_h = uimenu(gtm_h,'Label','Save as xls','enable','off','Callback',@saveGTXls);
predm_h = uimenu(fig_h,'Label','Prediction','enable','off');
uimenu(predm_h,'Label','Open','Callback',@openPREDlabel);
predxls_h = uimenu(predm_h,'Label','Save as xls','enable','off','Callback',@savePredXls);
% store handles
handles.fig_h = fig_h;
handles.trk_h = trk_h;
handles.gtm_h = gtm_h;
handles.predm_h = predm_h;
handles.trkxls_h = trkxls_h;
handles.gtxls_h = gtxls_h;
handles.predxls_h = predxls_h;
% --- LEFT PANEL
% Image window
m_v = 30*scale; % vertical margin
m_h = 50*scale; % horizontal margin
sub_h = subplot('Position',[m_h/fig_width 1-(m_v+sub_height)/fig_height fig.sub_width/fig_width sub_height/fig_height]);
imshowAxesVisible = iptgetpref('ImshowAxesVisible');
iptsetpref('ImshowAxesVisible','on')
imshow(ones(sub_height,fig.sub_width)*.9);
set(sub_h,'box','on','XTickLabel',[], 'YTickLabel',[], 'tickLength',[0 0]);
iptsetpref('ImshowAxesVisible',imshowAxesVisible)
hold on
colormap(gray)
% display video path
position = [m_h fig_height-m_v*.75 fig.sub_width m_v/2];
videopath_h = addui('text',' ',12*fs,position);
% - chamber zoom in
position = [m_h-22*scale fig_height-m_v-19*scale 20*scale 20*scale];
chamber_h(1) = addui('pushbutton','+',14*fs,position,@chamberZoomIn);
set(chamber_h(1),'tooltip','View only current chamber','visible','off');
% - chamber zoom out
position = [m_h-22*scale fig_height-m_v-41*scale 20*scale 20*scale];
chamber_h(2) = addui('pushbutton','-',14*fs,position,@chamberZoomOut);
set(chamber_h(2),'tooltip','View all chambers','visible','off');
% Navigation slider
pos_h = m_h;
pos_v = fig_height-sub_height-m_v-18*scale;
position = [pos_h pos_v fig.sub_width+2*scale 15*scale];
slider_h = uicontrol(fig_h,'style','slider','units','normalized',...
'position',position./[fig_width fig_height fig_width fig_height],...
'Min',0,'Max',1,'value',0,'backgroundcolor',figclr,...
'callback',@vidSlider);
video_h(1) = slider_h;
% Navigation controls
pos_v = pos_v - 27*scale;
position = [pos_h pos_v 50*scale 25*scale];
video_play_h = addui('pushbutton','play',12*fs,position,@playVid,'left');
set(video_play_h,'backgroundcolor',[.3 .7 .4])
video_h(end+1) = video_play_h;
pos_h = pos_h+65*scale;
position = [pos_h pos_v 60*scale 25*scale];
video_frame_h = addui('edit','1',12*fs,position,@goToFrame);
video_h(end+1) = video_frame_h;
position = [pos_h+60*scale pos_v 20*scale 20*scale];
video_h(end+1) = addui('text','/',12*fs,position);
position = [pos_h+80*scale pos_v 70*scale 20*scale];
video_nfr_h = addui('text','0',12*fs,position);
set(video_nfr_h,'horizontalalignment','left');
video_h(end+1) = video_nfr_h;
position = [pos_h+150*scale pos_v 70*scale 20*scale];
video_h(end+1) = addui('text','framerate:',12*fs,position);
position = [pos_h+220*scale pos_v 50*scale 25*scale];
framerate_h = addui('edit',num2str(state.framerate),12*fs,position,@setFramerate);
video_h(end+1) = framerate_h;
position = [pos_h+330*scale pos_v 75*scale 25*scale];
jump_button_h = addui('pushbutton','undo jump',12*fs,position,@goBackToFrame,'left');
set(jump_button_h,'visible','off','backgroundcolor',figclr*.9);
video_h(end+1) = jump_button_h;
for i=1:numel(video_h)
set(video_h(i),'enable','off')
end
% Pan and zoom buttons
pos_h = m_h + fig.sub_width - 70*scale;
pos_v = pos_v + 5*scale;
% - zoom in
position = [pos_h pos_v 20*scale 20*scale];
zoom_h(1) = addui('pushbutton','',1,position,@zoomIn);
set(zoom_h(1),'cdata',fig.zoom_imgs{1});
% - zoom out
position = [pos_h+26*scale pos_v 20*scale 20*scale];
zoom_h(2) = addui('pushbutton','',1,position,@zoomOut);
set(zoom_h(2),'cdata',fig.zoom_imgs{2});
% - pan
position = [pos_h+26*2*scale pos_v 20*scale 20*scale];
zoom_h(3) = addui('pushbutton','',1,position,@panFunc);
set(zoom_h(3),'cdata',fig.zoom_imgs{3});
% GT and pred visualization
pos_v = pos_v - 40*scale;
position = [1 pos_v m_h-5*scale 20*scale];
gtl_h = addui('text','GT:',12*fs,position);
set(gtl_h,'enable','off');
gt_h = subplot('Position',[m_h/fig_width pos_v/fig_height fig.sub_width/fig_width 20*scale/fig_height]);
imshowAxesVisible = iptgetpref('ImshowAxesVisible');
iptsetpref('ImshowAxesVisible','on')
image(ones(1,fig.sub_width)*.9*255);
set(gt_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
iptsetpref('ImshowAxesVisible',imshowAxesVisible)
pos_v = pos_v - 40*scale;
position = [1 pos_v m_h-5*scale 20*scale];
predl_h = addui('text','Pred.:',12*fs,position);
set(predl_h,'enable','off');
pred_h = subplot('Position',[m_h/fig_width pos_v/fig_height fig.sub_width/fig_width 20*scale/fig_height]);
imshowAxesVisible = iptgetpref('ImshowAxesVisible');
iptsetpref('ImshowAxesVisible','on')
image(ones(1,fig.sub_width)*.9*255);
set(pred_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
iptsetpref('ImshowAxesVisible',imshowAxesVisible)
% Feature
pos_v = pos_v - 120*scale;
feat_h = subplot('Position',[m_h/fig_width pos_v/fig_height fig.sub_width/fig_width 80*scale/fig_height]);
imshowAxesVisible = iptgetpref('ImshowAxesVisible');
iptsetpref('ImshowAxesVisible','on')
image(ones(1,fig.sub_width)*.9*255);
set(feat_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
iptsetpref('ImshowAxesVisible',imshowAxesVisible)
% - feature dropdown list
position = [m_h-6*scale pos_v+72*scale 170*scale 30*scale];
featl_h = addui('popup','Feature',12*fs,position,@setFeat);
set(featl_h,'enable','off');
% - current feature value
position = [m_h+fig.sub_width/2-20*scale 10*scale 40*scale 15*scale];
feat_val_h = addui('text','value',11*fs,position);
set(feat_val_h,'enable','off');
% help annotating functions
pos_h = 220*scale;
pos_v = pos_v + 83*scale;
position = [pos_h pos_v 145*scale 20*scale];
help_anno_h = addui('checkbox','apply threshold',11*fs,position,@needHelpRadiobutton);
set(help_anno_h,'value',0,'enable','off');
% feat search help
pos_h = pos_h + 140*scale;
position = [pos_h pos_v 55*scale 18*scale];
feat_nav_h = addui('text','condition:',11*fs,position,[],'left');
pos_h = pos_h + 57*scale;
position = [pos_h pos_v 60*scale 20*scale];
feat_cond_h = addui('popup','>|<',12*fs,position,@setCondition,'left');
feat_nav_h(end+1) = feat_cond_h;
pos_h = pos_h + 70*scale;
position = [pos_h pos_v 60*scale 18*scale];
feat_nav_h(end+1) = addui('text','threshold:',11*fs,position,[],'left');
pos_h = pos_h + 60*scale;
position = [pos_h pos_v 40*scale 20*scale];
feat_thresh_h = addui('edit','0',11*fs,position,@threshEdit);
feat_nav_h(end+1) = feat_thresh_h;
pos_h = pos_h + 42*scale;
position = [pos_h pos_v 30*scale 20*scale];
feat_nav_h(end+1) = addui('pushbutton','<<',12*fs,position,@findPrevButton);
pos_h = pos_h + 32*scale;
position = [pos_h pos_v 30*scale 20*scale];
feat_nav_h(end+1) = addui('pushbutton','>>',12*fs,position,@findNextButton);
for i=1:numel(feat_nav_h)
set(feat_nav_h(i),'visible','off');
end
% vertical menu divider
pos_h = fig.sub_width+m_h+30*scale;
position = [pos_h 1 1 fig_height];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',.5*[1 1 1]);
% store handles
handles.sub_h = sub_h;
handles.videopath_h = videopath_h;
handles.chamber_h = chamber_h;
handles.slider_h = slider_h;
handles.video_h = video_h;
handles.video_play_h = video_play_h;
handles.video_frame_h = video_frame_h;
handles.video_nfr_h = video_nfr_h;
handles.framerate_h = framerate_h;
handles.jump_button_h = jump_button_h;
handles.zoom_h = zoom_h;
handles.gt_h = gt_h;
handles.gtl_h = gtl_h;
handles.pred_h = pred_h;
handles.predl_h = predl_h;
handles.feat_h = feat_h;
handles.featl_h = featl_h;
handles.feat_val_h = feat_val_h;
% --- RIGHT PANEL
% VISUALS
% - title
panel_h = pos_h + 1;
panel_width = fig_width-pos_h;
pos_v = fig_height-35*scale;
% set background to make vertical divider is only one pixel
position = [pos_h+1 1 panel_width-1 fig_height];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr);
position = [panel_h pos_v panel_width 36*scale];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr*.93);
position = [panel_h+15*scale pos_v+3*scale 170*scale 25*scale];
uicontrol('Style', 'text', 'String', 'Display settings', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'HorizontalAlignment', 'left','BackgroundColor', figclr*.93, 'FontSize',fs*16);
% - display options
track_h = [];
pos_h = panel_h + 20*scale;
pos_v = pos_v - 65*scale;
buff = 20*scale;
position = [pos_h+5*scale pos_v+buff+2*scale 260*scale 1];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr*.9);
position = [pos_h+5*scale pos_v+buff+3*scale 260*scale 1];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr);
position = [pos_h+10*scale pos_v 140*scale 20*scale];
show_track_h = addui('checkbox','show tracks',13*fs,position,@showAll);
track_h(end+1) = show_track_h;
position = [pos_h+5*scale pos_v-3*scale 135*scale 1];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr*.9);
position = [pos_h+4*scale pos_v-4*scale 135*scale 1];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr);
position = [pos_h+140*scale pos_v-64*scale 1 61*scale];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr*.9);
position = [pos_h+141*scale pos_v-64*scale 1 61*scale];
uicontrol(fig_h,'style','text','string','','units','normalized',...
'position', position./[fig_width fig_height fig_width fig_height],...
'backgroundColor',figclr);
position = [pos_h+150*scale pos_v 140*scale 20*scale];
show_separate_h = addui('checkbox','ellipse',13*fs,position,@showEllipse);
position = [pos_h+150*scale pos_v-buff 140*scale 20*scale];
show_separate_h(end+1) = addui('checkbox','trail',13*fs,position,@showTrail);
position = [pos_h+150*scale pos_v-2*buff 140*scale 20*scale];
show_separate_h(end+1) = addui('checkbox','wings',13*fs,position,@showWings);
position = [pos_h+150*scale pos_v-3*buff 140*scale 20*scale];
show_separate_h(end+1) = addui('checkbox','legs',13*fs,position,@showLegs);
track_h = [track_h show_separate_h];
position = [pos_h+10*scale pos_v+1.3*buff 140*scale 20*scale];
show_img_h = addui('checkbox','show image',13*fs,position,@showImg);
track_h(end+1) = show_img_h;
position = [pos_h+120*scale pos_v+1.3*buff 150*scale 20*scale];
show_seg_h = addui('checkbox','show segmentation',13*fs,position,@showSeg);
set(show_seg_h,'visible','off');
track_h(end+1) = show_seg_h;
pos_v = pos_v - 35*scale;
position = [pos_h+12*scale pos_v 80*scale 20*scale];
track_h(end+1) = addui('text','Active fly id:',13*fs,position,[],'left');
position = [pos_h+92*scale pos_v 30*scale 20*scale];
active_fly_h = addui('text','1',13*fs,position,[],'left');
track_h(end+1) = active_fly_h;
position = [pos_h+12*scale pos_v-20*scale 100*scale 20*scale];
auto_zoom_h = addui('checkbox','auto zoom',13*fs,position,@autoZoom);
track_h(end+1) = auto_zoom_h;
% TRACKING CONTROLS
% - title
pos_v = pos_v - 95*scale;
position = [panel_h pos_v+36*scale panel_width 1];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',.5*[1 1 1]);
position = [panel_h pos_v panel_width 36*scale];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr*.93);
position = [panel_h+15*scale pos_v+3*scale 170*scale 25*scale];
uicontrol('Style', 'text', 'String', 'Identity correction','units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'HorizontalAlignment', 'left','BackgroundColor', figclr*.93, ...
'FontSize',fs*16);
position = [panel_h+(panel_width-40)*scale pos_v+3*scale 40*scale 25*scale];
id_hot_h = uicontrol('Style','text','String','*',...
'units','normalized','FontSize',fs*30,'visible','off',...
'Position',position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr*.93,'ForegroundColor',[1 1 1],...
'ToolTip','Hot keys: arrowup (next), arrowdown (prev), spacebar (play), ''s'' (swap)');
% - potential identity swaps
pos_v = pos_v - 60*scale;
position = [pos_h pos_v panel_width 30*scale];
track_h(end+1) = addui('text','Potential id swaps:',13*fs,position,[],'left');
position = [pos_h+123*scale pos_v+12*scale 157*scale 20*scale];
switch_sort_h = addui('popup','sort by frame|sort by ambiguity',12*fs,position,@switchSortBy);
track_h(end+1) = switch_sort_h;
pos_v = pos_v - 25*scale;
position = [pos_h+10*scale pos_v 15*scale 20*scale];
track_h(end+1) = addui('pushbutton','<',12*fs,position,@idSwitchDown);
position = [pos_h+30*scale pos_v-2*scale 35*scale 25*scale];
swap_id_h = addui('edit','0',12*fs,position,@setIdSwitch);
track_h(end+1) = swap_id_h;
position = [pos_h+70*scale pos_v 15*scale 20*scale];
track_h(end+1) = addui('pushbutton','>',12*fs,position,@idSwitchUp);
position = [pos_h+86*scale pos_v 15*scale 20*scale];
track_h(end+1) = addui('text','/',12*fs,position);
position = [pos_h+100*scale pos_v 35*scale 20*scale];
nswaps_h = addui('text','0',12*fs,position,[],'left');
track_h(end+1) = nswaps_h;
position = [pos_h+130*scale pos_v-2*scale 65*scale 25*scale];
play_swap_h = addui('pushbutton','play clip',12*fs,position,{@playBout,'swap'});
set(play_swap_h,'backgroundcolor',[.3 .7 .4]);
track_h(end+1) = play_swap_h;
position = [pos_h+210*scale pos_v-2*scale 60*scale 25*scale];
do_swap_h = addui('pushbutton','swap',12*fs,position,@swapIds);
set(do_swap_h,'tooltipstring',' Swap identities at current frame ',...
'backgroundcolor',[.5 .65 .9]);
track_h(end+1) = do_swap_h;
pos_v = pos_v - 55*scale;
position = [pos_h pos_v fig_width-pos_h-20*scale 35*scale];
save_swaps_h = addui('pushbutton','save changes',12*fs,position,@saveTrk);
track_h(end+1) = save_swaps_h;
for i=1:numel(track_h)
set(track_h(i),'enable','off')
end
% store handles
handles.track_h = track_h;
handles.show_track_h = show_track_h;
handles.show_separate_h = show_separate_h;
handles.show_seg_h = show_seg_h;
handles.show_img_h = show_img_h;
handles.active_fly_h = active_fly_h;
handles.auto_zoom_h = auto_zoom_h;
handles.swap_id_h = swap_id_h;
handles.nswaps_h = nswaps_h;
handles.play_swap_h = play_swap_h;
handles.save_swaps_h = save_swaps_h;
handles.do_swap_h = do_swap_h;
handles.switch_sort_h = switch_sort_h;
handles.id_hot_h = id_hot_h;
% BEHAVIOR CONTROLS
behavior_h = [];
% - title
pos_v = pos_v - 75*scale;
position = [panel_h pos_v+36*scale panel_width 1];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',.5*[1 1 1]);
position = [panel_h pos_v panel_width 36*scale];
uicontrol('Style', 'text', 'String', '', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr*.93);
position = [panel_h+15*scale pos_v+3*scale 190*scale 25*scale];
uicontrol('Style', 'text', 'String', 'Behavior annotation', 'units','normalized',...
'Position', position./[fig_width fig_height fig_width fig_height],...
'HorizontalAlignment', 'left', 'BackgroundColor', figclr*.93, ...
'FontSize',fs*16);
position = [panel_h+(panel_width-40)*scale pos_v+3*scale 40*scale 25*scale];
beh_hot_h = uicontrol('Style','text','String','*',...
'units','normalized','FontSize',fs*30,'visible','off',...
'Position',position./[fig_width fig_height fig_width fig_height],...
'BackgroundColor',figclr*.93,'ForegroundColor',[1 1 1],...
'ToolTip','Hot keys: arrowup (next), arrowdown (prev), spacebar (play), ''a'' (add), ''d'' (delete)');
% - select behavior
pos_v = pos_v - 60*scale;
position = [pos_h pos_v 70*scale 30*scale];
behavior_h(end+1) = addui('text','Action:',13*fs,position,[],'left');
position = [pos_h+75*scale pos_v 150*scale 30*scale];
action_h = addui('popup','all|touch|lunge|wing threat|wing extension',...
12*fs,position,@setBeh,'left');
behavior_h(end+1) = action_h;
pos_v = pos_v - 30*scale;
position = [pos_h pos_v 100*scale 30*scale];
behavior_h(end+1) = addui('text','Bout type:',13*fs,position,[],'left');
position = [pos_h+75*scale pos_v 150*scale 30*scale];
bout_type_h = addui('popup','ground truth|prediction|false postives|false negatives',...
12*fs,position,@setBehType,'left');
behavior_h(end+1) = bout_type_h;
% - go to bout
pos_v = pos_v - 30*scale;
position = [pos_h+10*scale pos_v 15*scale 20*scale];
behavior_h(end+1) = addui('pushbutton','<',12*fs,position,@boutDown);
position = [pos_h+30*scale pos_v-2*scale 35*scale 25*scale];
bout_id_h = addui('edit','0',12*fs,position,@setBout);
behavior_h(end+1) = bout_id_h;
position = [pos_h+70*scale pos_v 15*scale 20*scale];
behavior_h(end+1) = addui('pushbutton','>',12*fs,position,@boutUp);
position = [pos_h+86*scale pos_v-2*scale 15*scale 20*scale];
behavior_h(end+1) = addui('text','/',12*fs,position);
position = [pos_h+100*scale pos_v-2*scale 35*scale 20*scale];
nbouts_h = addui('text','0',12*fs,position,[],'left');
behavior_h(end+1) = nbouts_h;
position = [pos_h+130*scale pos_v-2*scale 65*scale 25*scale];
play_bout_h = addui('pushbutton','play clip',12*fs,position,{@playBout,'beh'});
set(play_bout_h,'backgroundcolor',[.3 .7 .4]);
behavior_h(end+1) = play_bout_h;
bout_nav_h = behavior_h;
% bout manipulation
pos_v = pos_v - 55*scale;
% - add bout
buttonsz = 65*scale;
buff = 4*scale;
position = [pos_h pos_v buttonsz+6*scale 30*scale];
bout_manip_h(1) = addui('pushbutton','ADD NEW',11*fs,position,@addBout);
set(bout_manip_h(1),'foregroundcolor',[0 .7 0]);
% - delete bout
position = [pos_h+buttonsz+buff+6*scale pos_v buttonsz 30*scale];
bout_manip_h(2) = addui('pushbutton','DELETE',11*fs,position,@deleteBout);
set(bout_manip_h(2),'foregroundcolor',[.9 0 0]);
% - move to other
position = [pos_h+buttonsz*2+2*buff+6*scale pos_v buttonsz 30*scale];
bout_manip_h(3) = addui('pushbutton','MOVE',11*fs,position,@moveBout);
set(bout_manip_h(3),'foregroundcolor',[0 0 .9]);
% - copy to other
position = [pos_h+buttonsz*3+3*buff+6*scale pos_v buttonsz 30*scale];
bout_manip_h(4) = addui('pushbutton','COPY',11*fs,position,@copyBout);
set(bout_manip_h(4),'foregroundcolor',[.9 .7 0]);
bout_add_h = bout_manip_h(1);
bout_move_h = bout_manip_h(3);
bout_copy_h = bout_manip_h(4);
behavior_h(end+(1:4)) = bout_manip_h;
% select certainty of annotation
pos_v = pos_v - 40*scale;
position = [pos_h+2*scale pos_v-7*scale 70*scale 30*scale];
cert_str_h = addui('text','Certainty:',12*fs,position,[],'left');
position = [pos_h+70*scale pos_v 70*scale 30*scale];
cert_maybe_h = addui('radiobutton','maybe',10*fs,position,@radioMaybe);
position = [pos_h+135*scale pos_v 70*scale 30*scale];
cert_prob_h = addui('radiobutton','probably',10*fs,position,@radioProbably);
set(cert_prob_h,'value',1);
position = [pos_h+200*scale pos_v 70*scale 30*scale];
cert_def_h = addui('radiobutton','definitely',10*fs,position,@radioDefinitely);
cert_h = [cert_str_h cert_maybe_h cert_prob_h cert_def_h];
behavior_h = [behavior_h cert_h];
% save bouts function
pos_v = pos_v - 50*scale;
position = [pos_h pos_v fig_width-pos_h-20*scale 35*scale];
save_anno_h = addui('pushbutton','save changes',12*fs,position,@saveLabel);
behavior_h(end+1) = save_anno_h;
% disable behavior components until label files are loaded
for i=1:numel(behavior_h)
set(behavior_h(i),'enable','off')
end
% store handles
handles.behavior_h = behavior_h;
handles.action_h = action_h;
handles.bout_type_h = bout_type_h;
handles.bout_id_h = bout_id_h;
handles.nbouts_h = nbouts_h;
handles.bout_nav_h = bout_nav_h;
handles.bout_manip_h = bout_manip_h;
handles.bout_add_h = bout_add_h;
handles.bout_move_h = bout_move_h;
handles.bout_copy_h = bout_copy_h;
handles.play_bout_h = play_bout_h;
handles.cert_str_h = cert_str_h;
handles.cert_maybe_h = cert_maybe_h;
handles.cert_prob_h = cert_prob_h;
handles.cert_def_h = cert_def_h;
handles.cert_h = cert_h;
handles.save_anno_h = save_anno_h;
handles.help_anno_h = help_anno_h;
handles.feat_nav_h = feat_nav_h;
handles.feat_cond_h = feat_cond_h;
handles.feat_thresh_h = feat_thresh_h;
handles.beh_hot_h = beh_hot_h;
% figure handles
handles.img_h = [];
handles.plot_h = [];
end
function h = addui(style,string,fontsz,position,callback,halign)
figsz = [fig.width fig.height fig.width fig.height];
h = uicontrol('style',style,'string',string,'fontsize',fontsz,...
'units','normalized','position',position./figsz,...
'backgroundcolor',fig.color,...
'horizontalalignment','center');
if nargin > 4 && ~isempty(callback)
set(h,'callback',callback);
end
if nargin > 5 && ~isempty(halign)
set(h,'horizontalalignment',halign);
end
end
function resetVars()
% reset files
newfiles.video_dir = files.video_dir;
newfiles.action_list_path = files.action_list_path;
files = newfiles;
% reset data
trk = [];
seg = [];
feat = [];
actions = [];
swap = [];
% reset interface handles
% - xls handles
set(handles.trkxls_h,'enable','off');
set(handles.gtxls_h,'enable','off');
set(handles.predxls_h,'enable','off');
% - feature navigation
set(handles.featl_h,'value',1,'string','Feature','enable','off');
set(handles.gtl_h,'enable','off');
set(handles.predl_h,'enable','off');
set(handles.feat_val_h,'string','value','enable','off');
set(handles.help_anno_h,'value',0);
set(handles.help_anno_h,'enable','off');
for j=1:numel(handles.feat_nav_h)
set(handles.feat_nav_h(j),'visible','off');
end
% - swaps
set(handles.id_hot_h,'Visible','off');
set(handles.show_track_h,'value',1);
for j=1:numel(handles.show_separate_h)
set(handles.show_separate_h(j),'value',1);
end
set(handles.show_seg_h,'value',0);
set(handles.show_img_h,'value',1);
set(handles.active_fly_h,'string',1);
set(handles.auto_zoom_h,'value',0);
set(handles.switch_sort_h,'value',1);
for j=1:numel(handles.track_h)
set(handles.track_h(j),'enable','off')
end
% - actions
set(handles.beh_hot_h,'Visible','off');
set(handles.action_h,'value',1);
set(handles.bout_type_h,'value',1);
for j=1:numel(handles.behavior_h)
set(handles.behavior_h(j),'enable','off')
end
% clear subplots
imshowAxesVisible = iptgetpref('ImshowAxesVisible');
iptsetpref('ImshowAxesVisible','on')
% gt
subplot(handles.gt_h)
hold off; cla
image(ones(1,fig.sub_width)*.9*255);colormap gray
set(handles.gt_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
% pred
subplot(handles.pred_h)
hold off; cla
image(ones(1,fig.sub_width)*.9*255);colormap gray
set(handles.pred_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
% feat
subplot(handles.feat_h)
hold off; cla
image(ones(1,fig.sub_width)*.9*255);colormap gray
set(handles.feat_h,'box','on','XTick',[],'YTick',[],'tickLength',[0 0]);
iptsetpref('ImshowAxesVisible',imshowAxesVisible)
end
function initVars()
% figure variables
fig.clrs = [];
fig.sub_ax = [];
% state variables
state.curr_frame = 1;
state.prev_frame = 1;
state.curr_feat = 1;
state.curr_beh = 1;
state.curr_beh_type = 1;
state.curr_chamber = 1;
state.curr_flies = [];
state.active_fly = 1;
state.active_bout = [];
state.new_bout = [];
state.key_mode = 0; % 0:nothing 1:swaps 2:behaviors
state.featvec_conditioned = [];
state.roi = [];
state.screen_pix_sz = 1;
state.linewidth = 1;
state.framerate = 30;
state.fly_length = 1; % median length of fly in pixels
% boolean variables
bool.show_trk = 1;
bool.show_legs = 1;
bool.show_wings = 1;
bool.show_trail = 1;
bool.show_ellipse = 1;
bool.show_seg = 0;
bool.show_img = 1;
bool.do_play = 0;
bool.stop_bout_play = 0;
bool.updating = 0;
bool.auto_zoom = 0;
bool.moving_bout_boundaries = 0;
% question data
quest.ask_overwrite_trk = 1;
quest.ans_overwrite_trk = '';
quest.ask_change_cert = 1;
quest.ans_change_cert = '';
quest.ask_delete_bout = 1;
quest.ans_delete_bout = '';
% action data
actions.beh_labels = {};
actions.beh_colors = [];
actions.gt = [];
actions.pred = [];
actions.fn = [];
actions.fp = [];
actions.curr_bouts = [];
actions.curr_frames = [];
actions.overlapTHRESH = 0.1;
% swap data
swap.swaps = [];
swap.id_switch = 0;
swap.switches = [];
swap.all_switches = [];
end
function iconImgs = getIcons(figclr)
icon_names = {'tool_zoom_in.png','tool_zoom_out.png','tool_hand.png'};
iconImgs = cell(1,numel(icon_names)*2);
for j=1:numel(icon_names)
try
% run from desktop
cdata = imread(fullfile(parentdir,'tracking','utilities','icons', icon_names{j}));
catch
% run from app
cdata = imread(fullfile('tracking','utilities','icons', icon_names{j}));
end
cdata = double(cdata)./(2^16-1);
r = cdata(:,:,1); g = cdata(:,:,2); b = cdata(:,:,3);
inds = find((r+g+b)==0);
r(inds) = figclr(1);
g(inds) = figclr(2);
b(inds) = figclr(3);
icon_im = cdata; icon_im_active = cdata;
icon_im(:,:,1) = r; icon_im(:,:,2) = g; icon_im(:,:,3) = b;
r(inds) = figclr(1)-.2;
g(inds) = figclr(2)-.2;
b(inds) = figclr(3)-.2;
icon_im_active(:,:,1) = r; icon_im_active(:,:,2) = g; icon_im_active(:,:,3) = b;
iconImgs{j} = icon_im;
iconImgs{j+numel(icon_names)} = icon_im_active;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Open files functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function openVideo(~,~)
[video_file, path] = uigetfile({'*.seq;*.wmv;*.mov;*.mp4;*.avi;*.fmf;*.ufmf'},'Select video file',files.video_dir);
if ~video_file
return
end
openVideoFile(path,video_file);
end
function openVideoFile(path,video_file)
% load video
try
cache_size = 1;
vinfo_ = video_open(fullfile(path,video_file),cache_size);
img = video_read_frame(vinfo_,0);
catch
customDialog('warn','Could not load video file',12*fig.fs)
return;
end
% first check if track or label changes need to be saved
value = checkSaveSwaps;
if value == 0, return; end
value = checkSaveLabel;
if value == 0, return; end
% set new video
if ~isempty(quest), quest_old = quest; end
resetVars;
initVars;
if exist('quest_old','var'), quest = quest_old; end
vinfo = vinfo_;
files.video_dir = path;
% update video path visualization
filename = fullfile(path,video_file);
set(handles.videopath_h,'string',filename,'visible','on');
extent = get(handles.videopath_h,'extent');
while extent(3) > fig.sub_width
[~,filename] = strtok(filename,filesep);
set(handles.videopath_h,'String',['... ' filename]);
extent = get(handles.videopath_h,'extent');
end
figure(handles.fig_h)
subplot(handles.sub_h)
hold off
handles.img_h = imshow(img); colormap gray;
zoom reset
handles.plot_h = [];
set(handles.sub_h,'XTick',[],'YTick',[]);
total_frames = vinfo.n_frames;
state.framerate = round(vinfo.fps);
set(handles.framerate_h,'string',num2str(state.framerate));
set(handles.video_nfr_h,'string',num2str(total_frames))
set(handles.slider_h,'Min',1,'Max',total_frames,'value',1,'sliderstep',[1/total_frames .1]);
% enable video viewing components
for j=1:numel(handles.video_h)
set(handles.video_h(j),'enable','on')
end
% enable video dependent compoenents
set(handles.trk_h,'enable','on')
set(handles.gtm_h,'enable','on')
set(handles.predm_h,'enable','on')
% plot first frame
updateDisplay(state.curr_frame)
z = zoom; p = pan;
set(z,'ActionPreCallback',@getZoom);
set(z,'ActionPostCallback',@setZoom);
set(p,'ActionPreCallback',@getZoom);
set(p,'ActionPostCallback',{@setZoom,1});
% load trk file
[~,rawname] = fileparts(video_file);
trkfilepath = fullfile(path,rawname,[rawname '-track.mat']);
if exist(trkfilepath,'file')
[trk_path,trk_file,ext] = fileparts(trkfilepath);
openTrkFile(trk_path,[trk_file ext])
end
% load annotation file
annofilepath = fullfile(path, rawname, [rawname '-actions.mat']);
if exist(annofilepath,'file')
[anno_path,anno_file,ext] = fileparts(annofilepath);
loadLabelFile(anno_path,[anno_file ext],'gt')
end
end
function openTrk(~,~)
% get track file
[trk_file, path] = uigetfile('*track.mat','Select track file',files.video_dir);
if ~trk_file
return
end
openTrkFile(path,trk_file);
end
function openTrkFile(path,trk_file)
% load trk file
try
D = load(fullfile(path,trk_file)); trk_ = D.trk;
catch
customDialog('warn','Could not load trk file',12*fig.fs);
return
end
% first check if track or label changes need to be saved
value = checkSaveSwaps;
if value == 0, return; end
value = checkSaveLabel;
if value == 0, return; end
% reset interface
if ~isempty(quest), quest_old = quest; end
resetVars;
initVars;
if exist('quest_old','var'), quest = quest_old; end
state.framerate = round(vinfo.fps);
set(handles.framerate_h,'string',num2str(state.framerate));
% set new track
n_flies = size(trk_.data,1);
n_frames = size(trk_.data,2);
trk = trk_;
trk.obj_list = zeros(n_flies,n_frames);
% compute median length of flies
major_ax = trk.data(:,:,4);
state.fly_length = nanmedian(major_ax(:));
% enable track viewing components
for j=1:numel(handles.track_h)
set(handles.track_h(j),'enable','on')
end
% check whether enough legs to show them
if size(trk.data,3)>17
legs = trk.data(1,:,17+(1:2:11));
bool.show_legs = sum(~isnan(legs(:)))/n_frames > 3; % at least 3 legs on average
enabled = 'on';
if sum(~isnan(legs(:)))==0
enabled = 'off';
end
else
bool.show_legs = 0;
enabled = 'off';
end
set(handles.show_separate_h(4),'value',bool.show_legs)
set(handles.show_separate_h(4),'enable',enabled)
% check whether enough wings to show them
wings = trk.data(1,:,[10 12]);
bool.show_wings = sum(~isnan(wings(:)))/n_frames > 1; % at least 1 wing on average
enabled = 'on';
if sum(~isnan(wings(:)))==0
enabled = 'off';
end
set(handles.show_separate_h(3),'value',bool.show_wings)
set(handles.show_separate_h(3),'enable',enabled)
for j=1:n_flies
trk.obj_list(j,:) = j;
end
files.trk = trk_file;
files.path = path;
set(handles.save_swaps_h,'enable','off');
set(handles.gtm_h,'enable','on')
set(handles.predm_h,'enable','on')
set(handles.trkxls_h,'enable','on');
% determine handle visibilities
if isfield(trk,'flies_in_chamber')
n_chambers = numel(trk.flies_in_chamber);
for c=1:2
set(handles.chamber_h(c),'visible','on');
end
else
n_chambers = 1;
for c=1:2
set(handles.chamber_h(c),'visible','off');
end
end
majorax = trk.data(:,:,4);
radius = nanmean(majorax(:))*10;
if radius*2 > vinfo.sx && radius*2 > vinfo.sy
set(handles.auto_zoom_h,'visible','off');
else
set(handles.auto_zoom_h,'visible','on');
end
% set colors
state.curr_chamber = 1;
state.curr_flies = 1:n_flies;
fig.clrs = zeros(n_flies,3);
tmp = mycolormap(n_flies);
tmp = tmp*.8+.2;
if n_flies <= 3
tmp = [.3 .5 1; 1 .3 .3; .3 .8 .6];
tmp = tmp(1:n_flies,:);
end
if isfield(trk,'flies_in_chamber') && numel(trk.flies_in_chamber{1}) > 1 ...
&& numel(trk.flies_in_chamber{1}) == n_flies/n_chambers
for c=1:n_chambers
flies = trk.flies_in_chamber{c};
fig.clrs(flies,:) = tmp(c:n_chambers:end,:);
end
else
fig.clrs = tmp(1:n_flies,:);