-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDBscorer_1_1_exported.m
1138 lines (1069 loc) · 49.3 KB
/
DBscorer_1_1_exported.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
classdef DBscorer_1_1_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
Tool matlab.ui.Figure
LoadVideoButton matlab.ui.control.Button
MarkAreaButton matlab.ui.control.Button
StartsLabel matlab.ui.control.Label
StartSpinner matlab.ui.control.Spinner
EndsSpinnerLabel matlab.ui.control.Label
EndsSpinner matlab.ui.control.Spinner
StateButton matlab.ui.control.StateButton
TotalTimesEditFieldLabel matlab.ui.control.Label
TotalTimesEditField matlab.ui.control.NumericEditField
StatusEditField matlab.ui.control.NumericEditField
EnterInfoEditField matlab.ui.control.EditField
AutomaticScore matlab.ui.control.Label
GreenMobilityEditFieldLabel matlab.ui.control.Label
GreenMobilityEditField matlab.ui.control.NumericEditField
CalibratedThresholdLabel matlab.ui.control.Label
CalibratedThreshold matlab.ui.control.NumericEditField
AreaThresholdLabel matlab.ui.control.Label
EnterThreshold matlab.ui.control.NumericEditField
Analyse matlab.ui.control.Button
Manual matlab.ui.control.Button
Play matlab.ui.control.StateButton
LatencyMagentaLabel matlab.ui.control.Label
Latency matlab.ui.control.NumericEditField
LongestBoutMagentaLabel matlab.ui.control.Label
Largest_Bout matlab.ui.control.NumericEditField
TimeThresholdsLabel matlab.ui.control.Label
MinIB matlab.ui.control.NumericEditField
BinaryThresholdLabel matlab.ui.control.Label
BT matlab.ui.control.Spinner
TimeBinsSpinnerLabel matlab.ui.control.Label
TimeBinsSpinner matlab.ui.control.Spinner
ResetButton matlab.ui.control.StateButton
BackgroundFillButton matlab.ui.control.Button
BlurImageLabel matlab.ui.control.Label
Blur matlab.ui.control.Spinner
UIAxes matlab.ui.control.UIAxes
end
properties (Access = public)
v % Video
changingValue1 % Video analysis start time
changingValue2 % Video analysis end time
show1 % Video analysis first frame
show2 % Video analysis end frame
meanbinpercentage_ar % Area Change Per Second wise
leftColumn % For Cropping
topLine % For Cropping
width % For Cropping
height % For Cropping
ar % Raw area
cr % Struc for image data frames
T % Binary threshold
autoY %
path3 % Video Path
filename
filename_3
fbf % not paper
fbfd % not paper
J % Estimated and Refined Background
Bkg % Median background
G % Gaussian Blur value
selectioncord
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: LoadVideoButton
function LoadVideoButtonPushed(app, event)
% Changes Button Color and Values to initial state
app.LoadVideoButton.Text='Load Video';
app.MarkAreaButton.Text='Mark Area';
app.MarkAreaButton.BackgroundColor='White';
app.Analyse.Text='Automatic Analysis';
app.Analyse.BackgroundColor='White';
app.Play.Text='Play';
app.Play.BackgroundColor='White';
app.StateButton.BackgroundColor='White';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='White';
app.StatusEditField.Value = 0;
app.TotalTimesEditField.Value =0;
app.GreenMobilityEditField.Value =0;
app.CalibratedThreshold.Value = 0;
app.Play.Value=1;
app.StateButton.Value=0;
app.Largest_Bout.Value=0;
app.Latency.Value = 0;
%app.ResetButton.Value=0;
%app.Blur.Value=.1;
% Close all the figures first
figure
close all
% Imports selected video
[filename_2, pathname_2] = uigetfile('*.mov;*.wmv;*.mp4;*.avi','Open Video File');
if filename_2 == 0
% If user clicked the Cancel button.
return;
end
app.LoadVideoButton.BackgroundColor='y';
app.filename_3 = cellstr(filename_2);
pathname_3 = cellstr(pathname_2);
video = fullfile(pathname_3{1},app.filename_3{1});
% Get the name video and create folder with video name
[folder, baseFileNameNoExt, ~] = fileparts(video);
fileName = fullfile(pathname_3{1});
dotLocations = find(fileName == '.');
if isempty(dotLocations)
nameBeforeFirstDot = fileName;
else
nameBeforeFirstDot = fileName(1:dotLocations(1)-1);
end
cd(folder)
if ~exist([folder,'\',baseFileNameNoExt],'dir')
mkdir (baseFileNameNoExt)
end
cd(baseFileNameNoExt)
app.path3=nameBeforeFirstDot;
app.filename=baseFileNameNoExt;
% creates video reader and shows firstframe from the video
app.v = VideoReader(video);
show = readFrame(app.v);
imshow(show, 'Parent', app.UIAxes);
app.StartSpinner.Value=1;
app.EndsSpinner.Value=round(app.v.Duration)-1;
app.LoadVideoButton.BackgroundColor='cyan';
end
% Button pushed function: MarkAreaButton
function MarkAreaButtonPushed(app, event)
% Clear old data that may interfere with new data
clear app.cr
figure
close all
textFontSize=10;
% Changes Button Color and Values to initial state
app.Analyse.Text='Automatic Analysis';
app.Analyse.BackgroundColor='White';
app.Play.Text='Play';
app.Play.BackgroundColor='White';
app.StateButton.BackgroundColor='White';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='White';
app.StatusEditField.Value = 0;
app.TotalTimesEditField.Value =0;
app.GreenMobilityEditField.Value =0;
app.CalibratedThreshold.Value = 0;
%app.Blur.Value=.1;
% Show first frame within specified time period and
% allows selection of region of interest using polygon tool
if app.changingValue1 >0
app.v.CurrentTime = app.changingValue1;
app.show1 = readFrame(app.v);
else
app.v.CurrentTime = 1;
app.show1 = readFrame(app.v);
end
imshow(app.show1);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
hFH = impoly();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
app.selectioncord=xy;
% Get coordinates of the boundary of the drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
app.MarkAreaButton.Text='Cropping...';
app.MarkAreaButton.BackgroundColor='y';
hold on;
plot(x, y,'b', 'LineWidth', 2);
drawnow;
legend(['Start-',num2str(app.changingValue1),', End-',num2str(app.changingValue2),', ',[app.filename],',',[app.EnterInfoEditField.Value]])
text(median(x), median(y), [app.EnterInfoEditField.Value], 'FontSize', textFontSize, 'FontWeight', 'Bold','Color', 'y');
hold off
figure
imshow(app.show1);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
hold on;
plot(x, y,'b', 'LineWidth', 2);
drawnow;
legend(['Start-',num2str(app.changingValue1),', End-',num2str(app.changingValue2),', ',[app.filename],',',[app.EnterInfoEditField.Value]])
text(median(x), median(y), [app.EnterInfoEditField.Value], 'FontSize', textFontSize, 'FontWeight', 'Bold','Color', 'b');
format shortg
s = num2str(fix(clock));
saveas(gcf,['Tag ',[app.EnterInfoEditField.Value],' ',s,'.png'])
close all
app.MarkAreaButton.Text='Cropping ...';
% Now crop the image.
app.leftColumn = min(x);
rightColumn = max(x);
app.topLine = min(y);
bottomLine = max(y);
app.width = rightColumn - app.leftColumn + 1;
app.height = bottomLine - app.topLine + 1;
I1 = imcrop(app.show1, [app.leftColumn, app.topLine, app.width, app.height]);
% Create struct to store image data
% Use 1 for single channel 3 for 3 channel
% Read all the frames in the video from user defined start time
A=size(I1);
app.cr = struct('cdata',zeros(A(1),A(2),1,'uint8'),...
'colormap',[]);
app.v.CurrentTime = app.changingValue1;
maxf=(app.changingValue2-app.changingValue1)*app.v.FrameRate;
k = 1;
scale=round(256/max(A),1); % Scaled for fast visualization
while hasFrame(app.v)
rgb = readFrame(app.v);
gray1=rgb2gray(rgb);
I = imcrop(gray1, [app.leftColumn, app.topLine, app.width, app.height]);
%app.cr(k).cdata = I;
rI = imresize(I, scale, 'nearest');
app.cr(k).cdata = rI;
k = k+1;
if k>maxf+1
break
end
end
% Automatic background estimation
K=app.cr(1).cdata;
[m,n]=size(K);
% 10 percent (.1) randomly choosen frame used to sample frames
% for background estimation
no_of_frames_to_sample=round(.1*(k-1));
x = randsample(length(app.cr),no_of_frames_to_sample);
indices = sort(x);
A = zeros(m,n,1,'uint8');
for i=1:length(indices)
A(:,:,i) = uint8(app.cr(indices(i)).cdata);
end
% Median method for background estimation
app.Bkg = uint8(median(double(A),3));
app.J = app.Bkg;
level = graythresh(gray1);
app.BT.Value=level*100;
app.T=level*100;
K=app.cr(round(median(indices))).cdata;
bkgsubi=imgaussfilt(imabsdiff(app.J,K),.1);
blwh=imbinarize(bkgsubi);
B = imoverlay(K,blwh,'m');
imshowpair(B,K,'montage','Parent', app.UIAxes)
%app.G=.1;
app.MarkAreaButton.Text='Mark Area';
app.MarkAreaButton.BackgroundColor='cyan';
app.ResetButton.Value=1;
app.ResetButton.BackgroundColor='m';
app.ResetButton.Text='Auto';
cd([app.path3,'\',app.filename])
end
% Value changing function: StartSpinner
function StartSpinnerValueChanging(app, event)
app.changingValue1 = event.Value;
if app.changingValue1<app.v.Duration
app.v.CurrentTime = app.changingValue1;
app.show1 = readFrame(app.v);
imshow(app.show1, 'Parent', app.UIAxes);
else
app.v.CurrentTime = 0;
end
% Changes Button Color and Values
app.MarkAreaButton.Text='Mark Area';
app.MarkAreaButton.BackgroundColor='White';
app.Analyse.Text='Automatic Analysis';
app.Analyse.BackgroundColor='White';
app.Play.Text='Play';
app.Play.BackgroundColor='White';
app.StateButton.BackgroundColor='White';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='White';
app.StatusEditField.Value = 0;
app.TotalTimesEditField.Value =0;
app.GreenMobilityEditField.Value =0;
end
% Value changing function: EndsSpinner
function EndsSpinnerValueChanging(app, event)
app.changingValue2 = event.Value;
if app.changingValue2<app.v.Duration
app.v.CurrentTime = app.changingValue2;
app.show2 = readFrame(app.v);
imshow(app.show2, 'Parent', app.UIAxes);
else
app.v.CurrentTime = 0;
end
app.TotalTimesEditField.Value =app.changingValue2-app.changingValue1;
% Changes Button Color and Values
app.MarkAreaButton.Text='Mark Area';
app.MarkAreaButton.BackgroundColor='White';
app.Analyse.Text='Automatic Analysis';
app.Analyse.BackgroundColor='White';
app.Play.Text='Play';
app.Play.BackgroundColor='White';
app.StateButton.BackgroundColor='White';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='White';
app.StatusEditField.Value = 0;
app.GreenMobilityEditField.Value =0;
end
% Button pushed function: Analyse
function AnalyseButtonPushed(app, event)
% Changes Button Color and Values
app.Analyse.Text='Analysing';
app.Play.Text='Play';
app.Play.BackgroundColor='White';
app.StateButton.BackgroundColor='White';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='White';
%% For storing data
app.ar=[];
% app.fbf=[]; not paper
% app.fbfd=[0]; not paper
numframes=length(app.cr);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:numframes
K=app.cr(i).cdata;
bkgsubi=imgaussfilt(imabsdiff(app.J,K),app.G);
BW=imbinarize(bkgsubi,app.T/100);%
All=sum(BW,'all');%
app.ar=[app.ar,All];%
% if i<numframes-1 % not paper
% app.fbf=[app.fbf,sum(BW,'all')]; % not paper
% K2=(app.cr(i+1).cdata); % not paper
% bkgsubi2=imgaussfilt(imabsdiff(app.J,K2),app.G); % not paper
% BW3=imbinarize(bkgsubi2,app.T/100); % not paper
% app.fbfd=[app.fbfd,sum(abs(BW3-BW),'all')]; % not paper
% end
end
%% Calculation for determining immobility
ar_score=app.ar; % Raw Area data
ar_abs=abs(diff(ar_score));
b_ar=ar_score(1:length(ar_abs));
percentage_ar=(bsxfun(@rdivide,ar_abs,b_ar))*100;
r_ar =length(percentage_ar)- rem(length(percentage_ar),app.v.FrameRate);
R_ar=percentage_ar(1:r_ar);
bin_ar = reshape(R_ar,app.v.FrameRate,[]);
Area_Change=mean(bin_ar,1);
app.meanbinpercentage_ar = Area_Change;
im=app.meanbinpercentage_ar<app.EnterThreshold.Value;
framerate=app.v.FrameRate;
%%same for manual from here
A=im;
minimumchange=app.MinIB.Value-1;
Till=length(A);
transitions = diff([0,A,0]); % find where the array goes from non-zero to zero and vice versa
runstarts = find(transitions == 1);
runends = (find(transitions == -1)-1); %one past the end
runlengths = abs(runends - runstarts);
runstarts(runlengths<= minimumchange) = [];
runends(runlengths<= minimumchange) = [];
Y=zeros(length(A),1);
for i=1:length(runstarts)
Y(runstarts(i):runends(i))=1;
end
% new raster code start
bar(Y*app.EnterThreshold.Value,1,'M','EdgeColor','none')
alpha(.4) % sets transparency
hold on
bar(double(Y==0)*app.EnterThreshold.Value,1,'G','EdgeColor','none')
alpha(.4)
yticks([])
xticks(0:app.TimeBinsSpinner.Value:Till)
pbaspect([Till Till/10 1])
xlim([0 Till])
%axis off
%ylim([0 areathres])
ax = gca;
ax.Color ='none';
ax.FontWeight='bold';
ax.FontSize = 12;
%ax.YTick = 1:1:100;
ax.TickLength = [.005 0.035];
ax.LineWidth=2;
hold on
%new raster code end
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
xlabel('Time (second)')
ylabel([app.EnterInfoEditField.Value])
format shortg
s = num2str(fix(clock));
box on
saveas(gcf,['Raster ',[app.EnterInfoEditField.Value],' ',s,'.png'])
app.meanbinpercentage_ar(app.meanbinpercentage_ar>10)=10; % cap max value to 20
p=plot(app.meanbinpercentage_ar,'-k','LineWidth',2);
hold on
xlabel('Time (Second)')
ylabel(['Percent Area Change of ',[app.EnterInfoEditField.Value]])
pbaspect([Till Till/10 1])
ax.YTick = 0:5:100;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
box off
ylim([0 max(app.meanbinpercentage_ar)])
saveas(gcf,['Percent Area Change of ',[app.EnterInfoEditField.Value],' ',s,'.png'])
app.CalibratedThreshold.Value = 0;
app.autoY=Y;
Immobility=(sum(Y(1:Till))/Till)*100;
Number_of_bouts=length(runends);
if sum(runlengths,'all')==0
Longest_bout=0;
else
Longest_bout=max(runlengths)+1;
end
if sum(runstarts,'all')==0
Immobility_latency=Till;
else
Immobility_latency=runstarts(1)-1;
end
app.Largest_Bout.Value=Longest_bout;
app.Latency.Value = Immobility_latency;
app.StatusEditField.Value = Immobility;
app.TotalTimesEditField.Value = length(Y);
app.GreenMobilityEditField.Value =length(Y)-sum(Y);
AnalysisStart=app.changingValue1;
AnalysisEnd=app.changingValue2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% binned interval
binsize=app.TimeBinsSpinner.Value;
if binsize<length(Y)
bin =length(Y)- rem(length(Y),binsize);
Rim=Y(1:bin);
if length(Y)>bin
Rim2=Y(bin+1:end);
Rimbin = reshape(Rim,binsize,[]);
binnedim = [sum(Rimbin,1)/binsize,sum(Rim2)/length(Rim2)]*100;
else
Rimbin = reshape(Rim,binsize,[]);
binnedim = (sum(Rimbin,1)/binsize)*100;
end
else
binnedim=sum(Y)/length(Y)*100;
end
% Binned Immobility heatmap
figure
image(abs(100-binnedim))
colormap(gray(100))
colorbar
yticks([])
ylabel([app.EnterInfoEditField.Value])
xlabel('Binned Immobility (Darker)')
pbaspect([length(binnedim) length(binnedim)/10 1])
ax = gca;
ax.Color = 'none';
ax.FontWeight='bold';
ax.FontSize = 12;
ax.TickLength = [.005 0.035];
ax.LineWidth=2;
ax.XTick = 1:1:length(Y);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
saveas(gcf,['Binned ',[app.EnterInfoEditField.Value],' ',s,'.png'])
AutoAnalysis = ['Auto ',[app.filename],' ',[app.EnterInfoEditField.Value],' ',s];
Binary_Threshold=app.T;
Blur_Value=app.G;
Area_Threshold=app.EnterThreshold.Value;
Time_Threshold=minimumchange+1;
Bin_Size=binsize;
app.Analyse.BackgroundColor='cyan';
app.Analyse.Text='Saving';
close all
save(AutoAnalysis)%remove this for paper??
%% writes excel file start
filenamexlsx = ['Auto ',[app.filename],' ',[app.EnterInfoEditField.Value],' ',s,'.xlsx'];
A = {'Area'};
B=[ar_score'];
sheet = 1;
xlsrange = 'A1';
xlswrite(filenamexlsx,A,sheet,xlsrange)
xlsrange = 'A2';
xlswrite(filenamexlsx,B,sheet,xlsrange)
C={'AreaChange/S'};
D=[Area_Change'];
sheet = 1;
xlsrange = 'C1';
xlswrite(filenamexlsx,C,sheet,xlsrange)
xlsrange = 'C2';
xlswrite(filenamexlsx,D,sheet,xlsrange)
E={'Immobility','Latency','Longest Bout','Number of Bouts','FrameRate','Start', 'End',' Binary Threshold','Blur Value','Area Threshold','Time Threshold','Bin Size','Bins'};
xlsrange = 'E1';
xlswrite(filenamexlsx,E,sheet,xlsrange)
F=[Immobility,Immobility_latency,Longest_bout,Number_of_bouts,framerate,AnalysisStart, AnalysisEnd, app.T,app.G,app.EnterThreshold.Value, minimumchange+1,binsize,binnedim];
xlsrange = 'E2';
xlswrite(filenamexlsx,F,sheet,xlsrange)
%% writes excel file end
app.Analyse.Text='Automatic Analysis';
app.Analyse.BackgroundColor='cyan';
close all
end
% Button pushed function: Manual
function ManualButtonPushed(app, event)
figure
close all
clear app.cr
app.Play.Text='Play';
app.Play.BackgroundColor='y';
app.StatusEditField.Value = 0;
app.TotalTimesEditField.Value =0;
app.GreenMobilityEditField.Value =0;
app.Largest_Bout.Value=0;
app.Latency.Value = 0;
app.Play.Value=1;
is=0;
t=0;
frame_by_frame_time_original = 1/app.v.FrameRate;
im2 = [];
%imshow(app.cr(1).cdata,'Parent',app.UIAxes)
imshow(app.cr(1).cdata)
set(gca,'Units','pixels')
posf = get(gca,'Position');
widthf = posf(3);
heightf = posf(4);
drawnow
%hold on
%yl = yline(1,'LineWidth',3);
%yl.Color = 'g';
app.StateButton.Value=0;
app.StateButton.Text='Mobility';
xy=app.selectioncord
% Get coordinates of the boundary of the drawn region.
structBoundaries = bwboundaries(imbinarize(app.cr(1).cdata));
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
tic
for frame2=1:length(app.cr)
while app.Play.Value==1
app.Play.Text='Play';
app.Play.BackgroundColor='y';
app.StateButton.BackgroundColor='y';
pause(.01)
end
app.Play.Text='Pause';
app.Play.BackgroundColor='cyan';
if app.StateButton.Value==1
app.StateButton.BackgroundColor='m';
app.StateButton.Text='Immobility';
is=is+1;
im2=[im2,1];
%yl.Color = 'm';
else
app.StateButton.BackgroundColor='green';
app.StateButton.Text='Mobility';
is=is+0;
im2=[im2,0];
%yl.Color = 'g';
end
app.TotalTimesEditField.Value =t/app.v.FrameRate;
t=t+1;
app.GreenMobilityEditField.Value =(t-is)/app.v.FrameRate;
app.StatusEditField.Value = 100*(is/t);
imshow(app.cr(frame2).cdata)
%imshow(app.cr(frame2).cdata,'Parent',app.UIAxes)
set(gca,'Units','pixels')
posf = get(gca,'Position');
widthf = posf(3);
heightf = posf(4);
drawnow
frame_normalization = toc;
if frame_normalization < frame_by_frame_time_original
pause(frame_by_frame_time_original - frame_normalization);
end
tic
end
hold off
app.Play.Value=1;
rc =length(im2)- rem(length(im2),app.v.FrameRate);
Rc=im2(1:rc);
binc = reshape(Rc,app.v.FrameRate,[]);
meanbinpercentage1c = mean(binc,1);
meanbinpercentage2c=meanbinpercentage1c>0.5; %manual
%%same for manual from here
Ac=meanbinpercentage2c;
minimumchangec=app.MinIB.Value-1;
Tillc=length(Ac);
transitionsc = diff([0,Ac,0]); % find where the array goes from non-zero to zero and vice versa
runstartsc = find(transitionsc == 1);
runendsc = (find(transitionsc == -1)-1); %one past the end
runlengthsc = abs(runendsc - runstartsc);
runstartsc(runlengthsc<= minimumchangec) = [];
runendsc(runlengthsc<= minimumchangec) = [];
Yc=zeros(length(Ac),1);
for i=1:length(runstartsc)
Yc(runstartsc(i):runendsc(i))=1;
end
rasterc=sort([0,runstartsc,runendsc,Tillc]);
figure
% new raster code start
bar(Yc*app.EnterThreshold.Value,1,'m','EdgeColor','none')
alpha(.4) % sets transparency
hold on
bar(double(Yc==0)*app.EnterThreshold.Value,1,'g','EdgeColor','none')
alpha(.4)
yticks([])
xticks(0:app.TimeBinsSpinner.Value:Tillc)
pbaspect([Tillc Tillc/10 1])
xlim([0 Tillc])
%ylim([0 1])
ax = gca;
ax.Color ='none';
ax.FontWeight='bold';
ax.FontSize = 12;
%ax.YTick = 1:1:100;
ax.TickLength = [.005 0.035];
ax.LineWidth=2;
hold on
%new raster code end
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
xlabel('Time (second)')
ylabel([app.EnterInfoEditField.Value])
format shortg
s = num2str(fix(clock));
box on
saveas(gcf,['Manual Raster ',[app.EnterInfoEditField.Value],' ',s,'.png'])
box off
pc=plot(app.meanbinpercentage_ar,'-k','LineWidth',2);
xlabel('Time (Second)')
ylabel(['Manual Percent Change of ',[app.EnterInfoEditField.Value]])
pbaspect([Tillc Tillc/10 1])
ax.YTick = 0:5:100;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
ylim([0 max(app.meanbinpercentage_ar)])
saveas(gcf,['Manual Percent Change of ',[app.EnterInfoEditField.Value],' ',s,'.png'])
app.CalibratedThreshold.Value = 0;
Immobilityc=(sum(Yc(1:Tillc))/Tillc)*100;
Number_of_boutsc=length(runendsc);
if sum(runlengthsc,'all')==0
Longest_boutc=0;
else
Longest_boutc=max(runlengthsc)+1;
end
if sum(runstartsc,'all')==0
Immobility_latencyc=Tillc;
else
Immobility_latencyc=runstartsc(1)-1;
end
app.Largest_Bout.Value=Longest_boutc;
app.Latency.Value = Immobility_latencyc;
app.StatusEditField.Value = Immobilityc;
app.TotalTimesEditField.Value = length(Yc);
app.GreenMobilityEditField.Value =length(Yc)-sum(Yc);
AnalysisStartc=app.changingValue1;
AnalysisEndc=app.changingValue2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% binned interval
binsizec=app.TimeBinsSpinner.Value;
if binsizec<length(Yc)
binc =length(Yc)- rem(length(Yc),binsizec);
Rimc=Yc(1:binc);
if length(Yc)>binc
Rim2c=Yc(binc+1:end);
Rimbinc = reshape(Rimc,binsizec,[]);
binnedimc = [sum(Rimbinc,1)/binsizec,sum(Rim2c)/length(Rim2c)]*100;
else
Rimbinc = reshape(Rimc,binsizec,[]);
binnedimc = (sum(Rimbinc,1)/binsizec)*100;
end
else
binnedimc=sum(Yc)/length(Yc)*100;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Binned Immobility heatmap
figure
image(abs(100-binnedimc))
colormap(gray(100))
colorbar
yticks([])
ylabel([app.EnterInfoEditField.Value])
xlabel('Manual Binned Immobility')
pbaspect([length(binnedimc) length(binnedimc)/10 1])
ax = gca;
ax.Color = 'none';
ax.FontWeight='bold';
ax.FontSize = 12;
ax.TickLength = [.005 0.035];
ax.LineWidth=2;
ax.XTick = 1:1:length(Yc);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
saveas(gcf,['Manual Binned ',[app.EnterInfoEditField.Value],' ',s,'.png'])
%%same for manual from here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clip=3;
clipped_Manual_score=Yc;
clipped_area_change=app.meanbinpercentage_ar;
for i=clip+2:length(rasterc)-1
if rasterc(i)+clip<length(Yc)-clip
if rasterc(i)-clip<=0
rasterc(i)=clip+1;
clipped_Manual_score(rasterc(i)-clip:rasterc(i)+clip)=100;
clipped_area_change(rasterc(i)-clip:rasterc(i)+clip)=100;
end
end
end
clipped_Manual_score(clipped_Manual_score==100)=[];
clipped_area_change(clipped_area_change==100)=[];
IM=clipped_area_change(clipped_Manual_score==1);
M=clipped_area_change(clipped_Manual_score==0);
[~,edges] = histcounts(clipped_area_change',1000);
Nim = histcounts(IM,edges);
Nm = histcounts(M,edges);
NmPa=[]; % tpr
for i=1:length(Nm)
Pm=(sum(Nm(i:length(Nm)))/sum(Nm));
NmPa=[NmPa,Pm];
end
NimPa=[]; % fpr
for i=1:length(Nim)
Pim=sum(Nim(i:length(Nim)))/sum(Nim);
NimPa=[NimPa,Pim];
end
%CZ
gmeansa=(NmPa.*(1-NimPa));
ThresholdPa=(edges(gmeansa==max(gmeansa))); % fix things here
CalibratedThresholdMin=min(ThresholdPa);
CalibratedThresholdMax=max(ThresholdPa);
AUCa=abs(trapz(NimPa,NmPa));
p4=plot(NimPa,NmPa,'-m','LineWidth',4);
hold on
p5=plot((NimPa(gmeansa==max(gmeansa))),(NmPa(gmeansa==max(gmeansa))),'g*','LineWidth',5);
hold off
pbaspect([1 1 1]);
ax = gca;
ax.Color = 'none';
ax.FontWeight='bold';
ax.FontSize = 12;
ax.TickLength = [.005 0.035];
ax.LineWidth=2;
xlabel('1-Specificity')
ylabel('Sensitivity')
title('ROC Curve')
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
saveas(gcf,['ROC curve generated from Manual Quantification',[app.EnterInfoEditField.Value],' ',s,'.png'])
app.CalibratedThreshold.Value = max(ThresholdPa);
close all
format shortg
s = num2str(fix(clock));
ManualAnalysis = ['Manual ',[app.filename],' ',[app.EnterInfoEditField.Value],' ',s];
%remove this for paper
%fbfdiff=app.fbfd;%remove this for paper
Data=app.cr;
framerate=app.v.FrameRate;
Area_Change=app.meanbinpercentage_ar ;
rawarea=app.ar;
framerate=app.v.FrameRate;
AnalysisStart=app.changingValue1;
AnalysisEnd=app.changingValue2;
Selection_Coordinates=app.selectioncord;
Binary_Threshold=app.T;
Blur_Value=app.G;
Area_Threshold=app.EnterThreshold.Value;
Time_Threshold=minimumchangec+1;
Bin_Size=binsizec;
save(ManualAnalysis)
%remove this for paper
%% writes excel file
filenamexlsx = ['Manual ',[app.filename],' ',[app.EnterInfoEditField.Value],' ',s,'.xlsx'];
A = {'Area','Manual'};
B=[rawarea', im2'];
sheet = 1;
xlsrange = 'A1';
xlswrite(filenamexlsx,A,sheet,xlsrange)
xlsrange = 'A2';
xlswrite(filenamexlsx,B,sheet,xlsrange)
C={'AreaChange/S','Manual/S'};
D=[Area_Change',Yc];
sheet = 1;
xlsrange = 'C1';
xlswrite(filenamexlsx,C,sheet,xlsrange)
xlsrange = 'C2';
xlswrite(filenamexlsx,D,sheet,xlsrange)
E={'Immobility','Latency','Longest Bout','Number of Bouts','FrameRate','Start', 'End',' Binary Threshold','Blur Value','Area Threshold','Time Threshold','Bin Size','Bins'};
xlsrange = 'E1';
xlswrite(filenamexlsx,E,sheet,xlsrange)
F=[Immobilityc,Immobility_latencyc,Longest_boutc,Number_of_boutsc,framerate,AnalysisStart, AnalysisEnd,app.T,app.G,app.EnterThreshold.Value, minimumchangec+1,binsizec,binnedimc];
xlsrange = 'E2';
xlswrite(filenamexlsx,F,sheet,xlsrange)
imshow(app.show1,'Parent', app.UIAxes)
app.StateButton.BackgroundColor='cyan';
app.StateButton.Text='Done!';
app.Manual.Text='Manual Analysis';
app.Manual.BackgroundColor='cyan';
app.Analyse.BackgroundColor='cyan';
app.Manual.BackgroundColor='cyan';
cd(app.path3)
cd(app.filename)
close all
end
% Value changing function: BT
function BTValueChanging(app, event)
changingValue3 = event.Value;
app.T=changingValue3;
K =app.cr(1).cdata;
bkgsubi=imgaussfilt(imabsdiff(app.J,K),app.G);
blwh1=imbinarize(bkgsubi,app.T/100);
B1 = imoverlay(K,blwh1,'g');
imshowpair(B1,K,'montage','Parent', app.UIAxes)
app.ResetButton.BackgroundColor='g';
app.ResetButton.Text='Custom';
end
% Value changed function: ResetButton
function ResetButtonValueChanged(app, event)
value = app.ResetButton.Value;
K=app.cr(1).cdata;
app.G=.1;
app.Blur.Value=.1;
bkgsubi=imgaussfilt(imabsdiff(app.J,K),app.G);
ot=graythresh(bkgsubi);
blwh=imbinarize(bkgsubi);
B = imoverlay(K,blwh,'m');
imshowpair(B,K,'montage','Parent', app.UIAxes)
app.BT.Value=round(ot*100);
app.T=round(ot*100);
app.ResetButton.BackgroundColor='m';
app.ResetButton.Text='Auto';
end
% Button pushed function: BackgroundFillButton
function BackgroundFillButtonPushed(app, event)
figure;
imshow(app.J);
set(gcf, 'Position', get(0,'Screensize'));
hold on
h = impoly;
mask = h.createMask();
bw = regionfill(app.J,mask);
app.J=bw;
delete(h);
% cla
imshow(app.J)
pause(3)
app.G=.1;
app.Blur.Value=.1;
close all
end
% Value changing function: Blur
function BlurValueChanging(app, event)
changingValue4 = event.Value;
app.G=changingValue4;
K =app.cr(1).cdata;
bkgsubi=imgaussfilt(imabsdiff(app.J,K),app.G);
blwh1=imbinarize(bkgsubi,app.T/100);
B1 = imoverlay(K,blwh1,'g');
imshowpair(B1,K,'montage','Parent', app.UIAxes)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create Tool and hide until all components are created
app.Tool = uifigure('Visible', 'off');
app.Tool.AutoResizeChildren = 'off';
app.Tool.Color = [1 1 1];
app.Tool.Position = [100 100 640 480];
app.Tool.Name = 'DBScorer';
app.Tool.Resize = 'off';
% Create LoadVideoButton
app.LoadVideoButton = uibutton(app.Tool, 'push');
app.LoadVideoButton.ButtonPushedFcn = createCallbackFcn(app, @LoadVideoButtonPushed, true);
app.LoadVideoButton.Position = [10 442 224 22];
app.LoadVideoButton.Text = 'Load Video';
% Create MarkAreaButton
app.MarkAreaButton = uibutton(app.Tool, 'push');
app.MarkAreaButton.ButtonPushedFcn = createCallbackFcn(app, @MarkAreaButtonPushed, true);
app.MarkAreaButton.Tooltip = {'Select Only Area Containing the animal'};
app.MarkAreaButton.Position = [11 285 223 37];
app.MarkAreaButton.Text = 'Mark Area';
% Create StartsLabel
app.StartsLabel = uilabel(app.Tool);
app.StartsLabel.HorizontalAlignment = 'right';
app.StartsLabel.Position = [11 405 55 22];
app.StartsLabel.Text = 'Start (s)';
% Create StartSpinner
app.StartSpinner = uispinner(app.Tool);
app.StartSpinner.ValueChangingFcn = createCallbackFcn(app, @StartSpinnerValueChanging, true);
app.StartSpinner.Limits = [1 Inf];
app.StartSpinner.Position = [69 405 53 22];
app.StartSpinner.Value = 1;
% Create EndsSpinnerLabel
app.EndsSpinnerLabel = uilabel(app.Tool);
app.EndsSpinnerLabel.HorizontalAlignment = 'right';
app.EndsSpinnerLabel.Position = [136 405 44 22];
app.EndsSpinnerLabel.Text = 'End (s)';
% Create EndsSpinner
app.EndsSpinner = uispinner(app.Tool);
app.EndsSpinner.ValueChangingFcn = createCallbackFcn(app, @EndsSpinnerValueChanging, true);
app.EndsSpinner.Limits = [2 Inf];
app.EndsSpinner.Position = [181 405 53 22];
app.EndsSpinner.Value = 2;
% Create StateButton
app.StateButton = uibutton(app.Tool, 'state');
app.StateButton.Tooltip = {''};
app.StateButton.Text = 'State';
app.StateButton.Position = [426 5 204 38];
% Create TotalTimesEditFieldLabel
app.TotalTimesEditFieldLabel = uilabel(app.Tool);
app.TotalTimesEditFieldLabel.HorizontalAlignment = 'right';
app.TotalTimesEditFieldLabel.Position = [276 405 78 22];
app.TotalTimesEditFieldLabel.Text = 'Total Time (s)';
% Create TotalTimesEditField
app.TotalTimesEditField = uieditfield(app.Tool, 'numeric');
app.TotalTimesEditField.Editable = 'off';
app.TotalTimesEditField.Position = [365 405 54 22];
% Create StatusEditField
app.StatusEditField = uieditfield(app.Tool, 'numeric');
app.StatusEditField.Editable = 'off';
app.StatusEditField.Position = [365 370 54 22];
% Create EnterInfoEditField
app.EnterInfoEditField = uieditfield(app.Tool, 'text');
app.EnterInfoEditField.Tooltip = {'Input Animal ID'};
app.EnterInfoEditField.Position = [11 332 223 22];
% Create AutomaticScore
app.AutomaticScore = uilabel(app.Tool);
app.AutomaticScore.HorizontalAlignment = 'right';
app.AutomaticScore.Position = [245 370 109 22];
app.AutomaticScore.Text = 'Score (% Magenta)';
% Create GreenMobilityEditFieldLabel
app.GreenMobilityEditFieldLabel = uilabel(app.Tool);
app.GreenMobilityEditFieldLabel.HorizontalAlignment = 'right';
app.GreenMobilityEditFieldLabel.Position = [469 405 91 22];
app.GreenMobilityEditFieldLabel.Text = 'Green (Mobility)';
% Create GreenMobilityEditField
app.GreenMobilityEditField = uieditfield(app.Tool, 'numeric');
app.GreenMobilityEditField.Limits = [0 Inf];
app.GreenMobilityEditField.Editable = 'off';
app.GreenMobilityEditField.Position = [572 405 52 22];
% Create CalibratedThresholdLabel
app.CalibratedThresholdLabel = uilabel(app.Tool);
app.CalibratedThresholdLabel.HorizontalAlignment = 'right';
app.CalibratedThresholdLabel.Position = [448 369 112 22];
app.CalibratedThresholdLabel.Text = 'Calibrated Threshold';
% Create CalibratedThreshold
app.CalibratedThreshold = uieditfield(app.Tool, 'numeric');
app.CalibratedThreshold.Limits = [0 Inf];
app.CalibratedThreshold.Editable = 'off';
app.CalibratedThreshold.Position = [572 370 56 22];
% Create AreaThresholdLabel
app.AreaThresholdLabel = uilabel(app.Tool);
app.AreaThresholdLabel.HorizontalAlignment = 'right';
app.AreaThresholdLabel.Position = [23 129 121 22];
app.AreaThresholdLabel.Text = '? Area Threshold (%)';
% Create EnterThreshold
app.EnterThreshold = uieditfield(app.Tool, 'numeric');
app.EnterThreshold.Limits = [0 Inf];
app.EnterThreshold.Tooltip = {'Optional'};
app.EnterThreshold.Position = [156 129 78 23];
app.EnterThreshold.Value = 1;
% Create Analyse
app.Analyse = uibutton(app.Tool, 'push');
app.Analyse.ButtonPushedFcn = createCallbackFcn(app, @AnalyseButtonPushed, true);
app.Analyse.Position = [10 5 224 38];
app.Analyse.Text = 'Automatic Analysis';
% Create Manual
app.Manual = uibutton(app.Tool, 'push');
app.Manual.ButtonPushedFcn = createCallbackFcn(app, @ManualButtonPushed, true);
app.Manual.Tooltip = {'For getting optimum threshold based on user input or'; 'Manually analysing Video'};
app.Manual.Position = [248 5 106 38];
app.Manual.Text = 'Manual Analysis';
% Create Play
app.Play = uibutton(app.Tool, 'state');