-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlantSeg_exported.m
1267 lines (1091 loc) · 60.8 KB
/
PlantSeg_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 PlantSeg_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
AnalysisPanel matlab.ui.container.Panel
SaveButton matlab.ui.control.Button
ShowComponentsButton matlab.ui.control.Button
NumComponentsEditField matlab.ui.control.NumericEditField
CompsEditFieldLabel matlab.ui.control.Label
Gentype2EditField matlab.ui.control.NumericEditField
Gentype2Label matlab.ui.control.Label
Gentype1EditField matlab.ui.control.NumericEditField
Gentype1EditFieldLabel matlab.ui.control.Label
ConditionEditField matlab.ui.control.NumericEditField
ConditionEditFieldLabel matlab.ui.control.Label
PlateEditField matlab.ui.control.NumericEditField
PlateEditFieldLabel matlab.ui.control.Label
RootsEditField_4 matlab.ui.control.NumericEditField
RootsEditField_3 matlab.ui.control.NumericEditField
RootsEditField_2 matlab.ui.control.NumericEditField
RootsLabel matlab.ui.control.Label
RootsEditField_1 matlab.ui.control.NumericEditField
ShootsEditField_4 matlab.ui.control.NumericEditField
ShootsEditField_3 matlab.ui.control.NumericEditField
ShootsEditField_2 matlab.ui.control.NumericEditField
ShootsLabel matlab.ui.control.Label
ShootsEditField_1 matlab.ui.control.NumericEditField
RunAnalysisButton matlab.ui.control.Button
TabGroup matlab.ui.container.TabGroup
MainTab matlab.ui.container.Tab
ExportDataButton matlab.ui.control.Button
AutoSegmentButton matlab.ui.control.Button
LoadButton matlab.ui.control.Button
SegmentationPanel matlab.ui.container.Panel
ShowMaskButton matlab.ui.control.StateButton
IslandsSpinnerRoot matlab.ui.control.Spinner
IslandsSpinner_3Label matlab.ui.control.Label
IslandsSpinnerShoot matlab.ui.control.Spinner
IslandsSpinner_2Label matlab.ui.control.Label
IslandsSpinnerPlant matlab.ui.control.Spinner
IslandsSpinnerLabel matlab.ui.control.Label
RootCheckBox matlab.ui.control.CheckBox
ShootCheckBox matlab.ui.control.CheckBox
PlantCheckBox matlab.ui.control.CheckBox
NextButton matlab.ui.control.Button
PrevButton matlab.ui.control.Button
CurrentImageDropDown matlab.ui.control.DropDown
BrushButtonGroup matlab.ui.container.ButtonGroup
ResetAllButton matlab.ui.control.Button
RootLamp matlab.ui.control.Lamp
ShootLamp matlab.ui.control.Lamp
BackgroundLamp matlab.ui.control.Lamp
BackgroundButton matlab.ui.control.ToggleButton
ShootButton matlab.ui.control.ToggleButton
RootButton matlab.ui.control.ToggleButton
NewButton matlab.ui.control.Button
MaskAppearanceTab matlab.ui.container.Tab
SetUncertainColorButton matlab.ui.control.Button
SetRootColorButton matlab.ui.control.Button
SetShootColorButton matlab.ui.control.Button
SetBackgroundColorButton matlab.ui.control.Button
MaskVisibilityAndColorLabel matlab.ui.control.Label
CheckBoxShowUncertain matlab.ui.control.CheckBox
CheckBoxShowRoot matlab.ui.control.CheckBox
CheckBoxShowShoot matlab.ui.control.CheckBox
CheckBoxShowBackground matlab.ui.control.CheckBox
SliderMaskAlpha matlab.ui.control.Slider
MaskAlphaLabel matlab.ui.control.Label
HelpTab matlab.ui.container.Tab
StepsTextArea matlab.ui.control.TextArea
StepsTextAreaLabel matlab.ui.control.Label
UIAxesPlant_4 matlab.ui.control.UIAxes
UIAxesPlant_3 matlab.ui.control.UIAxes
UIAxesPlant_2 matlab.ui.control.UIAxes
UIAxesPlant_1 matlab.ui.control.UIAxes
UIAxesSeg matlab.ui.control.UIAxes
end
% Private variables:
properties (Access = private)
% These parameters can be changed:
settings = struct( ...
'bgColor', [0, 0, 0], ... % Color for displaying background pixels.
'shootColor', [0, 1, 1], ... % Color for displaying shoot pixels.
'rootColor', [1, 0, 0], ... % Color for displaying root pixels.
'uncertainColor', [1, 1, 0], ...% Color for displaying uncertain pixels.
'maskAlpha', 0.5, ... % Mask transparency value.
'showBackgroundColor', false, ...% Toggle display of background color.
'showShootColor', true, ... % Toggle display of shoot color.
'showRootColor', true, ... % Toggle display of root color.
'showUncertainColor', true, ... % Toggle display of uncertain color.
'plantSegEnabled', true, ... % Enable plant segmentation.
'shootSegEnabled', true, ... % Enable shoot segmentation.
'rootSegEnabled', true, ... % Enable root segmentation.
'plantSegIslands', 200, ... % Maximum size of pixel islands allowed.
'shootSegIslands', 150, ... % Maximum size of pixel islands allowed.
'rootSegIslands', 150 ... % Maximum size of pixel islands allowed.
);
data = struct;
% These parameters should not be changed unless you know what you
% are doing:
dataMagicString = "PlantSegData: v1.0"; % For file save/load.
saveFile = "none"; % Data filename.
bgMaskValue = 0; % Value of background pixels in mask image.
erasedMaskValue = 10; % Values of erased pixels in mask image.
shootMaskValue = 100; % Value of shoot pixels in mask image.
rootMaskValue = 150; % Value of root pixels in mask image.
uncertainMaskValue = 250; % Value of uncertain (root or shoot) pixels in mask image.
activeMaskValue = 0; % Value to be painted in mask using mouse.
segColorMap = 0;
imagePlot = 0; % Handle to main image plot.
mouseDown = false; % Is left mouse button currently pressed?
inputFiles = []; % List of input filenames.
% Data that changes per image:
inputFilename = ""; % Filename of current image with directory.
inputImage = 0; % Input image with 3 channels RGB of uint8.
mask = 0; % Output mask (uint8 single channel).
%maskPainted = 0; % User's manual segmentation (used for input erasures and mask corrections).
maskPlant1 = 0; % Output mask for plant 1.
maskPlant2 = 0; % Output mask for plant 2.
maskPlant3 = 0; % Output mask for plant 3.
maskPlant4 = 0; % Output mask for plant 4.
% Data that changes per session:
inputDir = ""; % File folder or directory where all input images are.
curInputIndex = 1; % Index of current image from from list.
end
% Private functions:
methods (Access = private)
function SaveSettings(app)
appSettings = app.settings;
save(app.saveFile, 'appSettings', '-append');
end
function LoadSettings(app, loadedData)
app.settings = loadedData.appSettings;
end
function SaveCurrentImageData(app)
if(sum(app.mask, "all") > 0)
imageFieldname = GetImageFieldName(app, app.inputFilename);
if(~isfield(app, imageFieldname))
app.data.(imageFieldname) = 0;
end
app.data.(imageFieldname) = struct( ...
'inputImage', app.inputImage, ...
'mask', app.mask, ... %'maskPainted', app.maskPainted, ...
'maskPlant1', app.maskPlant1, ...
'maskPlant2', app.maskPlant2, ...
'maskPlant3', app.maskPlant3, ...
'maskPlant4', app.maskPlant4, ...
'metadata', [app.PlateEditField.Value app.ConditionEditField.Value app.Gentype1EditField.Value app.Gentype2EditField.Value], ...
'shoots', [app.ShootsEditField_1.Value app.ShootsEditField_2.Value app.ShootsEditField_3.Value app.ShootsEditField_4.Value], ...
'roots', [app.RootsEditField_1.Value app.RootsEditField_2.Value app.RootsEditField_3.Value app.RootsEditField_4.Value] ...
);
end
appData = app.data;
save(app.saveFile, 'appData', '-append');
end
function imageFieldname = GetImageFieldName(~, imageFilename)
[~,filename,~] = fileparts(imageFilename);
imageFieldname = "i" + filename;
end
function UpdateSegmentationPlot(app)
cla(app.UIAxesSeg);
imagesc(app.inputImage, "Parent", app.UIAxesSeg);
hold(app.UIAxesSeg, "on");
app.imagePlot = imagesc(app.mask, "Parent", app.UIAxesSeg);
colormap(app.UIAxesSeg, app.segColorMap);
caxis(app.UIAxesSeg, [0 app.uncertainMaskValue]);
if(app.ShowMaskButton.Value)
alphaImage = ComputeMaskAlphaImage(app);
else
alphaImage = zeros(size(app.mask));
end
set(app.imagePlot, 'AlphaData', alphaImage);
app.UIAxesSeg.XLim = [0 app.imagePlot.XData(2)];
app.UIAxesSeg.YLim = [0 app.imagePlot.YData(2)];
hold(app.UIAxesSeg, "off")
set(app.imagePlot, 'Tag', 'WindowTag')
end
function InitializeVariables(app)
app.SliderMaskAlpha.Value = app.settings.maskAlpha;
app.CheckBoxShowBackground.Value = app.settings.showBackgroundColor;
app.BackgroundLamp.Color = app.settings.bgColor;
app.CheckBoxShowShoot.Value = app.settings.showShootColor;
app.ShootLamp.Color = app.settings.shootColor;
app.CheckBoxShowRoot.Value = app.settings.showRootColor;
app.RootLamp.Color = app.settings.rootColor;
app.CheckBoxShowUncertain.Value = app.settings.showUncertainColor;
app.segColorMap = [app.settings.bgColor; app.settings.shootColor; app.settings.rootColor; app.settings.uncertainColor];
app.PlantCheckBox.Value = app.settings.plantSegEnabled;
app.ShootCheckBox.Value = app.settings.shootSegEnabled;
app.RootCheckBox.Value = app.settings.rootSegEnabled;
app.IslandsSpinnerPlant.Value = app.settings.plantSegIslands;
app.IslandsSpinnerShoot.Value = app.settings.shootSegIslands;
app.IslandsSpinnerRoot.Value = app.settings.rootSegIslands;
app.ShowMaskButton.Value = true;
end
function RecomputeSegmentationMask(app)
if(app.RootCheckBox.Value)
rootsMask = SegmentRoots(app.inputImage, app.IslandsSpinnerRoot.Value, app.PlantCheckBox.Value, app.IslandsSpinnerPlant.Value);
end
if(app.ShootCheckBox.Value)
shootsMask = SegmentShoots(app.inputImage, app.IslandsSpinnerShoot.Value, app.PlantCheckBox.Value, app.IslandsSpinnerPlant.Value);
end
if(app.RootCheckBox.Value && app.ShootCheckBox.Value)
app.mask = (uint8(rootsMask) * app.rootMaskValue) + (uint8(shootsMask) * app.shootMaskValue);
elseif(app.RootCheckBox.Value && ~app.ShootCheckBox.Value)
plantMask = SegmentPlant(app.inputImage, app.IslandsSpinnerPlant.Value);
shootsMask = plantMask - rootsMask;
shootsMask = bwareaopen(shootsMask, app.IslandsSpinnerShoot.Value);
app.mask = (uint8(rootsMask) * app.rootMaskValue) + (uint8((shootsMask)) * app.shootMaskValue);
elseif(~app.RootCheckBox.Value && app.ShootCheckBox.Value)
plantMask = SegmentPlant(app.inputImage, app.IslandsSpinnerPlant.Value);
rootsMask = plantMask - shootsMask;
rootsMask = bwareaopen(rootsMask, app.IslandsSpinnerRoot.Value);
app.mask = (uint8(shootsMask) * app.shootMaskValue) + (uint8((rootsMask)) * app.rootMaskValue);
else
app.mask = zeros(size(app.inputImage), 'uint8');
app.mask = app.mask(:,:,1);
end
end
function UpdateMaskFromMouse(app, event)
mouseButtonFlag = get(event.Source,'SelectionType');
mousePos = get(app.UIAxesSeg,'CurrentPoint');
mouseRowPos = round(mousePos(1,2));
mouseColPos = round(mousePos(1,1));
if(strcmp(mouseButtonFlag, 'normal'))
app.mask(mouseRowPos, mouseColPos) = app.activeMaskValue;
%app.maskPainted(mouseRowPos, mouseColPos) = app.activeMaskValue;
alphaImage = ComputeMaskAlphaImage(app);
set(app.imagePlot, 'CData', app.mask, 'alphadata', alphaImage);
else
app.inputImage(mouseRowPos, mouseColPos, :) = app.erasedMaskValue;
app.mask(mouseRowPos, mouseColPos) = app.erasedMaskValue;
alphaImage = ComputeMaskAlphaImage(app);
set(app.imagePlot, 'CData', app.inputImage, 'alphadata', alphaImage);
end
end
function alphaImage = ComputeMaskAlphaImage(app)
alphaImage = ones(size(app.mask));
if ~app.CheckBoxShowBackground.Value
alphaImage = alphaImage - single(app.mask == app.bgMaskValue);
end
if ~app.CheckBoxShowShoot.Value
alphaImage = alphaImage - single(app.mask == app.shootMaskValue);
end
if ~app.CheckBoxShowRoot.Value
alphaImage = alphaImage - single(app.mask == app.rootMaskValue);
end
if ~app.CheckBoxShowUncertain.Value
alphaImage = alphaImage - single(app.mask == app.uncertainMaskValue);
end
alphaImage = alphaImage * app.settings.maskAlpha;
end
function result = GetTifImageFiles(~, inputDir)
listing = dir(strcat(inputDir, filesep, '*.tif'));
numFilenames = size(listing, 1);
fileIndex = 1;
result = strings(1, size(listing, 1));
while(fileIndex <= numFilenames)
result(1, fileIndex) = string(listing(fileIndex).name);
fileIndex = fileIndex + 1;
end
end
function ResetMask(app)
%app.maskPainted = ones([size(app.inputImage, 1) size(app.inputImage, 2)], 'uint8') * app.bgMaskValue;
app.mask = ones([size(app.inputImage, 1) size(app.inputImage, 2)], 'uint8') * app.bgMaskValue;
UpdateSegmentationPlot(app);
end
function LoadMask(app, imageFieldname)
%app.maskPainted = app.data.(imageFieldname).maskPainted;
app.mask = app.data.(imageFieldname).mask;
UpdateSegmentationPlot(app);
end
function ResetAnalysis(app)
app.PlateEditField.Value = 0;
app.ConditionEditField.Value = 0;
app.Gentype1EditField.Value = 0;
app.Gentype2EditField.Value = 0;
app.NumComponentsEditField.Value = 0;
app.ShootsEditField_1.Value = 0;
app.ShootsEditField_2.Value = 0;
app.ShootsEditField_3.Value = 0;
app.ShootsEditField_4.Value = 0;
app.RootsEditField_1.Value = 0;
app.RootsEditField_2.Value = 0;
app.RootsEditField_3.Value = 0;
app.RootsEditField_4.Value = 0;
app.NumComponentsEditField.BackgroundColor = [1 1 1];
app.maskPlant1 = 0;
app.maskPlant2 = 0;
app.maskPlant3 = 0;
app.maskPlant4 = 0;
cla(app.UIAxesPlant_1);
cla(app.UIAxesPlant_2);
cla(app.UIAxesPlant_3);
cla(app.UIAxesPlant_4);
end
function LoadAnalysis(app, imageFieldname)
metadata = app.data.(imageFieldname).metadata;
shoots = app.data.(imageFieldname).shoots;
roots = app.data.(imageFieldname).roots;
app.maskPlant1 = app.data.(imageFieldname).maskPlant1;
app.maskPlant2 = app.data.(imageFieldname).maskPlant2;
app.maskPlant3 = app.data.(imageFieldname).maskPlant3;
app.maskPlant4 = app.data.(imageFieldname).maskPlant4;
app.PlateEditField.Value = metadata(1);
app.ConditionEditField.Value = metadata(2);
app.Gentype1EditField.Value = metadata(3);
app.Gentype2EditField.Value = metadata(4);
app.NumComponentsEditField.Value = 0;
app.ShootsEditField_1.Value = shoots(1);
app.ShootsEditField_2.Value = shoots(2);
app.ShootsEditField_3.Value = shoots(3);
app.ShootsEditField_4.Value = shoots(4);
app.RootsEditField_1.Value = roots(1);
app.RootsEditField_2.Value = roots(2);
app.RootsEditField_3.Value = roots(3);
app.RootsEditField_4.Value = roots(4);
app.NumComponentsEditField.BackgroundColor = [1 1 1];
PlotPlantMask(app, app.maskPlant1, app.UIAxesPlant_1, app.segColorMap, [0 app.uncertainMaskValue]);
PlotPlantMask(app, app.maskPlant2, app.UIAxesPlant_2, app.segColorMap, [0 app.uncertainMaskValue]);
PlotPlantMask(app, app.maskPlant3, app.UIAxesPlant_3, app.segColorMap, [0 app.uncertainMaskValue]);
PlotPlantMask(app, app.maskPlant4, app.UIAxesPlant_4, app.segColorMap, [0 app.uncertainMaskValue]);
end
function ReloadCurrentImage(app)
app.inputFilename = strcat(app.inputDir, filesep, app.inputFiles(app.curInputIndex));
app.inputImage = imread(app.inputFilename);
end
function LoadImage(app, imageFieldname)
app.inputFilename = strcat(app.inputDir, filesep, app.inputFiles(app.curInputIndex));
app.inputImage = app.data.(imageFieldname).inputImage;
end
function LoadNewImage(app)
imageFieldname = GetImageFieldName(app, app.inputFiles(app.curInputIndex));
if(isfield(app.data, imageFieldname))
LoadImage(app, imageFieldname);
LoadMask(app, imageFieldname);
LoadAnalysis(app, imageFieldname);
else
ReloadCurrentImage(app);
ResetMask(app);
ResetAnalysis(app);
end
end
function UpdateSegmentationPlotAppearance(app)
if(app.ShowMaskButton.Value)
alphaImage = ComputeMaskAlphaImage(app);
else
alphaImage = zeros(size(app.mask));
end
set(app.imagePlot, 'CData', app.mask, 'alphadata', alphaImage);
app.segColorMap = [app.settings.bgColor; app.settings.shootColor; app.settings.rootColor; app.settings.uncertainColor];
colormap(app.UIAxesSeg, app.segColorMap);
caxis(app.UIAxesSeg, [0 app.uncertainMaskValue]);
end
function [plantMask, shootArea, rootArea] = GetPlantStats(app, bbox, plantPixelIndexList)
left = round(bbox(1));
top = round(bbox(2));
width = round(bbox(3));
height = round(bbox(4));
tempMask = app.mask .* 0;
tempMask(plantPixelIndexList) = 1;
tempMask = tempMask .* app.mask;
plantMask = tempMask(top:top+height, left:left+width, :);
% We count uncertain pixels as both shoots and roots:
shootArea = sum((plantMask == app.shootMaskValue) | (plantMask == app.uncertainMaskValue),'all');
rootArea = sum((plantMask == app.rootMaskValue) | (plantMask == app.uncertainMaskValue),'all');
end
function PlotPlantMask(~, plantMask, uiAxes, plotColorMap, valueRange)
cla(uiAxes);
if(plantMask == 0)
return;
end
plot = imshow(plantMask, 'Parent', uiAxes);
colormap(uiAxes, plotColorMap);
caxis(uiAxes, valueRange);
uiAxes.XLim = [0 plot.XData(2)];
uiAxes.YLim = [0 plot.YData(2)];
end
function [connectedComponents, numDetectedObjects] = ComputePlantConnectedComponents(app, filterTopFourComponents)
% Note that connected components are, by default, sorted from
% left to right, then top to bottom.
plantMask = (app.mask == app.rootMaskValue | app.mask == app.shootMaskValue | app.mask == app.uncertainMaskValue);
connectedComponents = bwconncomp(plantMask);
numDetectedObjects = connectedComponents.NumObjects;
app.NumComponentsEditField.Value = numDetectedObjects;
if(numDetectedObjects ~= 4)
app.NumComponentsEditField.BackgroundColor = [0.8, 0, 0];
else
app.NumComponentsEditField.BackgroundColor = [1, 1, 1];
end
if(filterTopFourComponents)
% Get top four components then redo connected component
% analysis (keep original number of components):
plantMask = bwareafilt(plantMask, 4);
connectedComponents = bwconncomp(plantMask);
end
end
function PlotConnectedComponents(app)
connectedComponents = ComputePlantConnectedComponents(app, false);
labels = labelmatrix(connectedComponents);
coloredLabels = label2rgb(labels, @lines, "black", "shuffle");
imshow(coloredLabels);
end
function ClearPlantMasksAndPlots(app)
app.maskPlant1 = 0;
app.maskPlant2 = 0;
app.maskPlant3 = 0;
app.maskPlant4 = 0;
cla(app.UIAxesPlant_1);
cla(app.UIAxesPlant_2);
cla(app.UIAxesPlant_3);
cla(app.UIAxesPlant_4);
app.RootsEditField_1.Value = 0;
app.RootsEditField_2.Value = 0;
app.RootsEditField_3.Value = 0;
app.RootsEditField_4.Value = 0;
app.ShootsEditField_1.Value = 0;
app.ShootsEditField_2.Value = 0;
app.ShootsEditField_3.Value = 0;
app.ShootsEditField_4.Value = 0;
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
InitializeVariables(app);
end
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
if(strcmp(app.saveFile, "none"))
app.saveFile = uiputfile('*.mat', "Save PlantSeg Data" );
end
if(app.saveFile)
app_dataMagicString = app.dataMagicString;
save(app.saveFile, 'app_dataMagicString');
app_inputDir = app.inputDir;
save(app.saveFile, 'app_inputDir', '-append');
app_curInputIndex = app.curInputIndex;
save(app.saveFile, 'app_curInputIndex', '-append');
SaveSettings(app);
SaveCurrentImageData(app);
end
end
% Button pushed function: LoadButton
function LoadButtonPushed(app, event)
app.saveFile = uigetfile('*.mat', "Load PlantSeg Data");
if(app.saveFile)
loadedData = load(app.saveFile);
if(isfield(loadedData, 'appData'))
app.data = loadedData.appData;
end
end
if(~isfield(loadedData, 'app_dataMagicString') || ~strcmp(app.dataMagicString, loadedData.app_dataMagicString))
message = {'Invalid PlantSeg file!','Please load a valid PlantSeg data file.'};
uialert(app.UIFigure,message,'Warning',...
'Icon','warning');
return;
end
LoadSettings(app, loadedData);
InitializeVariables(app);
app.inputDir = loadedData.app_inputDir;
app.inputFiles = GetTifImageFiles(app, app.inputDir);
app.CurrentImageDropDown.Items = app.inputFiles;
app.CurrentImageDropDown.ItemsData = 1 : size(app.inputFiles, 2);
app.curInputIndex = loadedData.app_curInputIndex;
set(app.CurrentImageDropDown, 'Value', app.curInputIndex);
LoadNewImage(app);
end
% Button pushed function: NewButton
function NewButtonPushed(app, event)
app.inputDir = uigetdir;
app.inputFiles = GetTifImageFiles(app, app.inputDir);
app.curInputIndex = 1;
app.CurrentImageDropDown.Items = app.inputFiles;
app.CurrentImageDropDown.ItemsData = 1 : size(app.inputFiles, 2);
LoadNewImage(app);
end
% Value changed function: SliderMaskAlpha
function SliderMaskAlphaValueChanged(app, event)
app.settings.maskAlpha = app.SliderMaskAlpha.Value;
UpdateSegmentationPlotAppearance(app);
end
% Window button down function: UIFigure
function UIFigureWindowButtonDown(app, event)
if ~isempty(event.Source.CurrentObject) && isequal(event.Source.CurrentObject.Tag,'WindowTag')
app.mouseDown = true;
UpdateMaskFromMouse(app, event);
end
end
% Window button motion function: UIFigure
function UIFigureWindowButtonMotion(app, event)
if ~isempty(event.Source.CurrentObject) && isequal(event.Source.CurrentObject.Tag,'WindowTag')
if(app.mouseDown)
UpdateMaskFromMouse(app, event);
end
end
end
% Window button up function: UIFigure
function UIFigureWindowButtonUp(app, event)
if ~isempty(event.Source.CurrentObject) && isequal(event.Source.CurrentObject.Tag,'WindowTag')
app.mouseDown = false;
end
end
% Selection changed function: BrushButtonGroup
function BrushButtonGroupSelectionChanged(app, event)
selectedButton = app.BrushButtonGroup.SelectedObject;
if(strcmp(selectedButton.Text, "Background"))
app.activeMaskValue = app.bgMaskValue;
end
if(strcmp(selectedButton.Text, "Root"))
app.activeMaskValue = app.rootMaskValue;
end
if(strcmp(selectedButton.Text, "Shoot"))
app.activeMaskValue = app.shootMaskValue;
end
end
% Value changed function: CurrentImageDropDown
function CurrentImageDropDownValueChanged(app, event)
app.curInputIndex = app.CurrentImageDropDown.Value;
LoadNewImage(app);
end
% Button pushed function: NextButton
function NextButtonPushed(app, event)
if (app.curInputIndex == size(app.inputFiles, 2))
message = {'End of files list!','Use dropdown menu to select file.'};
uialert(app.UIFigure,message,'Warning',...
'Icon','warning');
else
app.curInputIndex = app.curInputIndex + 1;
set(app.CurrentImageDropDown, 'Value', app.curInputIndex);
LoadNewImage(app);
end
end
% Button pushed function: PrevButton
function PrevButtonPushed(app, event)
if (app.curInputIndex == 1)
message = {'Beginning of files list!','Use dropdown menu to select file.'};
uialert(app.UIFigure,message,'Warning',...
'Icon','warning');
else
app.curInputIndex = app.curInputIndex - 1;
set(app.CurrentImageDropDown, 'Value', app.curInputIndex);
LoadNewImage(app);
end
end
% Button pushed function: AutoSegmentButton
function AutoSegmentButtonPushed(app, event)
RecomputeSegmentationMask(app);
UpdateSegmentationPlot(app);
end
% Button pushed function: ResetAllButton
function ResetAllButtonPushed(app, event)
ReloadCurrentImage(app); % We do this to reset the manually enforced changes to the input.
ResetMask(app);
end
% Value changed function: CheckBoxShowBackground
function CheckBoxShowBackgroundValueChanged(app, event)
app.settings.showBackgroundColor = app.CheckBoxShowBackground.Value;
UpdateSegmentationPlotAppearance(app);
end
% Value changed function: CheckBoxShowShoot
function CheckBoxShowShootValueChanged(app, event)
app.settings.showShootColor = app.CheckBoxShowShoot.Value;
UpdateSegmentationPlotAppearance(app);
end
% Value changed function: CheckBoxShowRoot
function CheckBoxShowRootValueChanged(app, event)
app.settings.showRootColor = app.CheckBoxShowRoot.Value;
UpdateSegmentationPlotAppearance(app);
end
% Value changed function: CheckBoxShowUncertain
function CheckBoxShowUncertainValueChanged(app, event)
app.settings.showUncertainColor = app.CheckBoxShowUncertain.Value;
UpdateSegmentationPlotAppearance(app);
end
% Callback function
function ToggleMaskVisibilityButtonPushed(app, event)
ToggleMaskVisibility(app);
UpdateSegmentationPlot(app);
end
% Button pushed function: RunAnalysisButton
function RunAnalysisButtonPushed(app, event)
[connectedComponents, numDetectedObjects] = ComputePlantConnectedComponents(app, true);
stats = regionprops(connectedComponents, "basic");
if(numDetectedObjects == 1)
[bbox1] = stats.BoundingBox;
end
if(numDetectedObjects == 2)
[bbox1, bbox2] = stats.BoundingBox;
end
if(numDetectedObjects == 3)
[bbox1, bbox2, bbox3] = stats.BoundingBox;
end
if(numDetectedObjects >= 4)
[bbox1, bbox2, bbox3, bbox4] = stats.BoundingBox;
end
ClearPlantMasksAndPlots(app);
if(numDetectedObjects > 0)
[app.maskPlant1, app.ShootsEditField_1.Value, app.RootsEditField_1.Value] = GetPlantStats(app, bbox1, connectedComponents.PixelIdxList{1});
PlotPlantMask(app, app.maskPlant1, app.UIAxesPlant_1, app.segColorMap, [0 app.uncertainMaskValue]);
end
if(numDetectedObjects > 1)
[app.maskPlant2, app.ShootsEditField_2.Value, app.RootsEditField_2.Value] = GetPlantStats(app, bbox2, connectedComponents.PixelIdxList{2});
PlotPlantMask(app, app.maskPlant2, app.UIAxesPlant_2, app.segColorMap, [0 app.uncertainMaskValue]);
end
if(numDetectedObjects > 2)
[app.maskPlant3, app.ShootsEditField_3.Value, app.RootsEditField_3.Value] = GetPlantStats(app, bbox3, connectedComponents.PixelIdxList{3});
PlotPlantMask(app, app.maskPlant3, app.UIAxesPlant_3, app.segColorMap, [0 app.uncertainMaskValue]);
end
if(numDetectedObjects > 3)
[app.maskPlant4, app.ShootsEditField_4.Value, app.RootsEditField_4.Value] = GetPlantStats(app, bbox4, connectedComponents.PixelIdxList{4});
PlotPlantMask(app, app.maskPlant4, app.UIAxesPlant_4, app.segColorMap, [0 app.uncertainMaskValue]);
end
% Try to detect digits on image:
% if(app.PlateEditField.Value == 0)
% imageHistEq = histeq(app.inputImage);
%
% app.PlateEditField.Value = DetectDigits(imageHistEq, [32.0000 22.0000 282 212]);
% app.ConditionEditField.Value = DetectDigits(imageHistEq, [673 21.0000 302 221]);
% app.Gentype1EditField.Value = DetectDigits(imageHistEq, [34 771 291 209]);
% app.Gentype2EditField.Value = DetectDigits(imageHistEq, [661 733 313 250]);
% end
end
% Value changed function: ShowMaskButton
function ShowMaskButtonValueChanged(app, event)
%UpdateSegmentationPlot(app);
UpdateSegmentationPlotAppearance(app);
end
% Button pushed function: ShowComponentsButton
function ShowComponentsButtonPushed(app, event)
PlotConnectedComponents(app);
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
delete(app)
close all;
end
% Button pushed function: ExportDataButton
function ExportDataButtonPushed(app, event)
exportFile = uiputfile('*.csv', "Save PlantSeg Metadata");
fn = fieldnames(app.data);
numFiles = numel(fn);
FileName = strings([numFiles 1]);
PlateNumber = strings([numFiles 1]);
Condition = strings([numFiles 1]);
GenType1 = strings([numFiles 1]);
GenType2 = strings([numFiles 1]);
Shoots1 = strings([numFiles 1]);
Shoots2 = strings([numFiles 1]);
Shoots3 = strings([numFiles 1]);
Shoots4 = strings([numFiles 1]);
Roots1 = strings([numFiles 1]);
Roots2 = strings([numFiles 1]);
Roots3 = strings([numFiles 1]);
Roots4 = strings([numFiles 1]);
for k = 1:numel(fn)
imageFieldname = fn{k};
FileName(k) = imageFieldname;
metadata = app.data.(imageFieldname).metadata;
shootsdata = app.data.(imageFieldname).shoots;
rootsdata = app.data.(imageFieldname).roots;
PlateNumber(k) = string(metadata(1));
Condition(k) = string(metadata(2));
GenType1(k) = string(metadata(3));
GenType2(k) = string(metadata(4));
Shoots1(k) = string(shootsdata(1));
Shoots2(k) = string(shootsdata(2));
Shoots3(k) = string(shootsdata(3));
Shoots4(k) = string(shootsdata(4));
Roots1(k) = string(rootsdata(1));
Roots2(k) = string(rootsdata(2));
Roots3(k) = string(rootsdata(3));
Roots4(k) = string(rootsdata(4));
end
exportTable = table(FileName, PlateNumber, Condition, GenType1, GenType2, ...
Shoots1, Shoots2, Shoots3, Shoots4, ...
Roots1, Roots2, Roots3, Roots4);
writetable(exportTable, exportFile);
end
% Button pushed function: SetBackgroundColorButton
function SetBackgroundColorButtonPushed(app, event)
app.settings.bgColor = uisetcolor(app.settings.bgColor);
app.BackgroundLamp.Color = app.settings.bgColor;
UpdateSegmentationPlotAppearance(app);
end
% Button pushed function: SetShootColorButton
function SetShootColorButtonPushed(app, event)
app.settings.shootColor = uisetcolor(app.settings.shootColor);
app.ShootLamp.Color = app.settings.shootColor;
UpdateSegmentationPlotAppearance(app);
end
% Button pushed function: SetRootColorButton
function SetRootColorButtonPushed(app, event)
app.settings.rootColor = uisetcolor(app.settings.rootColor);
app.RootLamp.Color = app.settings.rootColor;
UpdateSegmentationPlotAppearance(app);
end
% Button pushed function: SetUncertainColorButton
function SetUncertainColorButtonPushed(app, event)
app.settings.uncertainColor = uisetcolor(app.settings.uncertainColor);
UpdateSegmentationPlotAppearance(app);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 911 842];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
app.UIFigure.WindowButtonDownFcn = createCallbackFcn(app, @UIFigureWindowButtonDown, true);
app.UIFigure.WindowButtonUpFcn = createCallbackFcn(app, @UIFigureWindowButtonUp, true);
app.UIFigure.WindowButtonMotionFcn = createCallbackFcn(app, @UIFigureWindowButtonMotion, true);
% Create UIAxesSeg
app.UIAxesSeg = uiaxes(app.UIFigure);
app.UIAxesSeg.XTick = [];
app.UIAxesSeg.YTick = [];
app.UIAxesSeg.Box = 'on';
app.UIAxesSeg.Position = [2 217 600 600];
% Create UIAxesPlant_1
app.UIAxesPlant_1 = uiaxes(app.UIFigure);
app.UIAxesPlant_1.XTick = [];
app.UIAxesPlant_1.YTick = [];
app.UIAxesPlant_1.Box = 'on';
app.UIAxesPlant_1.Position = [600 516 150 300];
% Create UIAxesPlant_2
app.UIAxesPlant_2 = uiaxes(app.UIFigure);
app.UIAxesPlant_2.XTick = [];
app.UIAxesPlant_2.YTick = [];
app.UIAxesPlant_2.Box = 'on';
app.UIAxesPlant_2.Position = [749 516 150 300];
% Create UIAxesPlant_3
app.UIAxesPlant_3 = uiaxes(app.UIFigure);
app.UIAxesPlant_3.XTick = [];
app.UIAxesPlant_3.YTick = [];
app.UIAxesPlant_3.Box = 'on';
app.UIAxesPlant_3.Position = [600 218 150 300];
% Create UIAxesPlant_4
app.UIAxesPlant_4 = uiaxes(app.UIFigure);
app.UIAxesPlant_4.XTick = [];
app.UIAxesPlant_4.YTick = [];
app.UIAxesPlant_4.Box = 'on';
app.UIAxesPlant_4.Position = [749 218 150 300];
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [6 9 590 210];
% Create MainTab
app.MainTab = uitab(app.TabGroup);
app.MainTab.Title = 'Main';
% Create NewButton
app.NewButton = uibutton(app.MainTab, 'push');
app.NewButton.ButtonPushedFcn = createCallbackFcn(app, @NewButtonPushed, true);
app.NewButton.BackgroundColor = [0.8196 0.9882 0.7294];
app.NewButton.Tooltip = {'Start a new session by selecting a folder where all input tif images are located.'};
app.NewButton.Position = [5 150 50 23];
app.NewButton.Text = 'New';
% Create BrushButtonGroup
app.BrushButtonGroup = uibuttongroup(app.MainTab);
app.BrushButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @BrushButtonGroupSelectionChanged, true);
app.BrushButtonGroup.Title = 'Brush:';
app.BrushButtonGroup.Position = [322 4 143 136];
% Create RootButton
app.RootButton = uitogglebutton(app.BrushButtonGroup);
app.RootButton.Tooltip = {'Set brush to paint roots pixels.'};
app.RootButton.Text = 'Root';
app.RootButton.Position = [9 32 100 23];
% Create ShootButton
app.ShootButton = uitogglebutton(app.BrushButtonGroup);
app.ShootButton.Tooltip = {'Set brush to paint shoots pixels.'};
app.ShootButton.Text = 'Shoot';
app.ShootButton.Position = [9 60 100 23];
% Create BackgroundButton
app.BackgroundButton = uitogglebutton(app.BrushButtonGroup);
app.BackgroundButton.Tooltip = {'Set brush to paint background pixels.'};
app.BackgroundButton.Text = 'Background';
app.BackgroundButton.Position = [9 88 100 23];
app.BackgroundButton.Value = true;
% Create BackgroundLamp
app.BackgroundLamp = uilamp(app.BrushButtonGroup);
app.BackgroundLamp.Position = [117 90 20 20];
% Create ShootLamp
app.ShootLamp = uilamp(app.BrushButtonGroup);
app.ShootLamp.Position = [117 60 20 20];
% Create RootLamp
app.RootLamp = uilamp(app.BrushButtonGroup);
app.RootLamp.Position = [117 33 20 20];
% Create ResetAllButton
app.ResetAllButton = uibutton(app.BrushButtonGroup, 'push');
app.ResetAllButton.ButtonPushedFcn = createCallbackFcn(app, @ResetAllButtonPushed, true);
app.ResetAllButton.Tooltip = {'Reset segmentation mask (including erased pixels in input image).'};
app.ResetAllButton.Position = [9 5 100 23];
app.ResetAllButton.Text = 'Reset All';
% Create CurrentImageDropDown
app.CurrentImageDropDown = uidropdown(app.MainTab);
app.CurrentImageDropDown.Items = {};
app.CurrentImageDropDown.ValueChangedFcn = createCallbackFcn(app, @CurrentImageDropDownValueChanged, true);
app.CurrentImageDropDown.Position = [194 151 210 22];
app.CurrentImageDropDown.Value = {};
% Create PrevButton
app.PrevButton = uibutton(app.MainTab, 'push');
app.PrevButton.ButtonPushedFcn = createCallbackFcn(app, @PrevButtonPushed, true);
app.PrevButton.Tooltip = {'Go to previous image in image list. If segmentation data exists, it is loaded and plotted.'};
app.PrevButton.Position = [135 150 50 23];
app.PrevButton.Text = 'Prev.';
% Create NextButton
app.NextButton = uibutton(app.MainTab, 'push');
app.NextButton.ButtonPushedFcn = createCallbackFcn(app, @NextButtonPushed, true);
app.NextButton.Tooltip = {'Go to next image in image list. If segmentation data exists, it is loaded and plotted.'};
app.NextButton.Position = [414 150 50 23];
app.NextButton.Text = 'Next';
% Create SegmentationPanel
app.SegmentationPanel = uipanel(app.MainTab);
app.SegmentationPanel.Title = 'Segmentation:';
app.SegmentationPanel.Position = [134 4 178 136];
% Create PlantCheckBox
app.PlantCheckBox = uicheckbox(app.SegmentationPanel);
app.PlantCheckBox.Enable = 'off';
app.PlantCheckBox.Text = 'Plant';
app.PlantCheckBox.Position = [8 89 50 22];
% Create ShootCheckBox
app.ShootCheckBox = uicheckbox(app.SegmentationPanel);
app.ShootCheckBox.Text = 'Shoot';
app.ShootCheckBox.Position = [8 64 54 22];
% Create RootCheckBox
app.RootCheckBox = uicheckbox(app.SegmentationPanel);
app.RootCheckBox.Text = 'Root';
app.RootCheckBox.Position = [8 39 48 22];
% Create IslandsSpinnerLabel
app.IslandsSpinnerLabel = uilabel(app.SegmentationPanel);
app.IslandsSpinnerLabel.HorizontalAlignment = 'right';
app.IslandsSpinnerLabel.Position = [63 90 47 22];
app.IslandsSpinnerLabel.Text = 'Islands:';
% Create IslandsSpinnerPlant
app.IslandsSpinnerPlant = uispinner(app.SegmentationPanel);
app.IslandsSpinnerPlant.Step = 10;
app.IslandsSpinnerPlant.Limits = [0 Inf];
app.IslandsSpinnerPlant.RoundFractionalValues = 'on';
app.IslandsSpinnerPlant.Tooltip = {'After thresholding, connected components with fewer pixels than this value are removed from the output mask.'};
app.IslandsSpinnerPlant.Position = [114 89 59 22];
app.IslandsSpinnerPlant.Value = 200;
% Create IslandsSpinner_2Label
app.IslandsSpinner_2Label = uilabel(app.SegmentationPanel);
app.IslandsSpinner_2Label.HorizontalAlignment = 'right';
app.IslandsSpinner_2Label.Position = [64 64 47 22];
app.IslandsSpinner_2Label.Text = 'Islands:';
% Create IslandsSpinnerShoot
app.IslandsSpinnerShoot = uispinner(app.SegmentationPanel);
app.IslandsSpinnerShoot.Step = 10;
app.IslandsSpinnerShoot.Limits = [0 Inf];
app.IslandsSpinnerShoot.RoundFractionalValues = 'on';
app.IslandsSpinnerShoot.Tooltip = {'After thresholding, connected components with fewer pixels than this value are removed from the output mask.'};
app.IslandsSpinnerShoot.Position = [114 64 59 22];
app.IslandsSpinnerShoot.Value = 150;
% Create IslandsSpinner_3Label
app.IslandsSpinner_3Label = uilabel(app.SegmentationPanel);
app.IslandsSpinner_3Label.HorizontalAlignment = 'right';
app.IslandsSpinner_3Label.Position = [64 39 47 22];
app.IslandsSpinner_3Label.Text = 'Islands:';
% Create IslandsSpinnerRoot
app.IslandsSpinnerRoot = uispinner(app.SegmentationPanel);
app.IslandsSpinnerRoot.Step = 10;
app.IslandsSpinnerRoot.Limits = [0 Inf];
app.IslandsSpinnerRoot.RoundFractionalValues = 'on';
app.IslandsSpinnerRoot.Tooltip = {'After thresholding, connected components with fewer pixels than this value are removed from the output mask.'};
app.IslandsSpinnerRoot.Position = [114 39 59 22];
app.IslandsSpinnerRoot.Value = 150;
% Create ShowMaskButton
app.ShowMaskButton = uibutton(app.SegmentationPanel, 'state');
app.ShowMaskButton.ValueChangedFcn = createCallbackFcn(app, @ShowMaskButtonValueChanged, true);
app.ShowMaskButton.Tooltip = {'Toggle visibility of segmentation mask in plot.'};
app.ShowMaskButton.Text = 'Show Mask';
app.ShowMaskButton.Position = [52 5 73 23];