-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInCA.m
1953 lines (1722 loc) · 118 KB
/
InCA.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 InCA < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure
Version = 24;
TabGroup
HomeTab
DetectionTab
AnalysisTab
LogsTab
%Home
HomeImage %Done
VersionLabel %Done
NewButton %Done
NewPath %Done
OpenButton %Done
OpenPath %Done
SaveButton %Done
SavePath %Done
BatchButton %Done
BatchPanel
%Detect
Scrollpane %Done %The scrollpane that houses all of the image previews
CalibrationFrame %Done %The uiimage component used for calibrating the detection settings or refining the current frame
AutoColorToggle %Done %The uiimage component that represents the toggle instructing if InCA should automatically generate a threshold for color based image processing
AutoColorLabel %Done %The label accompanying the above uiimage component
ColorThresholdLabel %Done %The label accompanying the below uieditfield component
ColorThresholdField %Done %The uieditfield component that houses the gray thresh value
CStyleLabel %Done %The label accompanying the below component
CStyle %Done %The Drop down that determines the style of preprocessing to be used on the images for color-based mask generation
CValLabel %Done %The label accompanying the below component
CVal %Done %The uieditfield component containing the intensity of the preprocessing filter
ColorMask %Done %The uiimage component that houses the resulting overlay from color-based image mask generation
EdgeThresholdLabel %Done %The label component accompanying the below component
EdgeThresholdField %Done %The edge equivalent for ColorThresholdField
AutoEdgeToggle %Done %The edge equivalent for AutoColorToggle
AutoEdgeLabel %Done %The edge equivalent for AutoColorLabel
EStyle %Done %The Drop down that determines the style of preprocessing to be used on images for edge-based mask generation
EStyleLabel %Done %The label accompanyying the above component
EValLabel %Done %The label accompanying the below component
EVal %Done %The uieditfield component containing the intesnity of the preprocessing filter
EdgeMask %Done %The uiimage component that houses the resulting overlay from edge-based mask generation
MixerLabel %Done
MixerSlider %Done
FinalMaskViewer %Done
NextFrameDetectionButton %Done
PreviousFrameDetectionButton %Done
ResetDetectionButton %Done
ViewResultButton %Done
RefineButton %Done
RunDetectionButton %Done
AcceptFrameButton %Done
RejectFrameButton %Dohe
MultiviewToggle %Done
MultiviewLabel %Done
IFFToggle %Done
IFFLabel %Done
ICToggle %Done
ICLabel %Done
RTToggle %Done
RTLabel %Done
ColorLabel %Done
EdgeLabel %Done
VLToggle %Done
VLLabel %Done
NLButtonGroup %Done
BSRadioButton %Done
BSField %Done
BSLabel %Done
GARadioButton %Done
%Analyze
FPSLabel %Done
FPSField %Done
MPXLabel %Done
MPXField %Done
TPLabel %Done
TPField %Done
FourierFitToggle %Done
FourierFitToggleLabel %Done
AdaptiveTermsToggleLabel %Done
AdaptiveTermsToggle %Done
MinArcLengthLabel %Done
MinArcLengthField %Done
MaxTermsLabel %Done
MaxTermsField %Done
TermsofInterestLabel %Done
TermsofInterestField %Done
MetricTermsLabel %Done
MetricTermsField %Done
JumpFrameLabel %Done
JumpFrameField %Done
NextFrameButton %Done
PreviousFrameButton %Done
TargetFrameLabel %Done
TargetFrameField %Done
DecompositionTermsLabel %Done
DecompositionTermsField %Done
DecomposeButton %Done
DecompositionPanel %Done
SphFitLabel
SphFitToggle
PhiModesLabel
PhiModesField
ThetaModesLabel
ThetaModesField
RotationAxis %Done
FitType %Done
AnalyzeButton %Done
ViewerPanel %Done
PlotPanel %Done
SettingsPanel %Done
RefreshPlotsButton %Done
RadiusPlot %Done
TwoDimensionalPlot %Done
ThreeDimensionalPlot %Done
VelocityPlot %Done
CentroidPlot %Done
CentroidFirst %Done
CentroidLast %Done
OrientationPlot %Done
EvolutionPlot %Done
EvolutionFirst %Done
EvolutionLast %Done
MainPlot %Done
AsphericityPlot %Done
FourierSecond %Done
FourierLast %Done
FourierColorMap %Done
RadiusFitPlot %Done
TwoDimensionalFitPlot %Done
InspectFrameButton %Done
%Log
LogArea %Done
DownloadButton %Done
end
properties (Access = public)
frames = []
mask = []
maskInformation
ignoreFrames = []
currentFrame = 1
numFrames
plotSet
frameInterval
batchmode
workingFrame
initialized = 0;
end
methods (Access = private)
function checkVersion(app)
try
options = weboptions('Timeout', Inf);
newestVersion = str2double(string(webread('https://raw.githubusercontent.com/estradalab/inca/master/version.txt', options)));
if newestVersion > app.Version
uialert(app.UIFigure, 'A newer version of InCA is available. An update is recommended.', 'Newer version detected', 'Icon', 'warning');
elseif newestVersion < app.Version
UpdateLogs(app, 'Welcome developer :)');
else
UpdateLogs(app, 'InCA is up-to-date');
end
UpdateLogs(app, append('Latest Released Version: ', num2str(newestVersion./10)));
catch me
uialert(app.UIFigure, me.message, append('Version Check Error: ', me.identifier), 'Icon', 'error');
LogExceptions(app, me);
end
end
function resetFunction(app)
try
% Clear main variables
clear app.frames;
clear app.mask;
clear app.maskInformation;
clear app.ignoreFrames;
app.currentFrame = 1;
clear app.numFrames;
clear app.plotSet;
clear app.workingFrame;
delete(app.Scrollpane.Children);
app.ColorMask.ImageSource = 'icon.png';
app.EdgeMask.ImageSource = 'icon.png';
app.CalibrationFrame.ImageSource = 'icon.png';
cla(app.FinalMaskViewer);
app.NewPath.Value = '';
app.OpenPath.Value = '';
app.SavePath.Value = '';
app.BatchButton.Value = 0;
%Clear plots and graphs
cla(app.MainPlot);
cla(app.EvolutionPlot);
cla(app.RadiusPlot);
yyaxis(app.TwoDimensionalPlot, 'left');
cla(app.TwoDimensionalPlot);
yyaxis(app.TwoDimensionalPlot, 'right');
cla(app.TwoDimensionalPlot);
yyaxis(app.ThreeDimensionalPlot, 'left');
cla(app.ThreeDimensionalPlot);
yyaxis(app.ThreeDimensionalPlot, 'right');
cla(app.ThreeDimensionalPlot);
cla(app.CentroidPlot);
cla(app.OrientationPlot);
cla(app.VelocityPlot);
cla(app.RadiusFitPlot);
yyaxis(app.TwoDimensionalFitPlot, 'left');
cla(app.TwoDimensionalFitPlot);
yyaxis(app.TwoDimensionalFitPlot, 'right');
cla(app.TwoDimensionalFitPlot);
yyaxis(app.AsphericityPlot, 'left');
cla(app.AsphericityPlot);
yyaxis(app.AsphericityPlot, 'right');
cla(app.AsphericityPlot);
%Reset Fourier Decomp Tab
app.TargetFrameField.Value = 1;
app.TermsofInterestField.Value = 8;
app.MetricTermsField.Value = 5;
catch me
uialert(app.UIFigure, me.message, append('Reset Error: ', me.identifier), 'Icon', 'error');
end
end
function populateScrollpane(app)
f = uiprogressdlg(app.UIFigure, 'Indeterminate', 'on'); %Progress bar
delete(app.Scrollpane.Children); %Clear the scrollpane
UpdateLogs(app, 'Populating Detection Scrollpane...'); %Update logs
panelPos = app.Scrollpane.Position; %Get the current scrollpane position
[row, col] = size(app.frames(:, :, 1)); %Calculate the image size
imgHeight = row./col.*(panelPos(3) - 20); %Calculate uiimage height
for i = 1:app.numFrames
index = (app.numFrames + 1) - i;
if ~app.MultiviewToggle.UserData
%Single view point code
%Check if a mask exists for a frame
if ~any(any(app.mask(:, :, index)))
img = zeros([size(app.frames(:, :, index)), 3]);
img(:, :, 3) = app.frames(:, :, index);
scrollpaneImage = hsv2rgb(img);
else
scrollpaneImage = labeloverlay(app.frames(:, :, index), app.mask(:, :, index));
end
%Populate the uiimage component with the frame/mask
if isempty(find(app.ignoreFrames == index, 1))
uiimage(app.Scrollpane, "ScaleMethod",'scaledown', 'ImageSource', scrollpaneImage, ...
'Position', [5, (10 + (i - 1)*(imgHeight + 10)), panelPos(3) - 20, imgHeight], "ImageClickedFcn", {@imgClicked, app}, ...
'Tooltip', "Click to use frame " + num2str(index) + " for calibration", 'Tag', num2str(index));
else
uiimage(app.Scrollpane, "ScaleMethod",'scaledown', 'ImageSource', ...
labeloverlay(app.frames(:, :, index), ones(size(app.frames(:, :, index))), 'Colormap', 'autumn'), "Position", ...
[5, (10 + (i - 1)*(imgHeight + 10)), panelPos(3) - 20, imgHeight], "ImageClickedFcn", {@imgClicked, app},'Tooltip', ...
"This frame will be ignored during bubble analysis", 'Tag', num2str(index));
end
else
% Multiview point code
%Check if a mask exists for the frame
if ~any(any(app.mask(:, :, index, 1)))
img = zeros([size(app.frames(:, :, index)), 3]);
img(:, :, 3) = app.frames(:, :, index);
scrollpaneImage = hsv2rgb(img);
else
left = app.mask(:, :, index, 1);
right = app.mask(:, :, index, 2);
right(right == 1) = 2;
scrollpaneImage = labeloverlay(app.frames(:, :, index), left + right, 'Colormap', [0 0 1; 0 1 0; 0 1 1]);
end
%Populate the uiimage component with the frame/mask
if isempty(find(app.ignoreFrames == index, 1))
uiimage(app.Scrollpane, "ScaleMethod",'scaledown', 'ImageSource', scrollpaneImage, ...
'Position', [5, (10 + (i - 1)*(imgHeight + 10)), panelPos(3) - 20, imgHeight], "ImageClickedFcn", {@imgClicked, app}, ...
'Tooltip', "Click to use frame " + num2str(index) + " for calibration", 'Tag', num2str(index));
else
uiimage(app.Scrollpane, "ScaleMethod",'scaledown', 'ImageSource', ...
labeloverlay(app.frames(:, :, index), ones(size(app.frames(:, :, index))), 'Colormap', 'autumn'), "Position", ...
[5, (10 + (i - 1)*(imgHeight + 10)), panelPos(3) - 20, imgHeight], "ImageClickedFcn", {@imgClicked, app},'Tooltip', ...
"This frame will be ignored during bubble analysis", 'Tag', num2str(index));
end
end
end
%Callback for what happens if an image is clicked
function imgClicked(src, ~, app)
app.workingFrame = str2double(src.Tag); %Set the working frame
SetCalibrationFrame(app) %Execute calibration set-up code
end
UpdateLogs(app, 'Population complete'); %Update logs
close(f); %Close progress bar
end
function UpdateLogs(app, newText)
newText = string(newText);
app.LogArea.Value = [app.LogArea.Value; newText];
end
function LogExceptions(app, ME)
UpdateLogs(app, ' ');
UpdateLogs(app, '---Error Encountered---');
UpdateLogs(app, append(' MATLAB Exception message: ', ME.message));
UpdateLogs(app, append(' MATALB Exception identifier: ', ME.identifier));
stacksize = size(ME.stack, 1);
UpdateLogs(app, ' ---Begin Stacktrace---');
for i = 1:stacksize
UpdateLogs(app, append(' Line ', num2str(ME.stack(i).line), ' in ', ME.stack(i).name));
end
UpdateLogs(app, ' ---End Stacktrace---');
UpdateLogs(app, '---End Error---');
UpdateLogs(app, ' ');
end
function SetCalibrationFrame(app)
g = uiprogressdlg(app.UIFigure, 'Indeterminate', 'on'); %Progress bar
src = findobj(app.Scrollpane, 'Tag', num2str(app.workingFrame)); %Get the image that was clicked
app.CalibrationFrame.ImageSource = src.ImageSource; %Set the calibration image to the image that was clicked in the scollpane
if ~any(any(app.mask(:, :, app.workingFrame)))
%If no masks exists, convert the scrollpane/calibration
%image into an RGB image for the final mask viewer
mat = zeros([size(app.frames(:, :, app.workingFrame)), 3]);
mat(:, :, 3) = app.frames(:, :, app.workingFrame);
finalViewerImg = hsv2rgb(mat);
else
if isempty(find(app.ignoreFrames == app.workingFrame, 1))
%If the frame is not one to be ignored, then overlay
%the mask onto the image with the default color scheme
if ~app.MultiviewToggle.UserData
%Single view point code
finalViewerImg = labeloverlay(app.frames(:, :, app.workingFrame), app.mask(:, :, app.workingFrame));
else
%Multi view point code
left = app.mask(:, :, app.workingFrame, 1);
right = app.mask(:, :, app.workingFrame, 2);
right(right == 1) = 2;
finalViewerImg = labeloverlay(app.frames(:, :, app.workingFrame), left + right, 'Colormap', [0 0 1; 0 1 0; 0 1 1]);
end
else
%If the mask is being ignored, overlay the image with
%the red colormap
if ~app.MultiviewToggle.UserData
finalViewerImg = labeloverlay(app.frames(:, :, app.workingFrame), app.mask(:, :, app.workingFrame), 'Colormap', 'autumn');
else
finalViewerImg = labeloverlay(app.frames(:, :, app.workingFrame), ...
app.mask(:, :, app.workingFrame, 1) + app.mask(:, :, app.workingFrame, 2), 'Colormap', 'autumn');
end
end
end
%Get the raw frame that was clicked
calibrationImage = app.frames(:, :, app.workingFrame);
%Normalize the lighting if needed
if app.VLToggle.UserData
calibrationImage = bubbleDetection.normalizeLighting(calibrationImage, lower(string(app.NLButtonGroup.SelectedObject.Text)), app.IFFToggle.UserData, ...
app.frames(:, :, app.BSField.Value));
end
%Increase the contrast if needed
if app.ICToggle.UserData
calibrationImage = bubbleDetection.increaseContrast(calibrationImage);
end
%Remove the timestamps if needed
if app.RTToggle.UserData
calibrationImage = bubbleDetection.removeTimeStamps(calibrationImage);
end
%Set the generic old data
[row, col] = size(calibrationImage);
oldData.Center = [col./2, row./2];
oldData.Size = 0;
%Get or set the color threshold
if app.AutoColorToggle.UserData || app.ColorThresholdField.Value == 0
app.ColorThresholdField.Value = graythresh(calibrationImage);
end
%Get or set the edge threshold
if app.AutoEdgeToggle.UserData || app.EdgeThresholdField.Value == 0
[~, thresh] = edge(calibrationImage, 'Sobel');
if isnan(thresh)
thresh = 0;
end
app.EdgeThresholdField.Value = thresh;
end
%Attempt to generate color and edge masks for the frame
try
if ~app.MultiviewToggle.UserData
%Single view point code
colorMask = bubbleDetection.colorMask(calibrationImage, oldData, app.ColorThresholdField.Value, lower(string(app.CStyle.Value)), app.CVal.Value, ...
~app.BSRadioButton.Value);
edgeMask = bubbleDetection.edgeMask(calibrationImage, oldData, app.EdgeThresholdField.Value, lower(string(app.EStyle.Value)), app.EVal.Value);
else
%Multi view point code
colorMask = bubbleDetection.multiColor(calibrationImage, app.ColorThresholdField.Value, ...
lower(string(app.CStyle.Value)), app.CVal.Value, ~app.BSRadioButton.Value, 2);
edgeMask = bubbleDetection.multiEdge(calibrationImage, app.EdgeThresholdField.Value, lower(string(app.EStyle.Value)), app.EVal.Value, 2);
end
catch ME
uialert(app.UIFigure, ME.message, append('Calibration Frame Error: ', ME.identifier), 'Icon', 'error');
LogExceptions(app, ME);
end
%Create an rgb matrix representing the calibration
%image
[row, col] = size(calibrationImage);
hsv = zeros(row, col, 3);
hsv(:, :, 3) = calibrationImage;
rgb = hsv2rgb(hsv);
colorImgSrc = rgb; %Create a copy for the color mask image
rlayer = colorImgSrc(:, :, 1); %Separate out the red layer
glayer = colorImgSrc(:, :, 2); %Separate out the green layer
blayer = colorImgSrc(:, :, 3); %Separate out the blue layer
thinColor = bwmorph(colorMask, 'remove'); %Thin the mask to a one pixel thick outline
rlayer(thinColor) = 0; %Set the red layer equal to 0 where the mask is true
glayer(thinColor) = 0; %Set the green layer equal to 0 where the mask is true
blayer(thinColor) = 1; %Set the blue layer equal to 1 where the mask is true
%Recombine the layers
colorImgSrc(:, :, 1) = rlayer;
colorImgSrc(:, :, 2) = glayer;
colorImgSrc(:, :, 3) = blayer;
%See above comments
edgeImgSrc = rgb;
rlayer = edgeImgSrc(:, :, 1);
glayer = edgeImgSrc(:, :, 2);
blayer = edgeImgSrc(:, :, 3);
thinEdge = bwmorph(edgeMask, 'remove');
rlayer(thinEdge) = 0;
glayer(thinEdge) = 0;
blayer(thinEdge) = 1;
edgeImgSrc(:, :, 1) = rlayer;
edgeImgSrc(:, :, 2) = glayer;
edgeImgSrc(:, :, 3) = blayer;
%Show the Color and Edge Masks
app.ColorMask.ImageSource = colorImgSrc;
app.EdgeMask.ImageSource = edgeImgSrc;
imshow(finalViewerImg ,'Parent', app.FinalMaskViewer);
app.UpdateLogs("Calibration frame set to: " + num2str(app.workingFrame));
drawnow;
close(g);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.initialized = true;
checkVersion(app);
figureSizeChanged(app, 0);
UpdateLogs(app, "Initialization complete.");
UpdateLogs(app, append('Computer Architecture: ', computer));
UpdateLogs(app, append('InCA Version: ', num2str(app.Version./10)));
end
function figureSizeChanged(app, ~)
%Main Resizing
f = uiprogressdlg(app.UIFigure, 'Indeterminate', 'on');
pause(0.5);
UpdateLogs(app, 'Resizing components...');
position = app.UIFigure.Position;
app.TabGroup.Position = [1 1 position(3) position(4)];
drawnow;
pause(0.5);
%Home Tab Resizing
tabPos = app.HomeTab.InnerPosition;
pause(0.5);
imgSize = floor(tabPos(3)/3);
app.HomeImage.Position = [(tabPos(3)./6 - 5), (tabPos(4)/2 - (imgSize/2)), imgSize, imgSize];
app.VersionLabel.Position = [tabPos(3) - 120, 5, 120, 30];
app.NewButton.Position = [(tabPos(3)/2 + 5), (tabPos(4)/2 + (imgSize/2) - 30), 90, 30];
app.NewPath.Position = [(tabPos(3)/2 + 100), (tabPos(4)/2 + (imgSize/2) - 30), (tabPos(3)/3 - 90), 30];
app.OpenButton.Position = [(tabPos(3)/2 + 5), (tabPos(4)/2 + (imgSize/2) - 70), 90, 30];
app.OpenPath.Position = [(tabPos(3)/2 + 100), (tabPos(4)/2 + (imgSize/2) - 70), (tabPos(3)/3 - 90), 30];
app.SaveButton.Position = [(tabPos(3)/2 + 5), (tabPos(4)/2 + (imgSize/2) - 110), 90, 30];
app.SavePath.Position = [(tabPos(3)/2 + 100), (tabPos(4)/2 + (imgSize/2) - 110), (tabPos(3)/3 - 90), 30];
%Detection Tab Resizing
tabPos = app.DetectionTab.Position;
buttonWidth = ((2*tabPos(3)./5) - 15)./3;
app.Scrollpane.Position = [1, 1, tabPos(3)./5, 992];
app.ResetDetectionButton.Position = [(tabPos(3)./5) + 5, 10, buttonWidth, 30];
app.ViewResultButton.Position = [(tabPos(3)./5) + 10 + buttonWidth, 10, buttonWidth, 30];
app.RefineButton.Position = [(tabPos(3)./5) + 15 + 2*buttonWidth, 10, buttonWidth, 30];
app.MixerLabel.Position = [(tabPos(3)./5) + 15, 105, tabPos(3).*2./5 - 20, 30];
app.MixerSlider.Position = [(tabPos(3)./5) + 10, 80, ((2*tabPos(3)./5) - 20), 3];
app.ColorMask.Position = [(tabPos(3)./5) + 5, 135, (tabPos(3)./5) - 10, (tabPos(3)./7) - 10];
app.EdgeMask.Position = [(2*tabPos(3)./5) + 5, 135, (tabPos(3)./5) - 10, (tabPos(3)./7) - 10];
app.MultiviewLabel.Position = [2.*(tabPos(3))./5 + 65, 962, ((tabPos(3))./5) - 60, 20];
app.MultiviewToggle.Position = [2.*(tabPos(3))./5 + 10, 962, 40, 20];
app.IFFLabel.Position = [(tabPos(3)).*2./5 + 65, 932, ((tabPos(3))./5) - 60, 20];
app.IFFToggle.Position = [(tabPos(3)).*2./5 + 10, 932, 40, 20];
app.ICToggle.Position = [(tabPos(3)).*2./5 + 10, 902, 40, 20];
app.ICLabel.Position = [(tabPos(3)).*2./5 + 65, 902, ((tabPos(3))./5) - 60, 20];
app.RTToggle.Position = [(tabPos(3)).*2./5 + 10, 872, 40, 20];
app.RTLabel.Position = [(tabPos(3)).*2./5 + 65, 872, ((tabPos(3))./5) - 60, 20];
app.VLToggle.Position = [(tabPos(3)).*2./5 + 10, 842, 40, 20];
app.VLLabel.Position = [(tabPos(3)).*2./5 + 65, 842, ((tabPos(3))./5) - 60, 20];
app.NLButtonGroup.Position = [(tabPos(3)).*2./5 + 10, 782, ((tabPos(3))./5) - 20, 55];
app.BSRadioButton.Position = [5, 30, app.NLButtonGroup.Position(3) - 10, 20];
app.GARadioButton.Position = [5, 5, app.NLButtonGroup.Position(3) - 10, 20];
app.BSLabel.Position = [(tabPos(3)).*2./5 + 10, 747, ((tabPos(3)./5 - 30)./2), 25];
app.BSField.Position = [(tabPos(3)).*2./5 + ((tabPos(3)./5 - 30)./2) + 20, 747, ((tabPos(3)./5 - 30)./2), 25];
app.RunDetectionButton.Position = [2.*(tabPos(3)./5) + 10, 687, (tabPos(3)./5) - 20, 50];
app.CalibrationFrame.Position = [(tabPos(3)./5) + 10, 734, (tabPos(3)./5) - 10, 248];
app.ColorLabel.Position = [(tabPos(3)./5) + 10, 657, tabPos(3)./5 - 20, 25];
app.EdgeLabel.Position = [2.*(tabPos(3)./5) + 10, 657, tabPos(3)./5 - 20, 25];
app.CStyleLabel.Position = [(tabPos(3)./5) + 20, 624, (tabPos(3)./5 - 40)./2, 25];
app.CStyle.Position = [(3.*tabPos(3)./10) + 20, 624, (tabPos(3)./5 - 40)./2, 25];
app.CValLabel.Position = [(tabPos(3)./5) + 20, 589, (tabPos(3)./5 - 40)./2, 25];
app.CVal.Position = [(3.*tabPos(3)./10) + 20, 589, (tabPos(3)./5 - 40)./2, 25];
app.AutoColorToggle.Position = [(tabPos(3)./5) + 20, 559, 40, 20];
app.AutoColorLabel.Position = [(tabPos(3)./5) + 70, 559, tabPos(3)./5 - 80, 20];
app.ColorThresholdLabel.Position = [(tabPos(3)./5) + 20, 524, (tabPos(3)./5 - 40)./2, 25];
app.ColorThresholdField.Position = [(3.*tabPos(3)./10) + 20, 524, (tabPos(3)./5 - 40)./2, 25];
app.EStyleLabel.Position = [2.*(tabPos(3)./5) + 20, 624, (tabPos(3)./5 - 40)./2, 25];
app.EStyle.Position = [(5.*tabPos(3)./10) + 20, 624, (tabPos(3)./5 - 40)./2, 25];
app.EValLabel.Position = [2.*(tabPos(3)./5) + 20, 589, (tabPos(3)./5 - 40)./2, 25];
app.EVal.Position = [(5.*tabPos(3)./10) + 20, 589, (tabPos(3)./5 - 40)./2, 25];
app.AutoEdgeToggle.Position = [2*(tabPos(3)./5) + 20, 559, 40, 20];
app.AutoEdgeLabel.Position = [2*(tabPos(3)./5) + 70, 559, tabPos(3)./5 - 80, 20];
app.EdgeThresholdLabel.Position = [2.*(tabPos(3)./5) + 20, 524, (tabPos(3)./5 - 40)./2, 25];
app.EdgeThresholdField.Position = [(5.*tabPos(3)./10) + 20, 524, (tabPos(3)./5 - 40)./2, 25];
app.FinalMaskViewer.Position = [tabPos(3)*3./5 + 10, 70, (tabPos(3).*2./5 - 20), 912];
app.PreviousFrameDetectionButton.Position = [tabPos(3) - (tabPos(3).*2./5 - 20), 10, ((tabPos(3)*2./5) - 60)./5, 50];
app.NextFrameDetectionButton.Position = [tabPos(3)*3./5 + 4.*(((tabPos(3)*2./5) - 60)./5) + 40, 10, ((tabPos(3)*2./5) - 60)./5, 50];
app.AcceptFrameButton.Position = [tabPos(3)*3./5 + 3.*(((tabPos(3)*2./5) - 60)./5) + 30, 10, ((tabPos(3)*2./5) - 60)./5, 50];
app.RejectFrameButton.Position = [tabPos(3)*3./5 + 1.*(((tabPos(3)*2./5) - 60)./5) + 30, 10, ((tabPos(3)*2./5) - 60)./5, 50];
%Analysis Tab Resizing
tabPos = app.AnalysisTab.Position;
app.PreviousFrameButton.Position = [10, 5, (tabPos(3)./5 - 30)/2, 35];
app.NextFrameButton.Position = [tabPos(3)/10 + 5, 5, (tabPos(3)./5 - 30)/2, 35];
app.JumpFrameLabel.Position = [10, 45, (tabPos(3)./5 - 30)/2, 25];
app.JumpFrameField.Position = [tabPos(3)/10 + 5, 45, (tabPos(3)./5 - 30)/2, 25];
app.AnalyzeButton.Position = [10, 75, tabPos(3)./5 - 20, 35];
app.RefreshPlotsButton.Position = [10, 115, tabPos(3)./5 - 20, 35];
app.InspectFrameButton.Position = [10, 155, tabPos(3)./5 - 20, 35];
%Settings Panel Resizing
app.SettingsPanel.Position = [1, 195, tabPos(3)./5, tabPos(4) - 195];
panelPos = app.SettingsPanel.Position;
app.MPXLabel.Position = [10, panelPos(4) - 30, (panelPos(3) - 30)./2, 25];
app.MPXField.Position = [panelPos(3)./2 + 5, panelPos(4) - 30, (panelPos(3) - 30)./2 - 40, 25];
app.FPSLabel.Position = [10, panelPos(4) - 60, (panelPos(3) - 30)./2, 25];
app.FPSField.Position = [panelPos(3)./2 + 5, panelPos(4) - 60, (panelPos(3) - 30)./2 - 40, 25];
app.TPLabel.Position = [10, panelPos(4) - 90, (panelPos(3) - 30)./2, 25];
app.TPField.Position = [panelPos(3)./2 + 5, panelPos(4) - 90, (panelPos(3) - 30)./2 - 40, 25];
app.RotationAxis.ButtonGroup.Position = [10, panelPos(4) - 240, panelPos(3) - 40, 140];
app.RotationAxis.VerticalButton.Position = [5, 5, app.RotationAxis.ButtonGroup.Position(3) - 10, 25];
app.RotationAxis.HorizontalButton.Position = [5, 35, app.RotationAxis.ButtonGroup.Position(3) - 10, 25];
app.RotationAxis.MinorButton.Position = [5, 65, app.RotationAxis.ButtonGroup.Position(3) - 10, 25];
app.RotationAxis.MajorButton.Position = [5, 95, app.RotationAxis.ButtonGroup.Position(3) - 10, 25];
app.FourierFitToggle.Position = [10, panelPos(4) - 275, 40, 20];
app.FourierFitToggleLabel.Position = [60, panelPos(4) - 275, panelPos(3) - 70, 20];
app.FitType.ButtonGroup.Position = [10, panelPos(4) - 400, panelPos(3) - 40, 110];
app.FitType.ParametricButton.Position = [5, 5, app.FitType.ButtonGroup.Position(3) - 10, 25];
app.FitType.PolarPButton.Position = [5, 35, app.FitType.ButtonGroup.Position(3) - 10, 25];
app.FitType.PolarSButton.Position = [5, 65, app.FitType.ButtonGroup.Position(3) - 10, 25];
app.MinArcLengthLabel.Position = [10, panelPos(4) - 430, (panelPos(3) - 30)./2, 25];
app.MinArcLengthField.Position = [panelPos(3)./2 + 5, panelPos(4) - 430, (panelPos(3) - 30)./2 - 40, 25];
app.AdaptiveTermsToggle.Position = [10, panelPos(4) - 460, 40, 20];
app.AdaptiveTermsToggleLabel.Position = [60, panelPos(4) - 460, panelPos(3) - 70, 20];
app.MaxTermsLabel.Position = [10, panelPos(4) - 490, (panelPos(3) - 30)./2, 25];
app.MaxTermsField.Position = [panelPos(3)./2 + 5, panelPos(4) - 490, (panelPos(3) - 30)./2 - 40, 25];
app.TermsofInterestLabel.Position = [10, panelPos(4) - 520, (panelPos(3) - 30)./2, 25];
app.TermsofInterestField.Position = [panelPos(3)./2 + 5, panelPos(4) - 520, (panelPos(3) - 30)./2 - 40, 25];
app.MetricTermsLabel.Position = [10, panelPos(4) - 550, (panelPos(3) - 30)./2, 25];
app.MetricTermsField.Position = [panelPos(3)./2 + 5, panelPos(4) - 550, (panelPos(3) - 30)./2 - 40, 25];
app.DecompositionPanel.Position = [10 panelPos(4) - 680, panelPos(3) - 40, 125];
subPanel = app.DecompositionPanel.Position;
app.DecomposeButton.Position = [10 5, subPanel(3) - 20, 35];
app.DecompositionTermsLabel.Position = [10, 45, (subPanel(3) - 30)./2, 25];
app.DecompositionTermsField.Position = [subPanel(3)./2 + 5, 45, (subPanel(3) - 30)./2, 25];
app.TargetFrameLabel.Position = [10, 75, (subPanel(3) - 30)./2, 25];
app.TargetFrameField.Position = [subPanel(3)./2 + 5, 75, (subPanel(3) - 30)./2, 25];
scroll(app.SettingsPanel, 'top');
%Plot Panel Resizing
app.PlotPanel.Position = [tabPos(3)./5, 1, 4*tabPos(3)/5 - tabPos(4)./2, tabPos(4)];
panelPos = app.PlotPanel.Position;
app.FourierSecond.Position = [5, 5, 105 40];
app.FourierLast.Position = [panelPos(3) - 105, 5, 100, 40];
app.FourierColorMap.Position = [115, 5, panelPos(3) - 225, 40];
position = app.FourierColorMap.Position;
width = floor(position(3));
height = floor(position(4));
colorMapImage = zeros(height, width, 3);
imageMap = viridis(width);
redLine = transpose(imageMap(:, 1));
greenLine = transpose(imageMap(:, 2));
blueLine = transpose(imageMap(:, 3));
redLayer = repmat(redLine, height, 1);
greenLayer = repmat(greenLine, height, 1);
blueLayer = repmat(blueLine, height, 1);
colorMapImage(:, :, 1) = redLayer;
colorMapImage(:, :, 2) = greenLayer;
colorMapImage(:, :, 3) = blueLayer;
app.FourierColorMap.ImageSource = colorMapImage;
app.AsphericityPlot.Position = [10, 50, panelPos(3) - 40, panelPos(4)./2];
app.TwoDimensionalFitPlot.Position = [10, (panelPos(4)./2 + 60), panelPos(3) - 40, panelPos(3)./3];
app.RadiusFitPlot.Position = [10, (panelPos(4)./2 + 60) + 1*(panelPos(3)./3 + 10), panelPos(3) - 40, panelPos(3)./3];
app.CentroidFirst.Position = [5, (panelPos(4)./2 + 60) + 2*(panelPos(3)./3 + 10) + 10, 100 40];
app.CentroidLast.Position = [panelPos(3) - 125, (panelPos(4)./2 + 60) + 2*(panelPos(3)./3 + 10) + 10, 100, 40];
app.CentroidPlot.OuterPosition = [10, (panelPos(4)./2 + 60) + 2*(panelPos(3)./3 + 10) + 60, (panelPos(3) - 30)./2,(panelPos(3) - 30)./2];
app.OrientationPlot.OuterPosition = [panelPos(3)./2 + 5, (panelPos(4)./2 + 60) + 2*(panelPos(3)./3 + 10) + 60, (panelPos(3) - 50)./2, (panelPos(3) - 50)./2];
app.VelocityPlot.Position = [10, panelPos(4)./2 + 105 + 2*(panelPos(3)./3 + 10) + panelPos(3)./2 , panelPos(3) - 40, panelPos(3)./3];
app.ThreeDimensionalPlot.Position = [10, panelPos(4)./2 + 105 + 3*(panelPos(3)./3 + 10) + panelPos(3)./2 , panelPos(3) - 40, panelPos(3)./3];
app.TwoDimensionalPlot.Position = [10, panelPos(4)./2 + 105 + 4*(panelPos(3)./3 + 10) + panelPos(3)./2 , panelPos(3) - 40, panelPos(3)./3];
app.RadiusPlot.Position = [10, panelPos(4)./2 + 105 + 5*(panelPos(3)./3 + 10) + panelPos(3)./2 , panelPos(3) - 40, panelPos(3)./3];
scroll(app.PlotPanel, 'top');
%Viewer Panel Resizing
app.ViewerPanel.Position = [tabPos(3) - tabPos(4)./2, 1, tabPos(4)./2, tabPos(4)];
panelPos = app.ViewerPanel.Position;
app.MainPlot.OuterPosition = [10, panelPos(4)./2 + 10, panelPos(3) - 20, (panelPos(4) - 30)./2];
app.EvolutionFirst.Position = [10, 5, 100, 40];
app.EvolutionLast.Position = [panelPos(3) - 100, 5, 90, 40];
app.EvolutionPlot.OuterPosition = [10, 50, panelPos(3) - 20, (panelPos(4)./2 - 50)];
%Logs Tab Resizing'
tabPos = app.LogsTab.Position;
app.LogArea.Position = [10, 10, (tabPos(3) - 20), (tabPos(4) - 20)];
app.DownloadButton.Position = [tabPos(3) - 90, 30, 70, 70];
UpdateLogs(app, 'Resize Complete');
drawnow;
close(f);
end
function NewClicked(app, ~)
app.batchmode = false;
UpdateLogs(app, 'Loading new video...');
try
resetFunction(app);
f = uiprogressdlg(app.UIFigure, 'Title', "Please Wait", 'Message', "Loading video...", 'Indeterminate', 'on');
[app.frames, vidPath, vidFile] = incaio.loadFrames();
app.NewPath.Value = append(vidPath, vidFile);
UpdateLogs(app, append('Loading video from: ', app.NewPath.Value));
figure(app.UIFigure);
[~, ~, app.numFrames] = size(app.frames);
UpdateLogs(app, append('Number of Frames: ', num2str(app.numFrames)));
app.mask = zeros(size(app.frames));
try
populateScrollpane(app);
catch mesub
uialert(app.UIFigure, mesub.message, append('Scrollpane Open Error: ', mesub.identifier), 'Icon', 'error');
LogExceptions(app, mesub);
end
close(f);
uialert(app.UIFigure, 'Video loaded!', 'Success', 'Icon',"success", "Modal", true);
catch mesub
uialert(app.UIFigure, mesub.message, append('File Open Error: ', mesub.identifier), 'Icon', 'error');
LogExceptions(app, mesub);
end
end
function OpenClicked(app, ~)
w = uiprogressdlg(app.UIFigure, "Title", 'Please wait', 'Message', 'Loading data...', 'Indeterminate',"on");
try
resetFunction(app);
[app, op] = incaio.readFromFile(app);
if string(op) ~= ''
app.OpenPath.Value = op;
end
populateScrollpane(app);
RefreshClicked(app);
catch me
uialert(app.UIFigure, me.message, append('Open Error: ', me.identifier), 'Icon', 'error');
LogExceptions(app, me);
end
figure(app.UIFigure);
pause(0.01);
close(w);
end
function SaveClicked(app, ~)
UpdateLogs(app, "Saving analysis...");
try
f = uiprogressdlg(app.UIFigure, "Title", "Please wait", 'Message', 'Saving...', "Indeterminate", "on");
savePath = incaio.writeToFile(app);
if string(savePath) ~= ''
app.SavePath.Value = savePath;
uialert(app.UIFigure, "Save complete!", 'Message', 'Icon', "success");
figure(app.UIFigure);
end
close(f);
UpdateLogs(app, append("Success! Analysis saved to: ", app.SavePath.Value));
catch me
uialert(app.UIFigure, me.message, append('Save Error: ', me.identifier), 'Icon', 'error');
LogExceptions(app, me);
end
end
function BatchClicked(app, ~)
if app.BatchButton.Value
app.BatchPanel.Enable = 'on';
else
app.BatchPanel.Enable = 'off';
end
end
function ResetDetectionClicked(app, ~)
app.MixerSlider.Value = 50;
calImgThree = rgb2hsv(app.CalibrationFrame.ImageSource);
calImg = calImgThree(:, :, 3);
app.ColorThresholdField.Value = graythresh(calImg);
[~, thresh] = edge(calImg, 'Sobel');
app.EdgeThresholdField.Value = thresh;
end
function ViewResultClicked(app, ~)
f = uiprogressdlg(app.UIFigure, 'Indeterminate', 'on', 'Title', 'Please wait', 'Message', 'Refining...');
calibrationImage = app.frames(:, :, app.workingFrame);
%Normalize the lighting if needed
if app.VLToggle.UserData
calibrationImage = bubbleDetection.normalizeLighting(calibrationImage, lower(string(app.NLButtonGroup.SelectedObject.Text)), app.IFFToggle.UserData, ...
app.frames(:, :, app.BSField.Value));
end
%Increase the contrast if needed
if app.ICToggle.UserData
calibrationImage = bubbleDetection.increaseContrast(calibrationImage);
end
%Remove the timestamps if needed
if app.RTToggle.UserData
calibrationImage = bubbleDetection.removeTimeStamps(calibrationImage);
end
%Set generic old data values
[row, col] = size(calibrationImage);
oldData.Center = [col./2, row./2];
oldData.Size = 0;
try
%Get or set the color threshold value
if ~app.AutoColorToggle.UserData
gt = app.ColorThresholdField.Value;
else
gt = graythresh(calibrationImage);
end
%Get or set the edge threshold value
if ~app.AutoEdgeToggle.UserData
et = app.EdgeThresholdField.Value;
else
[~, et] = edge(calibrationImage, 'Sobel');
end
%Determine if the image needs to be flipped during color
%masking
flip = ~app.BSRadioButton.Value && ~app.VLToggle.UserData;
%Create and mix the color and edge masks
if ~app.MultiviewToggle.UserData
colorMask = bubbleDetection.colorMask(calibrationImage, oldData, gt, lower(string(app.CStyle.Value)), app.CVal.Value, ...
flip);
edgeMask = bubbleDetection.edgeMask(calibrationImage, oldData, et, lower(string(app.EStyle.Value)), app.EVal.Value);
finalMask = bubbleDetection.mixMasks(colorMask, edgeMask, app.MixerSlider.Value);
else
colorMask = bubbleDetection.multiColor(calibrationImage, gt, lower(string(app.CStyle.Value)), app.CVal.Value, ...
flip, 2);
edgeMask = bubbleDetection.multiEdge(calibrationImage, et, lower(string(app.EStyle.Value)), app.EVal.Value, 2);
finalMask = bubbleDetection.mixMasks(colorMask, edgeMask, app.MixerSlider.Value);
end
%Create an rgb matrix representing the calibration
%image
[row, col] = size(calibrationImage);
hsv = zeros(row, col, 3);
hsv(:, :, 3) = calibrationImage;
rgb = hsv2rgb(hsv);
colorImgSrc = rgb; %Create a copy for the color mask image
rlayer = colorImgSrc(:, :, 1); %Separate out the red layer
glayer = colorImgSrc(:, :, 2); %Separate out the green layer
blayer = colorImgSrc(:, :, 3); %Separate out the blue layer
thinColor = bwmorph(colorMask, 'remove'); %Thin the mask to a one pixel thick outline
rlayer(thinColor) = 0; %Set the red layer equal to 0 where the mask is true
glayer(thinColor) = 0; %Set the green layer equal to 0 where the mask is true
blayer(thinColor) = 1; %Set the blue layer equal to 1 where the mask is true
colorImgSrc(:, :, 1) = rlayer; %Recombine the layers
colorImgSrc(:, :, 2) = glayer;
colorImgSrc(:, :, 3) = blayer;
edgeImgSrc = rgb;
rlayer = edgeImgSrc(:, :, 1);
glayer = edgeImgSrc(:, :, 2);
blayer = edgeImgSrc(:, :, 3);
thinEdge = bwmorph(edgeMask, 'remove');
rlayer(thinEdge) = 0;
glayer(thinEdge) = 0;
blayer(thinEdge) = 1;
edgeImgSrc(:, :, 1) = rlayer;
edgeImgSrc(:, :, 2) = glayer;
edgeImgSrc(:, :, 3) = blayer;
%Set the image for the preview images
app.ColorMask.ImageSource = colorImgSrc;
app.EdgeMask.ImageSource = edgeImgSrc;
%Split the masks if needed
if app.MultiviewToggle.UserData
[left, right] = bubbleDetection.separateViews(app.frames(:, :, app.workingFrame), finalMask, 2);
right(right == 1) = 2;
finalMask = left + right;
end
%Show the mask in the final mask viewer
imshow(labeloverlay(app.frames(:, :, app.workingFrame), finalMask, 'Colormap', [0 0 1; 0 1 0; 0 1 1]), 'Parent', app.FinalMaskViewer);
catch ME
uialert(app.UIFigure, ME.message, append('Mask Preview Error: ', ME.identifier), 'Icon', 'error');
LogExceptions(app, ME);
end
close(f);
end
function RefineClicked(app, ~)
f = uiprogressdlg(app.UIFigure, 'Indeterminate', 'on', 'Title', 'Please wait', 'Message', 'Refining...');
calibrationImage = app.frames(:, :, app.workingFrame);
%Normalize the lighting if needed
if app.VLToggle.UserData
calibrationImage = bubbleDetection.normalizeLighting(calibrationImage, lower(string(app.NLButtonGroup.SelectedObject.Text)), app.IFFToggle.UserData, ...
app.frames(:, :, app.BSField.Value));
end
%Increase the contrast if needed
if app.ICToggle.UserData
calibrationImage = bubbleDetection.increaseContrast(calibrationImage);
end
%Remove the timestamps if needed
if app.RTToggle.UserData
calibrationImage = bubbleDetection.removeTimeStamps(calibrationImage);
end
%Set generic old data values
[row, col] = size(calibrationImage);
oldData.Center = [col./2, row./2];
oldData.Size = 0;
try
%Get or set the color threshold value
if ~app.AutoColorToggle.UserData
gt = app.ColorThresholdField.Value;
else
gt = graythresh(calibrationImage);
end
%Get or set the edge threshold value
if ~app.AutoEdgeToggle.UserData
et = app.EdgeThresholdField.Value;
else
[~, et] = edge(calibrationImage, 'Sobel');
end
%Determine if the image needs to be flipped during color
%masking
flip = ~app.BSRadioButton.Value && ~app.VLToggle.UserData;
%Create and mix the color and edge masks
if ~app.MultiviewToggle.UserData
colorMask = bubbleDetection.colorMask(calibrationImage, oldData, gt, lower(string(app.CStyle.Value)), app.CVal.Value, ...
flip);
edgeMask = bubbleDetection.edgeMask(calibrationImage, oldData, et, lower(string(app.EStyle.Value)), app.EVal.Value);
finalMask = bubbleDetection.mixMasks(colorMask, edgeMask, app.MixerSlider.Value);
else
colorMask = bubbleDetection.multiColor(calibrationImage, gt, lower(string(app.CStyle.Value)), app.CVal.Value, ...
flip, 2);
edgeMask = bubbleDetection.multiEdge(calibrationImage, et, lower(string(app.EStyle.Value)), app.EVal.Value, 2);
finalMask = bubbleDetection.mixMasks(colorMask, edgeMask, app.MixerSlider.Value);
end
%Create an rgb matrix representing the calibration
%image
[row, col] = size(calibrationImage);
hsv = zeros(row, col, 3);
hsv(:, :, 3) = calibrationImage;
rgb = hsv2rgb(hsv);
colorImgSrc = rgb; %Create a copy for the color mask image
rlayer = colorImgSrc(:, :, 1); %Separate out the red layer
glayer = colorImgSrc(:, :, 2); %Separate out the green layer
blayer = colorImgSrc(:, :, 3); %Separate out the blue layer
thinColor = bwmorph(colorMask, 'remove'); %Thin the mask to a one pixel thick outline
rlayer(thinColor) = 0; %Set the red layer equal to 0 where the mask is true
glayer(thinColor) = 0; %Set the green layer equal to 0 where the mask is true
blayer(thinColor) = 1; %Set the blue layer equal to 1 where the mask is true
colorImgSrc(:, :, 1) = rlayer; %Recombine the layers
colorImgSrc(:, :, 2) = glayer;
colorImgSrc(:, :, 3) = blayer;
edgeImgSrc = rgb;
rlayer = edgeImgSrc(:, :, 1);
glayer = edgeImgSrc(:, :, 2);
blayer = edgeImgSrc(:, :, 3);
thinEdge = bwmorph(edgeMask, 'remove');
rlayer(thinEdge) = 0;
glayer(thinEdge) = 0;
blayer(thinEdge) = 1;
edgeImgSrc(:, :, 1) = rlayer;
edgeImgSrc(:, :, 2) = glayer;
edgeImgSrc(:, :, 3) = blayer;
%Set the image for the preview images
app.ColorMask.ImageSource = colorImgSrc;
app.EdgeMask.ImageSource = edgeImgSrc;
%Assign the final masks to the output array
if app.MultiviewToggle.UserData
[left, right] = bubbleDetection.separateViews(app.frames(:, :, app.workingFrame), finalMask, 2);
app.mask(:, :, app.workingFrame, 1) = left;
app.mask(:, :, app.workingFrame, 2) = right;
right(right == 1) = 2;
finalMask = left + right;
else
app.mask(:, :, app.workingFrame) = finalMask;
end
%Show the mask in the final mask viewer
imshow(labeloverlay(app.frames(:, :, app.workingFrame), finalMask, 'Colormap', [0 0 1; 0 1 0; 0 1 1]), 'Parent', app.FinalMaskViewer);
catch ME
uialert(app.UIFigure, ME.message, append('Mask Preview Error: ', ME.identifier), 'Icon', 'error');
LogExceptions(app, ME);
end
%Update the scrollpane image
src = findobj(app.Scrollpane, 'Tag', num2str(app.workingFrame));
src.ImageSource = labeloverlay(app.frames(:, :, app.workingFrame), finalMask, 'Colormap', [0 0 1; 0 1 0; 0 1 1]);
%Remove the current frame from the ignore frames list (if its
%there)
app.ignoreFrames = app.ignoreFrames(app.ignoreFrames ~= app.workingFrame);
%Update the calibration image
app.CalibrationFrame.ImageSource = labeloverlay(app.frames(:, :, app.workingFrame), finalMask, 'Colormap', [0 0 1; 0 1 0; 0 1 1]);
close(f);
end
function RunDetection(app, ~)
try
%Enable final mask viewer buttons
app.NextFrameDetectionButton.Enable = 'on';
app.PreviousFrameDetectionButton.Enable = 'on';
app.AcceptFrameButton.Enable = 'on';
app.RejectFrameButton.Enable = 'on';