forked from justinblaber/ncorr_2D_matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncorr.m
3780 lines (3399 loc) · 213 KB
/
ncorr.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 ncorr < handle
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
%
% Programmed by: Justin Blaber
% Principle Investigator: Antonia Antoniou
%
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
%
% Start Ncorr with:
%
% handles_ncorr = ncorr;
%
% NOTE: Images/masks can be set using the GUI menu or by using
% "handles_ncorr.set_ref(data)", "handles_ncorr.set_cur(data)",
% "handles_ncorr.set_roi_ref(data)", or "handles_ncorr.set_roi_cur(data)".
% These will manually set the reference image, current image(s), or
% region of interest if these have been calculated or
% modified using MATLAB and exist in the main workspace. Sometimes the
% reference and current images can be obtained using an average of a
% series of images. The region of interest can also be calculated using
% various thresholding and edge detecting algorithms. Displacement and
% strain data can be obtained through handles_ncorr.data_dic in case the
% user is interested in doing further calculations on the deformation
% data. In addition, if the menu freezes for some reason due to an error,
% it can be unfrozen with "handles_ncorr.refresh()". Lastly, calling
% "delete(handles_ncorr)" will force close Ncorr.
%
%
% MAIN CONSIDERATIONS:
%
% 1) All program data and appdata in the ncorr object are SET by callbacks directly.
% 2) "Downstream" program data (i.e. dependent data) and appdata are
% CLEARED with the "clear_downstream()" function before setting the
% new data.
% 3) UI objects are modified by the update* functions through the wrapper
% function "util_wrapcallbackall" and not within callback directly.
%
% The point of doing this was to make each callback as clear and
% uncluttered as possible. The clearing of "downstream" data and updating
% of UI controls are mainly for upkeep.
%
%
% MAIN FLOW OF CALLBACK:
%
% 1) Update UI controls. Generally, if a menu option updates information,
% it will freeze the top menu to prevent users from calling other
% options while the first one is running.
% 2) Check if data will be overwritten with: "overwrite = clear_downstream('callback')"
% This returns a logical value which is true if the user chooses
% to proceed and also clears the downstream data.
% 3) Perform calculations
% 4) If calculations succeed, store the data directly in the callback
% 5) Update UI controls. This unfreezes the top menu and updates it,
% as well as sets any images (such as the reference/current image)
% if they were loaded.
%
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
%
% NCORR_GUI* FUNCTIONS:
%
% These functions contain GUIs which are called by the callbacks within
% this "ncorr" object. These are, unlike "ncorr," functions that
% utilize "uiwait" and nested functions instead of an object. Thus, when
% the function returns, all workspace data within those functions get
% cleared. Some of these ncorr_gui* functions have an input called
% "params_init" which contains initialization data if the function has
% been called before. Most of these functions use a modal window because
% they must be set before proceeding. These figure are generally not
% resizeable, and their size is checked with "ncorr_util_formatwindow" to
% ensure the top right edges of the window are within the screen.
%
% The initialization of the ncorr_gui_functions generally proceeds as:
%
% 1) Initialize outputs
% 2) Create GUI handles
% 3) Run the constructor function
%
% The callbacks have the general flow:
%
% 1) Possibly freeze menu if it exists
% 2) Get data with "getappdata()"
% 3) Perform calculations
% 4) Set data with "setappdata()"
% 5) Possibly unfreeze menu if it exists and update UI controls
%
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
%
% NCORR_ALG* FUNCTIONS:
%
% These functions contain source-code for algorithms used with the ncorr
% object callbacks and ncorr_gui* functions. Most of these algorithms are
% mex files written in C++ and must be compiled with "compile_func_cpp_mex()"
% or mex before using ncorr.
%
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
%
% NCORR_UTIL* FUNCTIONS:
%
% These functions contain certain utilities used by ncorr, such as
% checking whether the window is within the screen, formatting a colobar,
% etc.
%
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
properties (SetAccess = private)
% GUI handles:
handles_gui;
% DIC data:
reference;
current;
data_dic;
% Installation data:
support_openmp;
total_cores;
end
% Constructor
methods(Access = public)
function obj = ncorr
% Set default UI control font size here before UI opens. Larger
% font sizes can make the GUI too large. This seems to be an
% error in earlier versions of Matlab
set(0,'defaultuicontrolfontsize',7);
% Initialize GUI and get GUI handles
obj.handles_gui = init_gui(obj);
% Run c-tor
feval(ncorr_util_wrapcallbacktrycatch(@obj.constructor,obj.handles_gui.figure));
end
% Constructor ----------------------------------------------------%
function constructor(obj)
% Check MATLAB version and toolbox compatibility -------------%
if (obj.check_matlabcompat() ~= out.success)
% Force close
obj.callback_close_function([],[],true);
return;
end
% Check MEX installation and load/set ncorr_installinfo.txt --%
if (obj.check_mexinstall() ~= out.success)
% Force close
obj.callback_close_function([],[],true);
return;
end
% Set path ---------------------------------------------------%
% Path to ncorr install directory must be set or else program
% can freeze when loading images from a different directory -
% not sure why this happens.
% Note that if ncorr.m is not in the current directory, then
% path has already been set.
listing = dir;
if (isempty(strfind(lower(path),lower(pwd))) && any(strcmp('ncorr.m',{listing.name}))) %#ok<STREMP>
% Ask user if its okay to add path
contbutton = questdlg('Path has not been set. Path must be set before running program. Press yes to add path.','Continue Operation','Yes','No','Yes');
if (strcmp(contbutton,'Yes'))
% Add Path
path(path,pwd);
else
% Force close, ncorr may not to work if path is not set
obj.callback_close_function([],[],true);
return;
end
end
% Initialize opengl ------------------------------------------%
% In earlier versions of Matlab this will fix inverted plots
% Plotting tools are also run based on opengl
if (ispc)
data_opengl = opengl('data');
if (~data_opengl.Software)
% Set opengl to software
opengl('software');
end
end
% Start timer to fetch name of the handle that points to ncorr.
% Use a timer because, to my knowledge, there isn't a callback
% option if the handle pointing to ncorr gets cleared.
timer_ncorr = timer('executionMode', 'fixedRate', ...
'Period', 1/5, ...
'TimerFcn', ncorr_util_wrapcallbacktrycatch(@(hObject,eventdata) callback_update_title(obj,hObject,eventdata),obj.handles_gui.figure));
start(timer_ncorr);
% Program Data -----------------------------------------------%
% Reference image info
obj.reference = struct('imginfo',{},'roi',{});
% Current image(s) info
obj.current = struct('imginfo',{},'roi',{});
% DIC data
obj.data_dic = struct('displacements',struct('plot_u_dic',{}, ... % U plot after DIC (can be regular or eulerian)
'plot_v_dic',{}, ... % V plot after DIC (can be regular or eulerian)
'plot_corrcoef_dic',{}, ... % Correlaton coef plot after DIC (can be regular or eulerian)
'roi_dic',{}, ... % ROI after DIC
'plot_u_ref_formatted',{}, ... % formatted U plot WRT reference image used for displaying displacements
'plot_v_ref_formatted',{}, ... % formatted V plot WRT reference image used for displaying displacements
'roi_ref_formatted',{}, ... % ROI after formatting used for plotting displacements
'plot_u_cur_formatted',{}, ... % formatted U plot WRT current image used for displaying displacements
'plot_v_cur_formatted',{}, ... % formatted V plot WRT current image used for displaying displacements
'roi_cur_formatted',{}), ... % ROI after formatting used for plotting displacements
'dispinfo',struct('type',{}, ... % Type of DIC: either regular or backward
'radius',{}, ... % Radius for DIC
'spacing',{}, ... % Spacing for DIC
'cutoff_diffnorm',{}, ... % Cutoff for norm of the difference vector
'cutoff_iteration',{}, ... % Cutoff for the number of iterations
'total_threads',{}, ... % Number of threads for computation
'stepanalysis',struct('enabled',{},'type',{},'auto',{},'step',{}), ... % Indicates whether or not to implement step analysis for high strain
'subsettrunc',{}, ... % Indicates whether or not to implement subset truncation for DIC analysis
'imgcorr',struct('idx_ref',{},'idx_cur',{}), ... % Image correspondences
'pixtounits',{}, ... % Ratio of "units" to pixels. Assumes pixels are square
'units',{}, ... % String to display units
'cutoff_corrcoef',{}, ... % Correlation coefficient cutoff for each formatted displacement plot
'lenscoef',{}), ... % Radial lens distortion coefficient
'strains',struct('plot_exx_ref_formatted',{}, ... % Exx Green-Lagragian strain plot
'plot_exy_ref_formatted',{}, ... % Exy Green-Lagragian strain plot
'plot_eyy_ref_formatted',{}, ... % Exy Green-Lagragian strain plot
'roi_ref_formatted',{}, ... % ROI used for plotting strains
'plot_exx_cur_formatted',{}, ... % Exx Eulerian-Almansi strain plot
'plot_exy_cur_formatted',{}, ... % Exy Eulerian-Almansi strain plot
'plot_eyy_cur_formatted',{}, ... % Exy Eulerian-Almansi strain plot
'roi_cur_formatted',{}), ... % ROI used for plotting strains
'straininfo',struct('radius',{}, ... % Strain radius used for calculating strains
'subsettrunc',{})); % Indicates whether or not to implement subset truncation for strain analysis
% Appdata - setting these to '[]' technically does nothing, but
% it is included here to show what appdata are/will be present in the
% figure
% handles_plot are the handles for the data plots:
setappdata(obj.handles_gui.figure,'handles_plot',[]);
% num_cur is the current image which is currently being displayed:
setappdata(obj.handles_gui.figure,'num_cur',[]);
% timer_ncorr is the timer for this instantiation of ncorr;
% it is used for fetching the name of the handle points to
% ncorr in the current workspace
setappdata(obj.handles_gui.figure,'timer',timer_ncorr);
% Set visible
set(obj.handles_gui.figure,'Visible','on');
end
% Destructor -----------------------------------------------------%
% This ensures data is deleted if delete is called directly on the
% ncorr handle.
function delete(obj)
if (ishandle(obj.handles_gui.figure))
% Setting 3rd argument to true results in a force close.
obj.callback_close_function([],[],true);
end
end
% Public methods -------------------------------------------------%
% These are used for setting the reference image, current image, or
% ROI manually.
function set_ref(obj,ref_prelim)
% Form wrapped function
handle_set_ref = obj.util_wrapcallbackall(ncorr_util_wrapcallbacktrycatch(@(ref_prelim)set_ref_source(obj,ref_prelim),obj.handles_gui.figure));
% Call wrapped function
handle_set_ref(ref_prelim);
end
function set_cur(obj,cur_prelim)
% Form wrapped function
handle_set_cur = obj.util_wrapcallbackall(ncorr_util_wrapcallbacktrycatch(@(cur_prelim)set_cur_source(obj,cur_prelim),obj.handles_gui.figure));
% Call wrapped function
handle_set_cur(cur_prelim);
end
function set_roi_ref(obj,mask_prelim)
% Form wrapped function
handle_set_roi_ref = obj.util_wrapcallbackall(ncorr_util_wrapcallbacktrycatch(@(mask_prelim)set_roi_ref_source(obj,mask_prelim),obj.handles_gui.figure));
% Call wrapped function
handle_set_roi_ref(mask_prelim);
end
function set_roi_cur(obj,mask_prelim)
% Form wrapped function
handle_set_roi_cur = obj.util_wrapcallbackall(ncorr_util_wrapcallbacktrycatch(@(mask_prelim)set_roi_cur_source(obj,mask_prelim),obj.handles_gui.figure));
% Call wrapped function
handle_set_roi_cur(mask_prelim);
end
function refresh(obj)
% Form wrapped function
handle_refresh = obj.util_wrapcallbackall(ncorr_util_wrapcallbacktrycatch(@()refresh_source(obj),obj.handles_gui.figure));
% Call wrapped function
handle_refresh();
end
end
methods(Access = private)
% ----------------------------------------------------------------%
% Local Utility Function(s) --------------------------------------%
% ----------------------------------------------------------------%
function handle_wrapcallback = util_wrapcallbackall(obj,handle_callback)
% This function wraps the callback with the update* UI functions. It
% also checks to see if an error was generated, and if it was, it
% will rethrow it and recomplete the finishing update* UI functions
% to ensure the menu unfreezes. Note that the error will not be
% rethrown if the figure is closed.
%
% Inputs ---------------------------------------------------------%
% handle_callback - function handle;
%
% Outputs --------------------------------------------------------%
% handle_wrapcallback - function handle;
handle_wrapcallback = @updatestatecallback;
function updatestatecallback(varargin)
try
% Beginning of callback - freeze menu;
obj.freeze_menu();
% Run callback
handle_callback(varargin{:});
% End of callback;
% Update axes
obj.update_axes('set');
% Update UI menu
obj.update_topmenu();
% Update state text
obj.update_dicstatetext();
% Unfreeze top layer of menu
obj.unfreeze_menu();
catch err
% If figure still exists, update UI and then rethrow
% error
if (isvalid(obj) && ishandle(obj.handles_gui.figure))
% Error is generated in callback
obj.update_axes('set');
% Update UI menu
obj.update_topmenu();
% Update state text
obj.update_dicstatetext();
% Unfreeze top layer of menu
obj.unfreeze_menu();
% Rethrow error
rethrow(err);
end
end
end
end
function name = util_get_name(obj)
% This function will obtain the name of the variable in the main
% workspace that points to this ncorr object
% Initialize output
name = '';
% Get all variables in base workspace
basevars = evalin('base','whos');
% Grab variables of class ncorr
ncorrvars = basevars(strcmp({basevars.class},class(obj)));
% Cycle through variables and find which one is equivalent to
% this object
for i = 1:length(ncorrvars)
if (eq(evalin('base',ncorrvars(i).name),obj))
% Its possible there are more than one handle that
% point to this object, if that's the case just return
% the first one
name = ncorrvars(i).name;
break;
end
end
end
%-----------------------------------------------------------------%
% Handle Functions Source ----------------------------------------%
%-----------------------------------------------------------------%
function set_ref_source(obj,ref_prelim)
% This function allows the manual uploading of a reference image;
% only a single image is permitted.
% Check to make sure input is of the correct form
if (ncorr_util_properimgfmt(ref_prelim,'Reference image') == out.success)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_ref');
if (overwrite)
% Set data
obj.reference(1).imginfo = ncorr_class_img;
obj.reference(1).imginfo.set_img('load',struct('img',ref_prelim,'name','reference','path',''));
obj.reference(1).roi = ncorr_class_roi.empty;
end
end
end
function set_cur_source(obj,cur_prelim)
% This function allows the manual uploading of a current image -
% if a single image is uploaded it must be a double array. If multiple
% images are uploaded, they must be in a cell array.
% Check to make sure input is a cell
if (~iscell(cur_prelim))
% Wrap cur_prelim in a cell
cur_prelim = {cur_prelim};
end
% Make sure each image has the correct format
cursameformat = true;
for i = 0:length(cur_prelim)-1
if (ncorr_util_properimgfmt(cur_prelim{i+1},'Current image') ~= out.success)
cursameformat = false;
break;
end
end
if (cursameformat)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_cur');
if (overwrite)
% It's possible to run out of memory here, so put this
% in a try-catch block
try
% Set data
for i = 0:length(cur_prelim)-1
obj.current(i+1).imginfo = ncorr_class_img;
obj.current(i+1).imginfo.set_img('load',struct('img',cur_prelim{i+1},'name',['current_' num2str(i+1)],'path',''));
obj.current(i+1).roi = ncorr_class_roi.empty;
end
setappdata(obj.handles_gui.figure,'num_cur',length(cur_prelim)-1);
catch %#ok<CTCH>
h_error = errordlg('Loading current images failed, most likely because Ncorr ran out of memory.','Error','modal');
uiwait(h_error);
% Clear all current images because exception could have
% been thrown midway through setting current images
obj.current(:) = [];
setappdata(obj.handles_gui.figure,'num_cur',[]);
end
end
else
h_error = errordlg('Loading current images failed because the image data format was incorrect.','Error','modal');
uiwait(h_error);
end
end
function set_roi_ref_source(obj,mask_prelim)
% This function allows the manual uploading of a region of
% interest.
% Check to make sure reference image has been loaded first -
% for menu callbacks this isn't necessary since these options
% will only become callable if "upstream" data has been loaded
if (~isempty(obj.reference))
% Make sure ROI is the same size as the reference image and
% is of class logical.
if (isequal(size(mask_prelim),[obj.reference.imginfo.height obj.reference.imginfo.width]) && islogical(mask_prelim))
% Form roi_prelim
roi_prelim = ncorr_class_roi;
roi_prelim.set_roi('load',struct('mask',mask_prelim,'cutoff',2000));
% Make sure ROI is not empty
if (roi_prelim.get_fullregions > 0)
% At this point, roi_prelim fits the critera for a ROI. Show ROI
% overlayed and ask user if ROI is appropriate:
outstate = ncorr_gui_loadroi(obj.reference.imginfo, ...
roi_prelim, ...
get(obj.handles_gui.figure,'OuterPosition'));
if (outstate == out.success)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_roi');
if (overwrite)
% At this point, ROI is acceptable, has been
% approved by the user, and will now be set.
% Package data:
dispinfo_template.type = 'regular';
dispinfo_template.radius = [];
dispinfo_template.spacing = [];
dispinfo_template.cutoff_diffnorm = [];
dispinfo_template.cutoff_iteration = [];
dispinfo_template.total_threads = [];
dispinfo_template.stepanalysis = struct('enabled',{},'type',{},'auto',{},'step',{});
dispinfo_template.subsettrunc = [];
dispinfo_template.imgcorr = struct('idx_ref',{},'idx_cur',{});
dispinfo_template.pixtounits = [];
dispinfo_template.units = '';
dispinfo_template.cutoff_corrcoef = [];
dispinfo_template.lenscoef = [];
% Store the data:
obj.data_dic.dispinfo(1) = dispinfo_template;
obj.reference(1).roi = roi_prelim;
end
end
else
h_error = errordlg('ROI must contain a large contiguous region.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('Input must be of class logical and the same size as the reference image','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('Reference image has not been loaded yet','Error','modal');
uiwait(h_error);
end
end
function set_roi_cur_source(obj,mask_prelim)
% This function allows the manual uploading of a region of
% interest. This ROI corresponds to the LAST current image.
% Check to make sure current image(s) have been loaded first -
% for menu callbacks this isn't necessary since these options
% will only become callable if "upstream" data has been loaded
if (~isempty(obj.current))
% Make sure ROI is the same size as the last current image
if (isequal(size(mask_prelim),[obj.current(end).imginfo.height obj.current(end).imginfo.width]) && islogical(mask_prelim))
% Form roi_prelim
roi_prelim = ncorr_class_roi;
roi_prelim.set_roi('load',struct('mask',mask_prelim,'cutoff',2000));
% Make sure ROI is not empty
if (roi_prelim.get_fullregions > 0)
% At this point, roi_prelim fits the critera for a ROI. Show ROI
% overlayed and ask user if ROI is appropriate:
outstate = ncorr_gui_loadroi(obj.current(end).imginfo, ...
roi_prelim, ...
get(obj.handles_gui.figure,'OuterPosition'));
if (outstate == out.success)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_roi');
if (overwrite)
% At this point, ROI is acceptable, has been
% approved by the user, and will now be set.
% Package data:
dispinfo_template.type = 'backward';
dispinfo_template.radius = [];
dispinfo_template.spacing = [];
dispinfo_template.cutoff_diffnorm = [];
dispinfo_template.cutoff_iteration = [];
dispinfo_template.total_threads = [];
dispinfo_template.stepanalysis = struct('enabled',{},'type',{},'auto',{},'step',{});
dispinfo_template.subsettrunc = [];
dispinfo_template.imgcorr = struct('idx_ref',{},'idx_cur',{});
dispinfo_template.pixtounits = [];
dispinfo_template.units = '';
dispinfo_template.cutoff_corrcoef = [];
dispinfo_template.lenscoef = [];
% Store the data:
obj.data_dic.dispinfo(1) = dispinfo_template;
obj.current(end).roi = roi_prelim;
end
end
else
h_error = errordlg('ROI must contain a large contiguous region.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('Input must be of class logical and the same size as the last current image','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('Current image(s) have not been loaded yet.','Error','modal');
uiwait(h_error);
end
end
function refresh_source(obj) %#ok<MANU>
% Call this function if there's an error and the menu needs to be
% unfrozen.
end
%-----------------------------------------------------------------%
% Menu Callbacks -------------------------------------------------%
%-----------------------------------------------------------------%
function callback_topmenu_loadref(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for loading a reference image from the
% GUI.
% false means lazy loading isnt used
[ref_prelim,outstate] = ncorr_util_loadimgs(false);
if (outstate == out.success && length(ref_prelim) == 1)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_ref');
if (overwrite)
% Set Image
obj.reference(1).imginfo = ref_prelim;
obj.reference(1).roi = ncorr_class_roi.empty;
end
elseif (outstate == out.success && length(ref_prelim) > 1)
h_error = errordlg('Please select only one reference image.','Error','modal');
uiwait(h_error);
end
end
function callback_topmenu_loadcur(obj,hObject,eventdata,lazyparam) %#ok<INUSL>
% This is the callback for loading current image(s) from the GUI.
[cur_prelim,outstate] = ncorr_util_loadimgs(lazyparam);
if (outstate == out.success)
overwrite = obj.clear_downstream('set_cur');
if (overwrite)
% Set Image
for i = 0:length(cur_prelim)-1
obj.current(i+1).imginfo = cur_prelim(i+1);
obj.current(i+1).roi = ncorr_class_roi.empty;
end
% Display last current image
setappdata(obj.handles_gui.figure,'num_cur',length(cur_prelim)-1);
end
end
end
function callback_topmenu_loaddata(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for loading data from a previous analysis.
% Get data filename
[filename,pathname] = uigetfile({'*.mat'},'Select the previous DIC data (must be .dat)');
if (~isequal(filename,0) && ~isequal(pathname,0))
try
% Load data
struct_load = load(fullfile(pathname,filename));
loadsuccess = true;
catch %#ok<CTCH>
% Probably out of memory
loadsuccess = false;
end
if (loadsuccess)
if (isfield(struct_load,'reference_save') && isfield(struct_load,'current_save') && isfield(struct_load,'data_dic_save'))
% The data has the correct fields
if (isa(struct_load.reference_save,'struct') && ...
isa(struct_load.current_save,'struct') && ...
isa(struct_load.data_dic_save,'struct'))
% Make sure data_dic has the correct
% fields. Easiest way to do this is to assign
% the saved data to a structure with the
% correct fields, an error will result if the
% fields are wrong
data_dic_prelim = obj.data_dic; % This will copy fields - only used temporarily
try
data_dic_prelim(1) = struct_load.data_dic_save; %#ok<NASGU>
loaddatasuccess = true;
catch %#ok<CTCH>
% Some fields are not correct
loaddatasuccess = false;
end
if (loaddatasuccess)
% See if data will be overwritten
overwrite = obj.clear_downstream('all');
if (overwrite)
% Put in try block - its possible to run
% out of memory here.
try
% Do reference image first
[ref_prelim,outstate_ref] = ncorr_util_loadsavedimg(struct_load.reference_save);
% Do current images next
cur_prelim = ncorr_class_img.empty;
for i = 0:length(struct_load.current_save)-1
[cur_buffer,outstate_cur] = ncorr_util_loadsavedimg(struct_load.current_save(i+1));
if (outstate_cur == out.success)
cur_prelim(i+1) = cur_buffer;
else
break;
end
end
if (outstate_ref == out.success && outstate_cur == out.success)
% Store data:
% Set reference:
obj.reference(1).imginfo = ref_prelim;
obj.reference(1).roi = struct_load.reference_save.roi;
% Set current:
for i = 0:length(struct_load.current_save)-1
obj.current(i+1).imginfo = cur_prelim(i+1);
obj.current(i+1).roi = struct_load.current_save(i+1).roi;
end
setappdata(obj.handles_gui.figure,'num_cur',length(struct_load.current_save)-1);
% Set dic data:
obj.data_dic(1) = struct_load.data_dic_save;
else
% Images could not be found
% Form error message
msg_error{1} = 'Images could not be located. Make sure they are in the current directory or were not moved from the locations listed here:';
msg_error{2} = fullfile(struct_load.reference_save.path,struct_load.reference_save.name);
max_display = 10;
for i = 0:min(length(struct_load.current_save),max_display)-1
msg_error{end+1} = fullfile(struct_load.current_save(i+1).path,struct_load.current_save(i+1).name); %#ok<AGROW>
end
if (length(struct_load.current_save) > max_display)
msg_error{end+1} = '...';
end
h_error = errordlg(msg_error,'Error','modal');
uiwait(h_error);
end
catch %#ok<CTCH>
h_error = errordlg('Loading failed, most likely because Ncorr ran out of memory.','Error','modal');
uiwait(h_error);
% Clear everything - its possible
% exception was caught while
% loading data midway.
obj.reference(:) = [];
obj.current(:) = [];
setappdata(obj.handles_gui.figure,'num_cur',[]);
fields_data_dic = fieldnames(obj.data_dic);
for i = 0:length(fields_data_dic)-1
obj.data_dic.(fields_data_dic{i+1})(:) = [];
end
end
end
else
h_error = errordlg('The data is not valid, please load data saved specifically from ncorr.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('The data is not valid, please load data saved specifically from ncorr.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('The data is not valid, please load data saved specifically from ncorr.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('Loading failed, most likely because Ncorr ran out of memory.','Error','modal');
uiwait(h_error);
end
end
end
function callback_topmenu_savedata(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for saving data.
% Get save data filename
[filename,pathname] = uiputfile({'*.mat'},'Save DIC data (must be .dat)');
if (~isequal(filename,0) && ~isequal(pathname,0))
% Check if data will be overwritten in directory
overwrite = true;
if (exist(fullfile(pathname,filename),'file'))
contbutton = questdlg('File already exists. Do you want to overwrite?','Continue Operation','Yes','No','Yes');
if (strcmp(contbutton,'No'))
overwrite = false;
end
end
if (overwrite)
% Must deal with image data, ROI data, and data_dic.
% 1) Images: Save the file names if they are 'file' or 'lazy'
% If they were set through the workspace (i.e. 'load') then save the
% image as double precision.
% 2) ROIs: For the ROIs, save each directly.
% 3) Data: Save data_dic directly.
reference_save = struct('type',{},'gs',{},'name',{},'path',{},'roi',{});
current_save = struct('type',{},'gs',{},'name',{},'path',{},'roi',{});
% Images ---------------------------------------------%
% Reference image:
reference_save(1).type = obj.reference.imginfo.type;
reference_save.gs = [];
if (strcmp(obj.reference.imginfo.type,'load'))
% Must save gs data directly
reference_save.gs = obj.reference.imginfo.get_gs();
end
reference_save.name = obj.reference.imginfo.name;
reference_save.path = obj.reference.imginfo.path;
reference_save.roi = obj.reference.roi; %#ok<STRNU>
% Current image(s):
for i = 0:length(obj.current)-1
current_save(i+1).type = obj.current(i+1).imginfo.type;
current_save(i+1).gs = [];
if (strcmp(obj.current(i+1).imginfo.type,'load'))
% Must save gs data directly
current_save(i+1).gs = obj.current(i+1).imginfo.get_gs();
end
current_save(i+1).name = obj.current(i+1).imginfo.name;
current_save(i+1).path = obj.current(i+1).imginfo.path;
current_save(i+1).roi = obj.current(i+1).roi;
end
% Data -----------------------------------------------%
data_dic_save = obj.data_dic; %#ok<NASGU>
% Save -----------------------------------------------%
try
% The v7.3 flag helps save larger files
save(fullfile(pathname,filename),'reference_save','current_save','data_dic_save','-v7.3');
catch %#ok<CTCH>
% If saving fails, its generally because the files
% are too large.
h_error = errordlg('Saving failed, probably because the amount of data being saved is too large.','Error','modal');
uiwait(h_error);
end
end
end
end
function callback_topmenu_clear(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for the clearing all the data.
obj.clear_downstream('all');
end
function callback_topmenu_sethandle(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for setting the handle to ncorr in the case
% that it gets cleared from the workspace
% Get Name
name = util_get_name(obj);
if (isempty(name))
% Prompt user for what handle he/she wants to use
[handle_name,outstate] = gui_sethandle(get(obj.handles_gui.figure,'OuterPosition'));
if (outstate == out.success)
% Assign handles to base workspace
assignin('base',handle_name,obj);
end
else
% Handle already exists
e = msgbox(['Handle already exists and is called: ' name],'WindowStyle','modal');
uiwait(e);
end
end
function callback_topmenu_reinstall(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for reinstalling the mex files.
% Check if ncorr.m is in current directory
listing = dir;
if (any(strcmp('ncorr.m',{listing.name})))
% Ask user if its okay to reinstall
contbutton = questdlg('Are you sure you want to reinstall? All data will be lost if proceeding.','Continue Operation','Yes','No','Yes');
if (strcmp(contbutton,'Yes'))
% Clear ncorr_installinfo.txt file - this is the standard
% way to reinstall
filename = 'ncorr_installinfo.txt';
fid = fopen(filename,'w'); % This will clear file
if (fid ~= -1)
fclose(fid); % Close file
% Force close
obj.callback_close_function([],[],true);
% Reopen ncorr - this will cause reinstallation since
% ncorr_installinfo.txt has been cleared
handles_ncorr = ncorr;
% Get name
name = util_get_name(obj);
if (~isempty(name))
% If there is a name then assign the name to the
% ncorr object.
assignin('base',name,handles_ncorr);
end
else
% Error
h_error = errordlg('For some reason ncorr was not able to clear "ncorr_installinfo.txt" file, reinstall cannot take place.','Error','modal');
uiwait(h_error);
end
end
else
% Error
h_error = errordlg('Please navigate to folder containing ncorr.m first before reinstalling.','Error','modal');
uiwait(h_error);
end
end
function callback_exit_callback(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for exiting the program.
% 3rd argument is set to false, which queries user about
% closing if DIC data has been computed.
obj.callback_close_function([],[],false);
end
function callback_topmenu_set_roi_ref(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for loading the region of interest from the
% GUI
% Get ROIs ---------------------------------------------------%
% Check if ROI has been set before - must be from the regular
% analysis, as backward DIC analysis will also set a reference ROI
params_init = ncorr_class_roi.empty;
if (~isempty(obj.reference) && ~isempty(obj.reference.roi) && ...
~isempty(obj.data_dic.dispinfo) && strcmp(obj.data_dic.dispinfo.type,'regular'))
params_init(1) = obj.reference.roi;
end
[roi_prelim,outstate] = ncorr_gui_setrois(obj.reference.imginfo, ...
get(obj.handles_gui.figure,'OuterPosition'), ...
params_init);
if (outstate == out.success)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_roi');
if (overwrite)
% Package data:
dispinfo_template.type = 'regular';
dispinfo_template.radius = [];
dispinfo_template.spacing = [];
dispinfo_template.cutoff_diffnorm = [];
dispinfo_template.cutoff_iteration = [];
dispinfo_template.total_threads = [];
dispinfo_template.stepanalysis = struct('enabled',{},'type',{},'auto',{},'step',{});
dispinfo_template.subsettrunc = [];
dispinfo_template.imgcorr = struct('idx_ref',{},'idx_cur',{});
dispinfo_template.pixtounits = [];
dispinfo_template.units = '';
dispinfo_template.cutoff_corrcoef = [];
dispinfo_template.lenscoef = [];
% Store the data:
obj.data_dic.dispinfo(1) = dispinfo_template;
obj.reference(1).roi = roi_prelim;
end
end
end
function callback_topmenu_set_roi_cur(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for loading the region of interest from
% the GUI
% Get ROIs ---------------------------------------------------%
% Check if ROI has been set before - must be from the backward
% analysis, as backward dic analysis will also set a current ROI
params_init = ncorr_class_roi.empty;
if (~isempty(obj.current) && ~isempty(obj.current(end).roi) && ...
~isempty(obj.data_dic.dispinfo) && strcmp(obj.data_dic.dispinfo.type,'backward'))
params_init(1) = obj.current(end).roi;
end
[roi_prelim,outstate] = ncorr_gui_setrois(obj.current(end).imginfo, ...
get(obj.handles_gui.figure,'OuterPosition'), ...
params_init);
if (outstate == out.success)
% See if data will be overwritten
overwrite = obj.clear_downstream('set_roi');
if (overwrite)
% Package data:
dispinfo_template.type = 'backward';
dispinfo_template.radius = [];
dispinfo_template.spacing = [];
dispinfo_template.cutoff_diffnorm = [];
dispinfo_template.cutoff_iteration = [];
dispinfo_template.total_threads = [];
dispinfo_template.stepanalysis = struct('enabled',{},'type',{},'auto',{},'step',{});
dispinfo_template.subsettrunc = [];
dispinfo_template.imgcorr = struct('idx_ref',{},'idx_cur',{});
dispinfo_template.pixtounits = [];
dispinfo_template.units = '';
dispinfo_template.cutoff_corrcoef = [];
dispinfo_template.lenscoef = [];
% Store the data:
obj.data_dic.dispinfo(1) = dispinfo_template;
obj.current(end).roi = roi_prelim;
end
end
end
function callback_topmenu_setdicparameters(obj,hObject,eventdata) %#ok<INUSD>
% This is the callback for setting DIC parameters
% Check if DIC parameters were set before - if dic parameters
% are being set, the data_dic.dispinfo field is gauranteed
% nonempty, so you don't need to check this first.
params_init = {};
if (~isempty(obj.data_dic.dispinfo.radius) && ...
~isempty(obj.data_dic.dispinfo.spacing) && ...
~isempty(obj.data_dic.dispinfo.cutoff_diffnorm) && ...
~isempty(obj.data_dic.dispinfo.cutoff_iteration) && ...
~isempty(obj.data_dic.dispinfo.total_threads) && ...
~isempty(obj.data_dic.dispinfo.stepanalysis) && ...
~isempty(obj.data_dic.dispinfo.subsettrunc))
params_init = {obj.data_dic.dispinfo.radius, ...
obj.data_dic.dispinfo.spacing, ...
obj.data_dic.dispinfo.cutoff_diffnorm, ...
obj.data_dic.dispinfo.cutoff_iteration, ...
obj.data_dic.dispinfo.total_threads, ...
obj.data_dic.dispinfo.stepanalysis, ...
obj.data_dic.dispinfo.subsettrunc};
end
% Get Params -------------------------------------------------%
if (strcmp(obj.data_dic.dispinfo.type,'regular'))
% Show reference image for regular analysis
[radius_prelim,spacing_prelim,cutoff_diffnorm_prelim,cutoff_iteration_prelim,total_threads_prelim,stepanalysis_prelim,subsettrunc_prelim,outstate] = ncorr_gui_setdicparams(obj.reference.imginfo, ...
obj.reference.roi, ...
obj.support_openmp, ...
obj.total_cores, ...
get(obj.handles_gui.figure,'OuterPosition'), ...
params_init);
else
% Show last current image for backward analysis
[radius_prelim,spacing_prelim,cutoff_diffnorm_prelim,cutoff_iteration_prelim,total_threads_prelim,stepanalysis_prelim,subsettrunc_prelim,outstate] = ncorr_gui_setdicparams(obj.current(end).imginfo, ...
obj.current(end).roi, ...
obj.support_openmp, ...
obj.total_cores, ...