-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI.m
More file actions
1593 lines (1307 loc) · 54 KB
/
GUI.m
File metadata and controls
1593 lines (1307 loc) · 54 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
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
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI
% Last Modified by GUIDE v2.5 22-Nov-2018 12:52:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update settings window
% csSettings = importdata('settings_vistrack.m', '\n');
csSettings = file2cellstr_('settings_vistrack.m');
set(handles.editSettings, 'String', csSettings);
[vcVer, vcVer_date] = version_();
set(handles.textVer, 'String', sprintf('%s (%s) James Jun', vcVer, vcVer_date));
set(handles.btnUpdate, 'Enable', ifeq_(exist_dir_('.git'), 'on', 'off'));
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function csLines = file2cellstr_(vcFile)
% read text file to a cell string
fid = fopen(vcFile, 'r');
csLines = {};
while ~feof(fid), csLines{end+1} = fgetl(fid); end
fclose(fid);
csLines = csLines';
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnLoadVideo.
function btnLoadVideo_Callback(hObject, eventdata, handles)
% hObject handle to btnLoadVideo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles in: {handles.edit1}
% out: {vidfile, vidobj}
vidFname = get_str_(handles.edit1);
if ~exist_file_(vidFname)
[FileName,PathName,FilterIndex] = uigetfile('*.wmv;*.avi;*.mpg;*.mp4', ...
'Select video file', vidFname);
if ~FilterIndex, return; end
vidFname = fullfile(PathName, FileName);
end
handles.vidFname = vidFname;
buttons_off_(handles);
clear_cache_();
% Set result field
vcFile_out = subsFileExt_(vidFname, '_Track.mat');
if exist_file_(vcFile_out)
set(handles.editResultFile, 'String', vcFile_out);
if ask_user_('Load previous tracking result?')
try
btnLoadPrev_Callback(handles.btnLoadPrev, eventdata, handles);
set(handles.btnSync, 'Enable', 'off');
set(handles.btnBackground, 'Enable', 'off');
set(handles.btnTrack, 'Enable', 'off');
set(handles.btnPreview, 'Enable', 'off');
set(handles.btnSave, 'Enable', 'on');
set(handles.panelPlot, 'Visible', 'on');
return;
catch
msgbox('Loading tracking result failed');
end
end
else
set(handles.editResultFile, 'String', '');
end
try
set(handles.edit1, 'String', handles.vidFname);
h = msgbox('Loading... (this will close automatically)', 'modal');
% handles.vidobj = VideoReader(handles.vidFname);
handles.vidobj = vistrack('VideoReader', vidFname);
fprintf('Video file info: %s\n', handles.vidFname);
disp(handles.vidobj);
% fprintf('Calculating number of frames...\n');
fprintf('\t# video frames: %d\n', handles.vidobj.NumberOfFrames);
close_(h);
set(handles.btnSync, 'Enable', 'on');
set(handles.btnBackground, 'Enable', 'off');
set(handles.btnTrack, 'Enable', 'off');
set(handles.btnPreview, 'Enable', 'off');
set(handles.btnSave, 'Enable', 'off');
set(handles.panelPlot, 'Visible', 'off');
msgstr = 'Video';
% set the ADC file and ADC timestamp paths
% [PathName, fname, ~] = fileparts(vidFname);
% set timestamps if exists
vcFile_Rs = subsFileExt_(vidFname, '_Rs.mat');
if ~exist_file_(vcFile_Rs), vcFile_Rs = ''; end
handles.ADCfile = vcFile_Rs;
set(handles.editADCfile, 'String', vcFile_Rs);
handles.ADC = try_load_(handles.ADCfile);
vcFile_Ts = subsFileExt_(vidFname, '_Ts.mat');
if ~exist_file_(vcFile_Ts), vcFile_Ts = ''; end
handles.ADCfileTs = vcFile_Ts;
set(handles.editADCfileTs, 'String', vcFile_Ts);
handles.ADCTS = try_load_(handles.ADCfileTs);
if isempty(handles.ADC) || isempty(handles.ADCTS)
set(handles.btnBackground, 'Enable', 'on');
set(handles.btnSync, 'Enable', 'off');
end
guidata(hObject, handles);
msgbox([msgstr ' file(s) loaded']);
catch
errordlg(lasterr);
end
function buttons_off_(handles)
set(handles.btnSync, 'Enable', 'off');
set(handles.btnBackground, 'Enable', 'off');
set(handles.btnTrack, 'Enable', 'off');
set(handles.btnPreview, 'Enable', 'off');
set(handles.btnSave, 'Enable', 'off');
set(handles.panelPlot, 'Visible', 'off');
function editSettings_Callback(hObject, eventdata, handles)
% hObject handle to editSettings (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editSettings as text
% str2double(get(hObject,'String')) returns contents of editSettings as a double
% --- Executes during object creation, after setting all properties.
function editSettings_CreateFcn(hObject, eventdata, handles)
% hObject handle to editSettings (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles in: editSettings
% --- Executes on button press in btnBackground.
function btnBackground_Callback(hObject, eventdata, handles)
% hObject handle to btnBackground (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
% Get time range from spike2
vcFile_adc_ts = get_str_(handles.editADCfileTs);
fSkipAdc = ~exist_file_(vcFile_adc_ts);
% if fSkipAdc
try
[FLIM, TC, img1, img00] = mov_flim_(handles.vidobj);
[img00, MASK, xy_init, vec0, xy0] = makeBackground(img1, img00);
try
TC = vistrack('cam2adc-sync', handles.S_sync, FLIM(1):FLIM(2));
% TC = interp1(handles.FLIM0, handles.TLIM0, FLIM(1):FLIM(2), 'linear');
catch
;
end
catch
return;
end
% Update handles structure
handles.MASK = MASK;
handles.xy_init = xy_init;
handles.vec0 = vec0;
handles.xy0 = xy0;
handles.TC = TC;
handles.TLIM = TC([1, end]);
handles.FLIM = FLIM;
handles.img1 = img1;
handles.img00 = img00;
guidata(hObject, handles);
set(handles.btnPreview, 'Enable', 'on');
% --- Executes on button press in btnTrack.
function btnTrack_Callback(hObject, eventdata, handles)
% hObject handle to btnTrack (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
handles.SE = strel('disk', BW_SMOOTH,0); %use disk size 3 for 640 480
handles.img0 = handles.img00 * (1-IM_MINCONTRAST); %make it darker
handles.fShow = TRACK_SHOW;
handles.ThreshLim = ThreshLim;
handles.fBWprev = 1;
TC = handles.TC;
try
[XC, YC, AC, Area, S1, MOV, XC_off, YC_off] = trackFish(handles, handles.FLIM);
catch
disp(lasterr)
errordlg('Cancelled by user');
return;
end
% Update figure handle
handles.XC = XC;
handles.YC = YC;
handles.AC = AC;
handles.MOV = MOV;
handles.XC_off = XC_off;
handles.YC_off = YC_off;
handles.xy_names = S1.xy_names;
handles.ang_names = S1.ang_names;
handles.csSettings = get_str_(handles.editSettings);
set(handles.panelPlot, 'Visible', 'on');
set(handles.btnSave, 'Enable', 'on');
guidata(hObject, handles);
vistrack('trial-save', handles);
% --- Executes on button press in btnSync.
function btnSync_Callback(hObject, eventdata, handles)
% hObject handle to btnSync (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = vistrack('trial-sync', handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in btnSync.
function btnSync_Callback1(hObject, eventdata, handles)
% S_sync = calc_sync_(handles);
LOADSETTINGS;
vidobj = handles.vidobj;
% mov1 = vistrack('loadvid-preview', handles.vidobj);
try
handles.xyLED = xyLED;
catch
handles.xyLED = round([vidobj.height, vidobj.width]/2);
end
if ~exist('FPS0', 'var'), FPS0 = get(vidobj, 'FrameRate'); end
ADCTC = load(get_str_(handles.editADCfileTs));
disp_adc_title_(ADCTC);
if ~exist('TLIM0', 'var')
try
%load from spike2 TC channel
prefix = getSpike2Prefix(ADCTC);
chTCAM = getfield(ADCTC, sprintf('%s_Ch%d', prefix, ADC_CH_TCAM));
chTCAM = chTCAM.times;
if ~exist('SYNC_PERIOD', 'var'), SYNC_PERIOD = median(diff(chTCAM)); end
TLIM0 = chTCAM([1 end]);
fprintf('TLIM0: ');
disp(TLIM0(:)');
catch
disp(lasterr);
errordlg('Specify TLIM0 = [First, Last]; in the Settings');
return;
end
end
% vistrack('checkLedPos', handles.vidobj, handles.xyLED);
try
if ~exist('FLIM0', 'var')
FLIM0 = [];
[FLIM0(1), handles.xyLed] = detectBlink(handles, 'first');
FLIM0(2) = detectBlink(handles, 'last');
end
if ~exist('SYNC_FIRST', 'var'), SYNC_FIRST = 0; end
FPS = diff(FLIM0) / diff(TLIM0);
SYNC_SKIP0 = (diff(FLIM0)/FPS0 - diff(TLIM0))/SYNC_PERIOD;
fprintf('TLIM: [%f, %f], SYNC_SKIP: %f\n', TLIM0(1), TLIM0(2), SYNC_SKIP0);
SYNC_SKIP = round(SYNC_SKIP0);
if SYNC_FIRST
TLIM0(2) = TLIM0(2) + SYNC_SKIP*SYNC_PERIOD;
else
TLIM0(1) = TLIM0(1) - SYNC_SKIP*SYNC_PERIOD;
end
FPS = diff(FLIM0) / diff(TLIM0);
fCheckSync = 0;
%check if correct
if fCheckSync
[viBlink, vrBlinkInt] = findBlink(handles.vidobj, SYNC_PERIOD, FLIM0(1), FPS);
chTCAM = getfield(ADCTC, sprintf('%s_Ch%d', prefix, ADC_CH_TCAM));
vtBlink = chTCAM.times;
n = min(numel(viBlink), numel(vtBlink));
viBlink2 = round(interp1(TLIM0, FLIM0, TLIM0(1):SYNC_PERIOD:TLIM0(end)));
hFig = figure;
set(gcf, 'OuterPosition', get(0, 'ScreenSize')); drawnow;
AX = [];
subplot 311; plot(vtBlink(1:n), viBlink(1:n), 'b.', vtBlink(1:n), viBlink(1:n), 'bo', vtBlink(1:n), viBlink2, 'r.-');
AX(1) = gca; title('Blue: Observed, Red: Predicted');
xlabel('ADC Time (s)'); ylabel('Frame #');
subplot 312; plot(vtBlink(1:n), viBlink(1:n) - viBlink2); ylabel('Frame# difference');
AX(2) = gca;
subplot 313; plot(vtBlink(1:n), vrBlinkInt(1:n)); ylabel('Brightness');
set(gca, 'YLim', [1 2]);
AX(3) = gca;
linkaxes(AX, 'x');
end
catch
disp(lasterr);
errordlg('Check the TLIM0 setting (ADC time range)');
return;
end
TLIM0 = round(TLIM0);
str = sprintf('FPS = %0.6f Hz, TLIM0=[%d, %d], FLIM0=[%d, %d]\n', FPS, TLIM0(1), TLIM0(2), FLIM0(1), FLIM0(2));
msgbox(str);
disp(str);
% Update handles structure
handles.TLIM0 = TLIM0;
handles.FLIM0 = FLIM0;
handles.FPS = FPS;
guidata(hObject, handles);
set(handles.btnBackground, 'Enable', 'on');
% --- Executes on button press in btnPreview.
function btnPreview_Callback(hObject, eventdata, handles)
% hObject handle to btnPreview (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
% Obrain mask and initial location
img0 = handles.img00 * (1-IM_MINCONTRAST); %make it darker
% Initial
SE = strel('disk', BW_SMOOTH,0); %use disk size 3 for 640 480
[WINPOS, ~] = getBoundingBoxPos(handles.xy_init, size(img0), winlen*[1 1]);
img = handles.img1(WINPOS(3):WINPOS(4), WINPOS(1):WINPOS(2), :);
img0c = img0(WINPOS(3):WINPOS(4), WINPOS(1):WINPOS(2), :);
dimg = uint8_diff(img0c, img, 0);
BW = imdilate(bwmorph((dimg > IM_THRESH), 'clean', inf), SE);
BW = imfill(BW, 'holes');
BW = imclearborder(BW);
regions = regionprops(BW, {'Area', 'Centroid', 'Orientation', 'FilledImage', 'BoundingBox'});
if numel(regions)>1
L = bwlabel(BW, 8);
[iRegion] = region_nearest(regions, handles.xy0 - WINPOS([1,3]));
regions = regions(iRegion);
BW = L==iRegion;
end
[BW, AreaTarget] = bwgetlargestblob(BW);
% Update
handles.SE = SE;
handles.thresh = IM_THRESH;
handles.AreaTarget = AreaTarget;
handles.WINPOS = WINPOS;
handles.img0 = img0;
guidata(hObject, handles);
set(handles.btnTrack, 'Enable', 'on');
% Preview images
hFig = figure;
set(hFig, 'OuterPosition', get(0, 'ScreenSize'), ...
'Color', 'w', 'Name', handles.vidFname, 'NumberTitle', 'off');
drawnow;
subplot(2,2,1);
imagesc(img, INTENSITY_LIM);
axis equal; axis tight;
set(gca, {'XTick', 'YTick'}, {[],[]});
title('1. Original image');
subplot(2,2,2);
imagesc(dimg);
axis equal; axis tight;
set(gca, {'XTick', 'YTick'}, {[], []});
title('2. Difference image');
subplot(2,2,3);
imshow(BW);
title(sprintf('3. Binary image (Area=%d)', AreaTarget));
subplot(2,2,4);
BW1 = bwperim(BW);
img4 = img; img4(BW1)=255;
% imshow(img4);
imagesc(img4, INTENSITY_LIM);
axis equal; axis tight;
set(gca, {'XTick', 'YTick'}, {[],[]});
title_('4. Superimposed (if incorrect, lower "IM_THRESH")');
colormap gray;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% do not auto save the settings unless manually saved
% writeText('settings.m', get(handles.editSettings, 'String'));
delete(hObject);
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
writeText('settings_vistrack.m', get_str_(handles.editSettings));
handles.csSettings = get_str_(handles.editSettings);
guidata(hObject, handles);
function editADCfile_Callback(hObject, eventdata, handles)
% hObject handle to editADCfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editADCfile as text
% str2double(get(hObject,'String')) returns contents of editADCfile as a double
% --- Executes during object creation, after setting all properties.
function editADCfile_CreateFcn(hObject, eventdata, handles)
% hObject handle to editADCfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnLoadADC.
function btnLoadADC_Callback(hObject, eventdata, handles)
% hObject handle to btnLoadADC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.mat','Select ADC file',get_str_(handles.editADCfile));
if FilterIndex
try
handles.ADCfile = fullfile(PathName, FileName);
set(handles.editADCfile, 'String', handles.ADCfile);
h = msgbox('Loading... (this will close automatically)');
handles.ADC = load(handles.ADCfile);
try close(h); catch, end;
guidata(hObject, handles);
msgbox('ADC File loaded');
catch
set(handles.editADCfile, 'String', '');
errordlg(lasterr);
end
end
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in btnPlot2.
function btnPlot2_Callback(hObject, eventdata, handles)
% hObject handle to btnPlot2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
XC = handles.XC;
YC = handles.YC;
hFig = figure;
imshow((handles.img0)); title('Posture trajectory (red: more recent, circle: head)');
resize_figure(hFig, [0,0,.5,1]);
hold on;
nframes = size(XC,1);
nxy = size(XC,2);
mrColor = jet(nframes);
for iframe=1:TRAJ_STEP:nframes
XI = interp1(2:nxy, XC(iframe,2:end), 2:.1:nxy, 'spline');
YI = interp1(2:nxy, YC(iframe,2:end), 2:.1:nxy, 'spline');
plot(XI, YI, 'color', mrColor(iframe,:));
plot(XI(1), YI(1), 'o', 'color', mrColor(iframe,:));
end
% --- Executes on button press in btnEodRate.
function btnEodRate_Callback(hObject, eventdata, handles)
% hObject handle to btnEodRate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
TC = handles.TC;
try
ADC = handles.ADC;
[EODR, TEOD, chName] = getSpike2Chan(ADC, ADC_CH_PLOT);
fprintf('Loaded Spike2 %s (Ch%d)\n', chName, ADC_CH_PLOT);
catch
disp(lasterr);
errordlg('Load ADC file');
return;
end
%smooth and interpolate position
TCi = TC(1):(1/EODR_SR):TC(end);
X2i = interp1(handles.TC, filtPos(handles.XC(:,2), TRAJ_NFILT), TCi, 'spline', 'extrap');
Y2i = interp1(handles.TC, filtPos(handles.YC(:,2), TRAJ_NFILT), TCi, 'spline', 'extrap');
X3i = interp1(handles.TC, filtPos(handles.XC(:,3), TRAJ_NFILT), TCi, 'spline', 'extrap');
Y3i = interp1(handles.TC, filtPos(handles.YC(:,3), TRAJ_NFILT), TCi, 'spline', 'extrap');
%convert rate to 0..255 color level at the camera time
R = interp1(TEOD, EODR, TCi);
R = (R-min(R))/(max(R)-min(R));
viColorRate = ceil(R * 256);
viColorRate(viColorRate<=0)=1;
mrColor = jet(256);
% figure; plot(handles.TC, viColorRate);
% Plot the EOD color representation
hFig = figure;
imagesc(handles.img0);
set(gca, {'XTick', 'YTick'}, {[],[]}); axis equal; axis tight;
hold on;
title(sprintf('EOD (%s) at the head trajectory (red: higher rate)', chName));
for i=1:numel(viColorRate)
plotChevron([X2i(i), X3i(i)], [Y2i(i), Y3i(i)], mrColor(viColorRate(i),:), 90, .3);
% plot(Xi(i), Yi(i), '.', 'color', mrColor(viColorRate(i),:));
end
colormap gray;
resize_figure(hFig, [0 0 .5 1]);
% --- Executes on button press in btnPlot1.
function btnPlot1_Callback(hObject, eventdata, handles)
% hObject handle to btnPlot1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
LOADSETTINGS;
XC = handles.XC;
YC = handles.YC;
% filter position
nf = TRAJ_NFILT;
XCf_h = filtPos(XC(:,2),nf); YCf_h = filtPos(YC(:,2),nf);
XCf_hm = filtPos(XC(:,3),nf); YCf_hm = filtPos(YC(:,3),nf);
XCf_m = filtPos(XC(:,4),nf); YCf_m = filtPos(YC(:,4),nf);
XCf_tm = filtPos(XC(:,5),nf); YCf_tm = filtPos(YC(:,5),nf);
XCf_t = filtPos(XC(:,6),nf); YCf_t = filtPos(YC(:,6),nf);
figure; imshow(handles.img0); title('Trajectory of the rostral tip');
hold on; plot(XCf_h, YCf_h);
pause(.4);
hold on; comet(XCf_h, YCf_h, .1);
% --- Executes on button press in btnPlot3.
function btnPlot3_Callback(hObject, eventdata, handles)
% hObject handle to btnPlot3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in btnMovOut.
function btnMovOut_Callback(hObject, eventdata, handles)
% hObject handle to btnMovOut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% load data
LOADSETTINGS;
TC = handles.TC;
XC = handles.XC;
YC = handles.YC;
try
ADC = handles.ADC;
[EODR, TEOD, chName] = getSpike2Chan(ADC, ADC_CH_PLOT);
fprintf('Loaded Spike2 %s (Ch%d)\n', chName, ADC_CH_PLOT);
% Resample EOD Rate to camera time
RC = interp1(TEOD, EODR, TC);
% RC = filtfilt(ones(1,TRAJ_STEP), TRAJ_STEP, RC);
catch
disp(lasterr)
errordlg('Load ADC file');
return;
end
% figure; plot(TEOD, EODR, 'r.', TC, RC, 'b-');
% Plot the EOD color representation
writerObj = VideoWriter(MOV_FILEOUT, 'MPEG-4');
set(writerObj, 'FrameRate', handles.FPS); %30x realtime
% set(writerObj, 'Quality', 90); %30x realtime
open(writerObj);
figure; title('Reposition'); pause;
subplot(4,1,1:3);
hfig = imshow(gray2rgb(handles.img0, INTENSITY_LIM));
% hfig = imagesc(handles.img0, INTENSITY_LIM);
set(gca, {'XTick', 'YTick'}, {[],[]});
axis equal; axis tight; hold on;
title('EOD rate at the head (red: higher rate)');
%plot locations
nframes = size(XC,1);
% mrColor = jet(nframes);
[mrColor, vrRateSrt, vrQuantSrt] = quantile2color(RC);
%colorbar
plotColorbar(size(handles.img0), vrRateSrt, vrQuantSrt);
EODR1 = EODR(TEOD > TLIM(1) & TEOD < TLIM(2));
RLIM = [quantile_(EODR1, .001), quantile_(EODR1, .999)];
htext = [];
vhChevron = [];
for iframe=1:nframes
%------------------
subplot(4,1,1:3);
frameNum = iframe + handles.FLIM(1) - 1;
mrImg = readFrame(handles.vidobj, frameNum);
mrImg(~handles.MASK) = 0;
mrImg = gray2rgb(mrImg, INTENSITY_LIM);
set(hfig, 'cdata', mrImg);
try delete(htext); catch, end;
htext(1) = text(10, 30, sprintf('EOD (%s): %0.1f Hz', chName, RC(iframe)), ...
'FontSize', 12, 'Color', [1 1 1]);
htext(2) = text(10, 75, sprintf('Time: %0.1f s', TC(iframe)), ...
'FontSize', 12, 'Color', [1 1 1]);
htext(3) = text(10, 120, sprintf('Frame: %d', frameNum), ...
'FontSize', 12, 'Color', [1 1 1]);
if mod(iframe, MOV_PLOTSTEP) == 0
vhChevron(end+1) = plotChevron(XC(iframe, 2:3), YC(iframe, 2:3), mrColor(iframe,:), 90, .3);
if numel(vhChevron) > MOV_PLOTLEN
delete(vhChevron(1:end-MOV_PLOTLEN));
vhChevron(1:end-MOV_PLOTLEN) = [];
end
end
%------------------
subplot(4,1,4);
hold off;
plot(TEOD - TC(iframe), EODR, 'k.'); hold on;
axis([MOV_TimeWin(1), MOV_TimeWin(2), RLIM(1), RLIM(2)]);
plot([0 0], get(gca, 'YLim'), 'r-');
grid on;
xlabel('Time (sec)');
ylabel(sprintf('EOD (%s) Hz', chName));
colormap jet;
% drawnow;
try
writeVideo(writerObj, getframe(gcf));
catch
disp('Movie output cancelled by user');
close(writerObj);
return;
end
end
close(writerObj);
msgbox(sprintf('File written to %s', MOV_FILEOUT));
% --- Executes on button press in btnSoundOut
function btnSoundOut_Callback(hObject, eventdata, handles)
% hObject handle to btnSoundOut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% load data
LOADSETTINGS;
TC = handles.TC;
TLIM = [TC(1), TC(end)];
try
S = handles.ADCTS;
csFieldnames = fieldnames(S);
S = getfield(S, csFieldnames{1});
Teod = S.times;
Teod1 = Teod(Teod > TLIM(1) & Teod < TLIM(2));
viEod1 = round((Teod1 - TLIM(1)) * WAV_Fs);
tdur = diff(TLIM);
ns = round(tdur * WAV_Fs);
%make a binary vector
mlBinvec = zeros(ns,1);
mlBinvec(viEod1) = 1;
wavwrite(mlBinvec, WAV_Fs, WAV_FILEOUT);
msgbox(sprintf('File written to %s', WAV_FILEOUT));
catch
disp(lasterr)
errordlg('Load ADC Timestamp');
return;
end
function editADCfileTs_Callback(hObject, eventdata, handles)
% hObject handle to editADCfileTs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editADCfileTs as text
% str2double(get(hObject,'String')) returns contents of editADCfileTs as a double
% --- Executes during object creation, after setting all properties.
function editADCfileTs_CreateFcn(hObject, eventdata, handles)
% hObject handle to editADCfileTs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnLoadADCTS.
function btnLoadADCTS_Callback(hObject, eventdata, handles)
% hObject handle to btnLoadADCTS (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.mat','Select ADC Timestamp', get_str_(handles.editADCfileTs));
if FilterIndex
try
handles.ADCfileTs = fullfile(PathName, FileName);
set(handles.editADCfileTs, 'String', handles.ADCfileTs);
h = msgbox('Loading... (this will close automatically)');
handles.ADCTS = load(handles.ADCfileTs);
try close(h); catch, end;
guidata(hObject, handles);
msgbox('ADC File loaded');
catch
set(handles.editADCfileTs, 'String', '');
errordlg(lasterr);
end
end
% --- Executes during object creation, after setting all properties.
function btnPreview_CreateFcn(hObject, eventdata, handles)
% hObject handle to btnPreview (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in btnPlotDensity.
function btnPlotDensity_Callback(hObject, eventdata, handles)
% hObject handle to btnPlotDensity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% vistrack('trial-visitcount', handles);
vistrack('trial-visitcount', handles);
% LOADSETTINGS;
% h = msgbox('Calculating... (this will close automatically)');
%
% % [mnVisit1, mnVisit] = calcVisitCount(vsTrialPool, img0, mlMask, nGrid);
%
% %track head
% [VISITCNT, TIMECNT] = calcVisitDensity(handles.img0, handles.TC, handles.XC(:,2), handles.YC(:,2), TRAJ_NFILT);
% try close(h); catch, end;
%
% [~, exprID, ~] = fileparts(handles.vidFname);
% img0_adj = imadjust(handles.img0);
% figure;
% subplot 121;
% imshow(rgbmix(img0_adj, imgray2rgb(TIMECNT), [], 'transparent'));
% title(['Time density map: ', exprID]);
%
% subplot 122;
% imshow(rgbmix(img0_adj, imgray2rgb(VISITCNT), [], 'transparent'));
% title(['Visit density map: ', exprID]);
%
% %update
% handles.TIMECNT = TIMECNT;
% handles.VISITCNT = VISITCNT;
% guidata(hObject, handles);
function editResultFile_Callback(hObject, eventdata, handles)
% hObject handle to editResultFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editResultFile as text
% str2double(get(hObject,'String')) returns contents of editResultFile as a double
% --- Executes during object creation, after setting all properties.
function editResultFile_CreateFcn(hObject, eventdata, handles)
% hObject handle to editResultFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnLoadPrev.
function btnLoadPrev_Callback(hObject, eventdata, handles)
% hObject handle to btnLoadPrev (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
resultFile = get_str_(handles.editResultFile);
if ~exist_file_(resultFile)
[FileName,PathName,FilterIndex] = uigetfile('*_Track.mat','Select *_Track.mat file', resultFile);
if ~FilterIndex, return; end
resultFile = fullfile(PathName, FileName);
end
try
% resultFile = fullfile(PathName, FileName);
set(handles.editResultFile, 'String', resultFile);
h = msgbox('Loading... (this will close automatically)');
warning off;
S = load(resultFile);
try close(h); catch, end;
% Apply settings
set(handles.editSettings, 'String', S.csSettings);
S_cfg = vistrack('load-cfg');
handles = struct_merge_(handles, S, S_cfg.csFields);
handles.vcFile_Track = resultFile;
set(handles.edit1, 'String', handles.vidFname);
set(handles.editADCfile, 'String', [handles.vidFname(1:end-4), '_Rs.mat']);
set(handles.editADCfileTs, 'String', [handles.vidFname(1:end-4), '_Ts.mat']);
set(handles.btnSync, 'Enable', 'off');
set(handles.btnBackground, 'Enable', 'off');
set(handles.btnTrack, 'Enable', 'off');
set(handles.btnPreview, 'Enable', 'off');
set(handles.btnSave, 'Enable', 'on');
set(handles.panelPlot, 'Visible', 'on');
guidata(hObject, handles);
clear_cache_();
msgbox('Tracking Result loaded');
catch
set(handles.editResultFile, 'String', '');
errordlg(lasterr);
end
% --- Executes on button press in btnSave.
function btnSave_Callback(hObject, eventdata, handles)
% hObject handle to btnSave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
vistrack('trial-save', handles);
% --- Executes during object creation, after setting all properties.
function btnSave_CreateFcn(hObject, eventdata, handles)
% hObject handle to btnSave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in btnReplay.