-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_processing.m
1316 lines (1249 loc) · 56.2 KB
/
pre_processing.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
function [settings, selection] = pre_processing(varargin)
%PRE_PROCESSING Summary of this function goes here
% Accepts up to 1 argument
% @param <int> preselected cached path (-1 if no preselection)
%% ADD ALL NECESSARY PATHS (REQUIRED)
addpath(genpath('./modules'))
addpath(genpath('./data'))
addpath(genpath('./export'))
addpath(genpath('./migrations'))
addpath(genpath('./cache'))
addpath(genpath('./utils'))
%% LOAD SETTINGS IF PRESELECTED
if nargin == 1 && varargin{1} > 0
load(['cache/cached_paths_' num2str(varargin{1}) '/last_parameters.mat'], ...
'settings', 'CACHED_PATH_SELECTION')
fprintf('Selected path to flakes: %s\n\n', settings.pathToFlakes);
selection.id = num2str(varargin{1});
selection.path = settings.pathToFlakes;
return;
end
%% INIT SHARED VARS
CACHED_PATHS_TXT = 'cache/cached_paths.txt';
pathToFlakes = '';
datestart = '';
dateend = '';
isCamColor = zeros(1,3);
outputProcessedImgs = 0;
camFOV = nan(1,3);
backgroundThresh = 0;
topDiscard = 0; applyTopDiscardToCams = [];
bottomDiscard = 0; applyBotDiscardToCams = [];
leftDiscard = 0; applyLeftDiscardToCams = [];
rightDiscard = 0; applyRightDiscardToCams = [];
lineFill = 0;
minFlakePerim = 0;
minCropWidth = 0;
maxEdgeTouch = 0;
avgFlakeBrightness = 0;
filterFocus = 0;
focusThreshold = 0;
internalVariability = 0;
flakeBrighten = 0;
siteName = '';
cameraName = '';
rescanOriginal = 0;
skipProcessed = 0;
filesInPath = [];
changedPathToFlakes = 0;
%% MAIN
% Prompt user to select a cached_path if a cached_paths.txt file has been made
if exist(CACHED_PATHS_TXT,'file')
[settings, selection, define] = load_settings();
if ~define
fprintf('Found previous set of processing parameters!\n');
s = input('Would you like to change some parameters before continuing (Y/n): ', 's');
while isempty(s) || length(s) > 1 || (s ~= 'Y' && s ~= 'n')
s = input('Please type ''Y'' or ''n'': ', 's');
end
fprintf('\n');
if s == 'n'
return;
else
settingsToVariables;
redefine = 1; %#ok<*NASGU>
end
else
pathToFlakes = selection.path;
end
else
define = 1;
selection.id = 1; % User is running for the first time, so will be the first cached_path
end
clear s
% Check if user chose to redefine existing parameters, or
% has to define new ones because there are no existing parameters
if exist('redefine','var') && redefine
fprintf(['You chose to redefine some of the parameters from the existing\n' ...
'set that was loaded. You will be prompted to change each of the\n' ...
'parameters. To keep the current value when prompted, just hit [Enter].\n'])
disp('%%% RE-DEFINE PARAMETERS %%%')
define_params()
disp('%%% END RE-DEFINE PARAMETERS %%%')
clear redefine
elseif exist('define','var') && define
disp('%%% DEFINE PARAMETERS %%%')
define_params()
disp('%%% END DEFINE PARAMETERS %%%')
clear define
end
fprintf('\n')
% Show full list of parameters and ask if user is okay with these values and is ready
% to proceed
s = 'n';
while s == 'n'
disp('%%%%%%%%%%%%%%%%%%%%%%% LIST OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%')
fprintf(' pathToFlakes: %s\n', pathToFlakes)
fprintf(' datestart: %s\n', datestr(datestart))
fprintf(' dateend: %s\n', datestr(dateend))
fprintf(' isCamColor: [%i, %i, %i]\n', isCamColor(1), isCamColor(2), isCamColor(3))
fprintf('outputProcessedImgs: %s\n', outputProcessedImgs)
fprintf(' camFOV: [%.2f, %.2f, %.2f]\n', camFOV(1), camFOV(2), camFOV(3))
fprintf(' backgroundThresh: %.0f\n', backgroundThresh)
fprintf(' topDiscard: %i\n', topDiscard)
fprintf(['applyTopDiscardToCams: [' num2str(applyTopDiscardToCams) ']\n'])
fprintf(' bottomDiscard: %i\n', bottomDiscard)
fprintf(['applyBotDiscardToCams: [' num2str(applyBotDiscardToCams) ']\n'])
fprintf(' leftDiscard: %i\n', leftDiscard)
fprintf(['applyLeftDiscardToCams: [' num2str(applyLeftDiscardToCams) ']\n'])
fprintf(' rightDiscard: %i\n', rightDiscard)
fprintf(['applyRightDiscardToCams: [' num2str(applyRightDiscardToCams) ']\n'])
fprintf(' lineFill: %i\n', lineFill)
fprintf(' minFlakePerim: %.1f\n', minFlakePerim)
fprintf(' minCropWidth: %.1f\n', minCropWidth)
fprintf(' maxEdgeTouch: %.0f\n', maxEdgeTouch)
fprintf(' avgFlakeBrightness: %.1f\n', avgFlakeBrightness)
fprintf(' filterFocus: %i\n', filterFocus)
fprintf(' focusThreshold: %.1f\n', focusThreshold)
fprintf('internalVariability: %.1f\n', internalVariability)
fprintf(' flakeBrighten: %s\n', flakeBrighten)
fprintf(' siteName: %s\n', siteName)
fprintf(' cameraName: %s\n', cameraName)
fprintf(' rescanOriginal: %i\n', rescanOriginal)
fprintf(' skipProcessed: %i\n', skipProcessed)
fprintf(['%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' ...
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n'])
s = input('Would you like to save these parameters and continue [Y], or go back and change some [n]? (Y/n): ', 's');
while isempty(s) || (length(s) > 1 || (s ~= 'Y' && s ~= 'n'))
disp('Please type ''Y'' or ''n''.')
s = input('Save parameters and continue [Y], or go back and change some [n]? (Y/n): ', 's');
end
if s == 'n'
disp('%%% RE-DEFINE PARAMETERS %%%')
define_params()
disp('%%% END RE-DEFINE PARAMETERS %%%')
end
end
clear s
% At this point, user has finalized the general processing parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% MASC-SPECIFIC FORMATTING
% The most effective way of getting the image's timestamp, flake ID, and/or
% camera ID from the image filename is using a regular expression.
% This is the current format of images from the MASC:
% yyyy.mm.dd_HH.MM.SS[.FFF]_flake_(image id)[.(particle id)]_cam_(cam id).png
% -> where the text in brackets indicates something that may or may
% not be in the filename, and
% -> where the text in parentheses indicates a reference to a value
% will be there
% According to this format, below is the pattern that will pull out the
% string between [prefix_] and [_suffix].
mascImgRegPattern = [ ...
'\d{4}\.{1}\d{2}\.{1}\d{2}' ... % yyyy.mm.dd
'\_{1}' ... % underscore
'\d{2}\.{1}\d{2}\.{1}\d{2}(\.{1}\d{1,6})?' ... % HH.MM.SS[.FFF]
'\_flake_{1}\d+(\.{1}\d+)?' ... % flake_(image id)[.(particle id)]
'\_cam_{1}[0-2]' ... % cam_(cam id)
'\.[pngjpeg]{3,4}'; % .png, .jpg, or .jpeg
];
% This can be changed should the format of image filenames from the MASC
% ever change.
%% SAVE FILES IN PATH (IF APPLICABLE)
% Now, we'll check if filesInPath returned from define_params is not empty.
% If it isn't, then we need to add the pathToFlakes to the cached_paths,
% along with the list of files given by filesInPath
if ~isempty(filesInPath)
fprintf('Caching PNG files for the specified pathToFlakes...');
% Get number of paths already cached
if exist(CACHED_PATHS_TXT, 'file')
numPaths = length(strfind(fileread(CACHED_PATHS_TXT), 'Path = "')) + 1;
else
numPaths = 1; % Actually 0, but we set to 1 so the index starts at 1
end
% Add new path to cached_paths
fwid = fopen(CACHED_PATHS_TXT,'a');
fprintf(fwid, 'Path = "%s"\n', pathToFlakes);
% Set which txt file the path will point to
fprintf(fwid, 'Cache Dir = "%s"\n\n', ['cache/cached_paths_' num2str(numPaths)]);
fclose(fwid);
% Create directory if necessary
if ~exist(['cache/cached_paths_' num2str(numPaths)], 'dir')
mkdir(['cache/cached_paths_' num2str(numPaths)])
end
% Open a new cache file
numCacheFiles = 1;
fwid = fopen(['cache/cached_paths_' num2str(numPaths) '/f1.txt'], 'w');
for i = 1:length(filesInPath)
if ~mod(i,500)
% Start a new cache file every 500 PNGs...
% Assuming O(100) cropped flakes for each PNG, that would
% result in O(50,000) records in each txt file.
fclose(fwid);
numCacheFiles = numCacheFiles + 1;
fwid = fopen(['cache/cached_paths_' num2str(numPaths) '/f' num2str(numCacheFiles) '.txt'], 'w');
end
fprintf(fwid, '%s\t1\t0\t0\t0\t0\n', filesInPath(i).name);
end
fclose(fwid);
fprintf('done.\n');
if exist('changedPathToFlakes', 'var') && changedPathToFlakes
selection.id = numPaths;
end
end
fclose('all');
% Put all parameters into a struct
settings = paramsAsStruct;
% Save current parameters to file for most recent parameters used
disp('Caching these parameters...')
cacheSettings;
function cacheSettings
CACHED_PATH_SELECTION = selection.id;
clearvars -except settings CACHED_PATH_SELECTION
save(['cache/cached_paths_' num2str(CACHED_PATH_SELECTION) '/last_parameters.mat'])
selection.id = CACHED_PATH_SELECTION;
selection.path = settings.pathToFlakes;
end
function [settings] = paramsAsStruct
% Save all parameters to a settings struct for convenient access and
% passing of the parameters around to other functions.
settings.pathToFlakes = pathToFlakes;
settings.datestart = datestart;
settings.dateend = dateend;
settings.mascImgRegPattern = mascImgRegPattern;
settings.isCamColor = isCamColor;
settings.outputProcessedImgs = outputProcessedImgs;
settings.camFOV = camFOV;
settings.backgroundThresh = backgroundThresh;
settings.topDiscard = topDiscard;
settings.applyTopDiscardToCams = applyTopDiscardToCams;
settings.bottomDiscard = bottomDiscard;
settings.applyBotDiscardToCams = applyBotDiscardToCams;
settings.leftDiscard = leftDiscard;
settings.applyLeftDiscardToCams = applyLeftDiscardToCams;
settings.rightDiscard = rightDiscard;
settings.applyRightDiscardToCams = applyRightDiscardToCams;
settings.lineFill = lineFill;
settings.minFlakePerim = minFlakePerim;
settings.minCropWidth = minCropWidth;
settings.maxEdgeTouch = maxEdgeTouch;
settings.avgFlakeBrightness = avgFlakeBrightness;
settings.filterFocus = filterFocus;
settings.focusThreshold = focusThreshold;
settings.internalVariability = internalVariability;
settings.flakeBrighten = flakeBrighten;
settings.siteName = siteName;
settings.cameraName = cameraName;
settings.rescanOriginal = rescanOriginal;
settings.skipProcessed = skipProcessed;
end
% When last parameters are loaded at the beginning of this function,
% need to break them out of settings struct so we can ask user if they
% want to keep the old value or set a new one.
function settingsToVariables
if isfield(settings, 'pathToFlakes')
pathToFlakes = settings.pathToFlakes;
end
if isfield(settings, 'datestart')
datestart = settings.datestart;
end
if isfield(settings, 'dateend')
dateend = settings.dateend;
end
if isfield(settings, 'mascImgRegPattern')
mascImgRegPattern = settings.mascImgRegPattern;
end
if isfield(settings, 'isCamColor')
isCamColor = settings.isCamColor;
end
if isfield(settings, 'outputProcessedImgs')
outputProcessedImgs = settings.outputProcessedImgs;
end
if isfield(settings, 'camFOV')
camFOV = settings.camFOV;
end
if isfield(settings, 'backgroundThresh')
backgroundThresh = settings.backgroundThresh;
end
if isfield(settings, 'topDiscard')
topDiscard = settings.topDiscard;
end
if isfield(settings, 'applyTopDiscardToCams')
applyTopDiscardToCams = settings.applyTopDiscardToCams;
end
if isfield(settings, 'bottomDiscard')
bottomDiscard = settings.bottomDiscard;
end
if isfield(settings, 'applyBotDiscardToCams')
applyBotDiscardToCams = settings.applyBotDiscardToCams;
end
if isfield(settings, 'leftDiscard')
leftDiscard = settings.leftDiscard;
end
if isfield(settings, 'applyLeftDiscardToCams')
applyLeftDiscardToCams = settings.applyLeftDiscardToCams;
end
if isfield(settings, 'rightDiscard')
rightDiscard = settings.rightDiscard;
end
if isfield(settings, 'applyRightDiscardToCams')
applyRightDiscardToCams = settings.applyRightDiscardToCams;
end
if isfield(settings, 'lineFill')
lineFill = settings.lineFill;
end
if isfield(settings, 'minFlakePerim')
minFlakePerim = settings.minFlakePerim;
end
if isfield(settings, 'minCropWidth')
minCropWidth = settings.minCropWidth;
end
if isfield(settings, 'maxEdgeTouch')
maxEdgeTouch = settings.maxEdgeTouch;
end
if isfield(settings, 'avgFlakeBrightness')
avgFlakeBrightness = settings.avgFlakeBrightness;
end
if isfield(settings, 'filterFocus')
filterFocus = settings.filterFocus;
end
if isfield(settings, 'focusThreshold')
focusThreshold = settings.focusThreshold;
end
if isfield(settings, 'internalVariability')
internalVariability = settings.internalVariability;
end
if isfield(settings, 'flakeBrighten')
flakeBrighten = settings.flakeBrighten;
end
if isfield(settings, 'siteName')
siteName = settings.siteName;
end
if isfield(settings, 'cameraName')
cameraName = settings.cameraName;
end
if isfield(settings, 'rescanOriginal')
rescanOriginal = settings.rescanOriginal;
end
if isfield(settings, 'skipProcessed')
skipProcessed = settings.skipProcessed;
end
end
function define_params
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% REQUIRED PARAMS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('%% REQUIRED PARAMS %%')
%% Var pathToFlakes
% Prompts user to set pathToFlakes or keep it the same as before. If it
% is set or changed, will check to see if new path is in
% cached_paths.txt file. If not, then searches for PNG files in the new
% path. If no PNGs are found, reprompts for a path with PNG files. If
% PNGs are found, filesInPath are saved and returned for caching later.
fprintf(['<strong>pathToFlakes</strong>: Where snowflake images are located. The images \n' ...
'\tdo not need to be in any set directory hierarchy. All PNG images\n' ...
'\tin the directory will be found. Path MUST be absolute.\n' ...
'\t<strong>Note</strong>: When this is set, the path will be searched for PNG files. If none are found\n' ...
'\toutside of the processed image directories (CROP_CAM, UNCROP_CAM, TRIPLETS, REJECTS),\n' ...
'\tthen you will be asked to provide a different path.\n'])
changingPathToFlakes = 0;
if exist('pathToFlakes', 'var')
s = input(sprintf('var pathToFlakes = %s;\nChange to: ', strrep(pathToFlakes,'\','\\')), 's');
changingPathToFlakes = 1;
else
% Require user to set a path
s = input('Set pathToFlakes to: ', 's');
end
while ~isempty(s) && ((s(1) ~= '/' && s(2) ~= ':') || (s(end) ~= '/' && s(end) ~= '\'))
disp('Invalid path, you must specify an absolute path and include a trailing slash. Please try again.')
s = input('Set pathToFlakes to: ', 's');
end
% Initialize filesInPath variable. If the user provides a path that
% doesn't exist in cached_paths, we'll need to write out the img file
% names to cached_paths. Instead of having to traverse to get these
% files again (since we make sure that path user provides contains
% PNGs), we'll hold onto this array of file structs.
if ~isempty(s)
% First, check if user provided path is already in cached_paths
if exist(CACHED_PATHS_TXT, 'file')
if strfind(fileread(CACHED_PATHS_TXT), ['"' s '"'])
% Path has been used before, accept this input
files = 1;
disp('Path found in cache! Okay to use again. Continuing...')
else
files = 0;
end
else
files = 0;
end
if ~files
imgFilter = @(d) isempty(regexp(d.name,'UNCROP_CAM')) && isempty(regexp(d.name,'TRIPLETS')) && isempty(regexp(d.name,'REJECTS'));
disp('Searching for PNGs...')
files = rdir([s '**' filesep '*.png'], imgFilter, s);
end
while ~isempty(s) && isempty(files)
% While user provides a path with NO PNG files, continue to prompt
if exist('pathToFlakes', 'var')
% NOTE: This still allows users to keep the existing pathToFlakes, if it is already set
disp('Invalid path, the path you specified contained no PNG files. Please try again (or keep the existing pathToFlakes).')
s = input(sprintf('var pathToFlakes = %s;\nChange to: ', strrep(pathToFlakes,'\','\\')), 's');
else
disp('Invalid path, the path you specified contained no PNG files. Please try again.')
% Require user to set a path
s = input('Set pathToFlakes to: ', 's');
end
while isempty(s) || ((s(1) ~= '/' && s(2) ~= ':') || (s(end) ~= '/' && s(end) ~= '\'))
disp('Invalid path, you must specify an absolute path and include a trailing slash. Please try again.')
s = input('Set pathToFlakes to: ', 's');
end
if ~isempty(s)
if exist(CACHED_PATHS_TXT, 'file')
if ~strfind(fileread(CACHED_PATHS_TXT), ['"' s '"'])
% Path has been used before, accept this input
files = 1;
disp('Path found in cache! Okay to use again. Continuing...')
else
files = 0;
end
else
files = 0;
end
if ~files
imgFilter = @(d) isempty(regexp(d.name,'CROP_CAM')) && isempty(regexp(d.name,'UNCROP_CAM')) && isempty(regexp(d.name,'TRIPLETS')) && isempty(regexp(d.name,'REJECTS')); %#ok<*RGXP1>
disp('Searching for PNGs...')
files = rdir([s '**' filesep '*.png'], imgFilter, s);
end
end
end
if ~isempty(s)
if isstruct(files)
disp('PNGs found!')
filesInPath = files;
% Signal to pre_processing that the user changed the cached path, indicating a new cached path should be made.
if changingPathToFlakes
changedPathToFlakes = 1;
end
end
pathToFlakes = s;
else
disp('Using old pathToFlakes with verified PNG files.')
end
else
disp('Using old pathToFlakes with verified PNG files.')
end
pathToFlakes %#ok<NOPRT> % Show user the result of defining pathToFlakes
%% Var datestart
fprintf(['<strong>datestart</strong>: Start date of snowflakes to process. When setting this,\n' ...
'\tinput the date as a string of the form "yyyymmdd_HHMM".\n'])
if exist('datestart', 'var')
s = input(sprintf('var datestart = %s; Change to: ', datestr(datestart, 'yyyymmdd_HHMM')), 's');
while ~isempty(s) && ~validate_datestr(s, 0)
disp('Must enter a valid string ("yyyymmdd_HHMM") for datestart.')
s = input(sprintf('var datestart = %s; Change to: ', datestr(datestart, 'yyyymmdd_HHMM')), 's');
end
else
% Require user to set datestart
s = input('Set datestart to: ', 's');
while isempty(s) || ~validate_datestr(s, 0)
disp('Must enter a valid string ("yyyymmdd_HHMM") for datestart.')
s = input('Set datestart to: ', 's');
end
end
if ~isempty(s)
datestart = datenum(s, 'yyyymmdd_HHMM');
end
datestr(datestart) % Show user the result of defining datestart
%% Var dateend
fprintf(['<strong>dateend</strong>: End date of snowflakes to process. When setting this,\n' ...
'\tinput the date as a string of the form "yyyymmdd_HHMM".\n'])
if exist('dateend', 'var')
s = input(sprintf('var dateend = %s; Change to: ', datestr(dateend, 'yyyymmdd_HHMM')), 's');
while ~isempty(s) && ~validate_datestr(s, datestart)
disp('Must enter a valid string ("yyyymmdd_HHMM") for dateend that is after datestart.')
s = input(sprintf('var dateend = %s; Change to: ', datestr(dateend, 'yyyymmdd_HHMM')), 's');
end
else
% Require user to set dateend
s = input('Set dateend to: ', 's');
while isempty(s) || ~validate_datestr(s, datestart)
disp('Must enter a valid string ("yyyymmdd_HHMM") for dateend that is after datestart.')
s = input('Set dateend to: ', 's');
end
end
if ~isempty(s)
dateend = datenum(s, 'yyyymmdd_HHMM');
end
datestr(dateend) % Show user the result of defining dateend
%% Var isCamColor
fprintf(['<strong>isCamColor</strong>: Specifies whether each camera in the MASC takes its pictures\n' ...
'\tin color. Binary 1x3 array, where the first element corresponds to color for camera 0, second\n' ...
'\telement corresponds to color camera 1, and third element corresponds to color for camera 2.\n' ...
'\tValue of 1 signifies camera <strong>DOES</strong> take pictures in color.\n'])
if exist('isCamColor', 'var')
% Camera 0
s = input(sprintf('var isCamColor(1) = %i; Change to (0 or 1): ', isCamColor(1)), 's');
while ~isempty(s) && (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('If changing this value, you must specify 0 or 1')
s = input(sprintf('var isCamColor(1) = %i; Change to (0 or 1): ', isCamColor(1)), 's');
end
if ~isempty(s)
isCamColor(1) = str2num(s); %#ok<*ST2NM>
end
% Camera 1
s = input(sprintf('var isCamColor(2) = %i; Change to (0 or 1): ', isCamColor(2)), 's');
while ~isempty(s) && (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('If changing this value, you must specify 0 or 1')
s = input(sprintf('var isCamColor(2) = %i; Change to (0 or 1): ', isCamColor(2)), 's');
end
if ~isempty(s)
isCamColor(2) = str2num(s);
end
% Camera 2
s = input(sprintf('var isCamColor(3) = %i; Change to (0 or 1): ', isCamColor(3)), 's');
while ~isempty(s) && (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('If changing this value, you must specify 0 or 1')
s = input(sprintf('var isCamColor(3) = %i; Change to (0 or 1): ', isCamColor(3)), 's');
end
if ~isempty(s)
isCamColor(3) = str2num(s);
end
else
% User must specify color (y/n = 1/0) for each camera
% Camera 0
s = input('Is camera 0 a color camera (0 = No, 1 = Yes): ', 's');
while isempty(s) || (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('Invalid input, must specify 0 or 1.')
s = input('Is camera 0 a color camera (0 = No, 1 = Yes): ', 's');
end
isCamColor(1) = str2num(s);
% Camera 1
s = input('Is camera 1 a color camera (0 = No, 1 = Yes): ', 's');
while isempty(s) || (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('Invalid input, must specify 0 or 1.')
s = input('Is camera 1 a color camera (0 = No, 1 = Yes): ', 's');
end
isCamColor(2) = str2num(s);
% Camera 2
s = input('Is camera 2 a color camera (0 = No, 1 = Yes): ', 's');
while isempty(s) || (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('Invalid input, must specify 0 or 1.')
s = input('Is camera 2 a color camera (0 = No, 1 = Yes): ', 's');
end
isCamColor(3) = str2num(s);
end
isCamColor %#ok<NOPRT> % Display results of defining isCamColor
%% Var outputProcessedImgs
fprintf(['<strong>outputProcessedImgs</strong>: Flag that determines if processed images should be\n' ...
'\twritten to processed output folders. If 0, images will not be written. Any additional img\n' ...
'\tprocessing functions may have their own flag for this, but if not, they will inherit this flag.\n'...
'\t<strong>Note</strong>: This is an important flag because if outputting images, processing\n' ...
'\t\ttime will be significantly affected. At best, each imwrite takes approx. 0.07 seconds.\n' ...
'\t\tThis means that an extra <strong>hour</strong> will be added to processing time for each\n' ...
'\t\t<strong>50,000</strong> flake images. The default for this value is 0 (i.e. NO IMG OUTPUT).\n'])
if exist('outputProcessedImgs', 'var')
s = input(sprintf('var outputProcessedImgs = %s; Change to (0 or 1): ', outputProcessedImgs), 's');
while ~isempty(s) && (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('To change this flag, must set to 0 or 1.')
s = input(sprintf('var outputProcessedImgs = %s; Change to (0 or 1): ', outputProcessedImgs), 's');
end
if ~isempty(s)
outputProcessedImgs = s;
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set outputProcessedImgs to (0 or 1): ', 's');
while ~isempty(s) && (length(s) > 1 || (s ~= '0' && s ~= '1'))
disp('To set this flag, must input 0 or 1.')
s = input('Set outputProcessedImgs to (0 or 1): ', 's');
end
if ~isempty(s)
outputProcessedImgs = s;
else
outputProcessedImgs = '0';
end
end
outputProcessedImgs %#ok<NOPRT> % Show user the result of defining outputProcessedImgs
%% Var camFOV
fprintf(['<strong>camFOV</strong>: The calculated <strong>pixels/mm</strong> of each image.\n' ...
'\tThis can be computed by dividing the pixel width of images that the\n' ...
'\tMASC camera takes by the field-of-view of the camera (in mm).\n' ...
'\tIt can also be obtained through observation by dropping an object\n' ...
'\tof known size through the MASC to then see the number of pixels\n' ...
'\tthe object spans.\n' ...
'\tIMPORANT: These values must be accurate in order for size measurement\n' ...
'\t\tmodules to return accurate results.\n'])
if exist('camFOV', 'var')
% Camera 0
s = input(sprintf('var camFOV(1) = %i; Change to: ', camFOV(1)), 's');
while ~isempty(s) && isnan(str2double(s))
disp('If changing this value, you must specify a decimal number')
s = input(sprintf('var camFOV(1) = %i; Change to: ', camFOV(1)), 's');
end
if ~isempty(s)
camFOV(1) = str2double(s);
end
% Camera 1
s = input(sprintf('var camFOV(2) = %i; Change to: ', camFOV(2)), 's');
while ~isempty(s) && isnan(str2double(s))
disp('If changing this value, you must specify a decimal number')
s = input(sprintf('var camFOV(2) = %i; Change to: ', camFOV(2)), 's');
end
if ~isempty(s)
camFOV(2) = str2double(s);
end
% Camera 2
s = input(sprintf('var camFOV(3) = %i; Change to: ', camFOV(3)), 's');
while ~isempty(s) && isnan(str2double(s))
disp('If changing this value, you must specify a decimal number')
s = input(sprintf('var camFOV(3) = %i; Change to: ', camFOV(3)), 's');
end
if ~isempty(s)
camFOV(3) = str2double(s);
end
else
% User must specify FOV decimal value for each camera
% Camera 0
s = input('Define FOV for camera 0: ', 's');
while isempty(s) || isnan(str2double(s))
disp('Invalid input, must define each FOV as a decimal number.')
s = input('Define FOV for camera 0: ', 's');
end
camFOV(1) = str2double(s);
% Camera 1
s = input('Define FOV for camera 1: ', 's');
while isempty(s) || isnan(str2double(s))
disp('Invalid input, must define each FOV as a decimal number.')
s = input('Define FOV for camera 1: ', 's');
end
camFOV(2) = str2double(s);
% Camera 2
s = input('Define FOV for camera 2: ', 's');
while isempty(s) || isnan(str2double(s))
disp('Invalid input, must define each FOV as a decimal number.')
s = input('Define FOV for camera 2: ', 's');
end
camFOV(3) = str2double(s);
end
camFOV %#ok<NOPRT> % Display results of defining camFOV
%% Var background threshold (all pixels outside flake should fall below
% this threshold brightness
fprintf(['<strong>backgroundThresh</strong>: The brightness threshold for which all pixels outside\n' ...
'\tthe flake should fall below. Value is on a scale from 0 to 255 in brightness,\n' ...
'\twith 0 being completely dark and 255 completely white.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 20\n')
if exist('backgroundThresh', 'var')
s = input(sprintf('var backgroundThresh = %.0f; Change to (0-255): ', backgroundThresh), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0 || str2double(s) > 255)
disp('To change this value, must set to a value between 0 and 255, inclusive.')
s = input(sprintf('var backgroundThresh = %.0f; Change to (0-255): ', backgroundThresh), 's');
end
if ~isempty(s)
backgroundThresh = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set backgroundThresh to (0-255): ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0 || str2double(s) > 255)
disp('To set this value, must input a value between 0 and 255.')
s = input('Set backgroundThresh to (0-255): ', 's');
end
if ~isempty(s)
backgroundThresh = str2double(s);
else
backgroundThresh = 20;
end
end
backgroundThresh %#ok<NOPRT> % Display results of defining backgroundThresh
%% Var topDiscard, bottomDiscard, leftDiscard, rightDiscard, apply*DiscardsToCams
fprintf(['<strong>XXXDiscard</strong>: The pixels to crop on each side of the image frame.\n' ...
'\tYou will be able to specify a value for discarding pixel rows on the top\n' ...
'\tand bottom of the img frame, as well as pixel columns on the left and right\n' ...
'\tsides of the img frame. E.g. Specifying 10 for topDiscard would mask out\n' ...
'\t10 rows of pixels at the top of the image.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 0\n')
fprintf(['\n<strong>applyXXXDiscardToCams</strong>: Array of camera IDs to apply discards to.\n' ...
'\tThis variable stipulates which cameras of the MASC you want to apply\n' ...
'\tthe top/bottom/left/right discard to. The possible options are 0, 1, and 2.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default (if discard not set)')
fprintf(' = []\n\t')
cprintf('_text', 'Default (if discard > 0)')
fprintf(' = [0, 1, 2]\n')
if exist('topDiscard', 'var')
s = input(sprintf('var topDiscard = %i; Change to: ', topDiscard), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var topDiscard = %i; Change to: ', topDiscard), 's');
end
if ~isempty(s)
topDiscard = str2num(s);
end
else
s = input('Set topDiscard to: ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set topDiscard to: ', 's');
end
if ~isempty(s)
topDiscard = str2num(s);
else
topDiscard = 0;
end
end
if exist('applyTopDiscardToCams', 'var')
cur = applyTopDiscardToCams;
else, cur = -1;
end
applyTopDiscardToCams = applyDiscardToCams('Top', cur);
if exist('bottomDiscard', 'var')
s = input(sprintf('var bottomDiscard = %i; Change to: ', bottomDiscard), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var bottomDiscard = %i; Change to: ', bottomDiscard), 's');
end
if ~isempty(s)
bottomDiscard = str2num(s);
end
else
s = input('Set bottomDiscard to: ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set bottomDiscard to: ', 's');
end
if ~isempty(s)
bottomDiscard = str2num(s);
else
bottomDiscard = 0;
end
end
if exist('applyBotDiscardToCams', 'var')
cur = applyBotDiscardToCams;
else, cur = -1;
end
applyBotDiscardToCams = applyDiscardToCams('Bot', cur);
if exist('leftDiscard', 'var')
s = input(sprintf('var leftDiscard = %i; Change to: ', leftDiscard), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var leftDiscard = %i; Change to: ', leftDiscard), 's');
end
if ~isempty(s)
leftDiscard = str2num(s);
end
else
s = input('Set leftDiscard to: ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set leftDiscard to: ', 's');
end
if ~isempty(s)
leftDiscard = str2num(s);
else
leftDiscard = 0;
end
end
if exist('applyLeftDiscardToCams', 'var')
cur = applyLeftDiscardToCams;
else, cur = -1;
end
applyLeftDiscardToCams = applyDiscardToCams('Left', cur);
if exist('rightDiscard', 'var')
s = input(sprintf('var rightDiscard = %i; Change to: ', rightDiscard), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var rightDiscard = %i; Change to: ', rightDiscard), 's');
end
if ~isempty(s)
rightDiscard = str2num(s);
end
else
s = input('Set rightDiscard to: ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set rightDiscard to: ', 's');
end
if ~isempty(s)
rightDiscard = str2num(s);
else
rightDiscard = 0;
end
end
if exist('applyRightDiscardToCams', 'var')
cur = applyRightDiscardToCams;
else, cur = -1;
end
applyRightDiscardToCams = applyDiscardToCams('Right', cur);
% Display results of settings discard variables
topDiscard %#ok<NOPRT>
applyTopDiscardToCams %#ok<NOPRT>
bottomDiscard %#ok<NOPRT>
applyBotDiscardToCams %#ok<NOPRT>
leftDiscard %#ok<NOPRT>
applyLeftDiscardToCams %#ok<NOPRT>
rightDiscard %#ok<NOPRT>
applyRightDiscardToCams %#ok<NOPRT>
%% Var lineFill
fprintf(['<strong>lineFill</strong>: In order to assess the flake area, internal\n' ...
'\tcomplexities are blurred with the lineFill parameter. This avoids small,\n' ...
'\tlocal discontinuities to make a single flake. This is essentially a guess\n' ...
'\tfor the minimum flake size (in microns).\n' ...
'\tIMPORANT: Used in Module processing!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 200 (microns) \n')
if exist('lineFill', 'var')
s = input(sprintf('var lineFill = %.0f; Change to: ', lineFill), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var lineFill = %.0f; Change to: ', lineFill), 's');
end
if ~isempty(s)
lineFill = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set lineFill to:', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set lineFill to: ', 's');
end
if ~isempty(s)
lineFill = str2double(s);
else
lineFill = 200;
end
end
lineFill %#ok<NOPRT> % Display results of setting lineFill
%% Var minFlakePerim
fprintf(['<strong>minFlakePerim</strong>: The minimum acceptable perimeter of a flake in pixels.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 250\n')
if exist('minFlakePerim', 'var')
s = input(sprintf('var minFlakePerim = %.0f; Change to: ', minFlakePerim), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var minFlakePerim = %.0f; Change to: ', minFlakePerim), 's');
end
if ~isempty(s)
minFlakePerim = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set minFlakePerim to:', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set minFlakePerim to: ', 's');
end
if ~isempty(s)
minFlakePerim = str2double(s);
else
minFlakePerim = 250;
end
end
minFlakePerim %#ok<NOPRT> % Display results of setting minFlakePerim
%% Var minCropWidth
fprintf(['<strong>minCropWidth</strong>: The minimum acceptable dimensions of the box\n' ...
'\tthat contains a cropped flake.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 40\n')
if exist('minCropWidth', 'var')
s = input(sprintf('var minCropWidth = %.0f; Change to: ', minCropWidth), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var minCropWidth = %.0f; Change to: ', minCropWidth), 's');
end
if ~isempty(s)
minCropWidth = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set minCropWidth to:', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set minCropWidth to: ', 's');
end
if ~isempty(s)
minCropWidth = str2double(s);
else
minCropWidth = 40;
end
end
minCropWidth %#ok<NOPRT> % Display results of setting minFlakePerim
%% Var maxEdgeTouch
fprintf(['<strong>maxEdgeTouch</strong>: The maximum acceptable length for a flake to touch the\n' ...
'\timage frame edge. This is used to filter out flakes where a relatively large\n' ...
'\tportion of the flake is outside of the image frame.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 100\n')
if exist('maxEdgeTouch', 'var')
s = input(sprintf('var maxEdgeTouch = %.0f; Change to: ', maxEdgeTouch), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input(sprintf('var maxEdgeTouch = %.0f; Change to: ', maxEdgeTouch), 's');
end
if ~isempty(s)
maxEdgeTouch = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set maxEdgeTouch to:', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0)
disp('Must input a non-negative value.')
s = input('Set maxEdgeTouch to: ', 's');
end
if ~isempty(s)
maxEdgeTouch = str2double(s);
else
maxEdgeTouch = 100;
end
end
maxEdgeTouch %#ok<NOPRT> % Display results of setting maxEdgeTouch
%% Var avgFlakeBrightness
fprintf(['<strong>avgFlakeBrightness</strong>: The minimum acceptable average pixel brightness.\n' ...
'\tDarker flakes tend to be out of focus, thus having a slight threshold on\n' ...
'\tavg pixel brightness of the flakes should eliminate some unuseful flakes.\n' ...
'\tIMPORANT: Used in SCAN & CROP!\n\t'])
cprintf('_text', 'Default ')
fprintf('= 10 (Ranges from 0-255, like backgroundThresh)\n')
if exist('avgFlakeBrightness', 'var')
s = input(sprintf('var avgFlakeBrightness = %.0f; Change to (0-255): ', avgFlakeBrightness), 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0 || str2double(s) > 255)
disp('To change this value, must set to a value between 0 and 255, inclusive.')
s = input(sprintf('var avgFlakeBrightness = %.0f; Change to (0-255): ', avgFlakeBrightness), 's');
end
if ~isempty(s)
avgFlakeBrightness = str2double(s);
end
else
% Don't require user to set the format (if they don't, default to format 1)
s = input('Set avgFlakeBrightness to (0-255): ', 's');
while ~isempty(s) && (isnan(str2double(s)) || str2double(s) < 0 || str2double(s) > 255)
disp('To set this value, must input a value between 0 and 255.')
s = input('Set avgFlakeBrightness to (0-255): ', 's');
end
if ~isempty(s)
avgFlakeBrightness = str2double(s);
else
avgFlakeBrightness = 10;
end
end
avgFlakeBrightness %#ok<NOPRT> % Display results of defining avgFlakeBrightness
%% Var filterFocus
fprintf(['<strong>filterFocus</strong>: Whether to filter objects in SCAN & CROP by their\n' ...
'\tin-focus measure. This uses the same number that''s calculated by the Focus module.\n' ...
'\tIf enabled, SCAN & CROP will accept objects with at least the value specified by\n' ...
'\tfocusThreshold OR all of the other conditions satisfied. The focus is used as a condition\n' ...
'\tto allow objects through SCAN & CROP that otherwise would have been filtered out.\n' ...