-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvisBase.m
2084 lines (1719 loc) · 83.9 KB
/
nvisBase.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 (Abstract) nvisBase < handle
%nvisBase Baseclass for nvis GUIs
% This class serves as a base class for the 'nvis...' classes,
% which are optimized for visualizing multidimensional (complex)
% matrices. nvisBase.m provides the building block for the GUI which are
% placed in the figure by the inheriting classes.
%
% Many of the UI features (windowing, colormap overlay,...) are
% handled by nvis member functions.
%______________________________________________________________________
% Authors: Johannes Fischer
% Yanis Taege
properties (Access = public)
f
Type
end
properties (Access = private)
% DISPLAY PROPERTIES
fftStatus % keeps track of fft-button status
% WINDOWING PROPERTIES
Max % maximum value in both images
Min % minimum value in both images
Std % standard deviation in both images
center % current value mapped to the center of the colormap
width % width of values mapped to the colormap, around center
widthMin % minimalWidth of the colormap, prevents division by 0
centerStep % how much 'center' changes when windowing
widthStep % how much 'width' changes when windowing
nrmFac % normalization factor for windowing process
end
properties (Access = protected)
%% INPUT PROPERTIES
nImages % number of images (1 or 2)
nAxes % number of displayed image Axes (nvis: 1, nvis3: 3)
img % cell array in which the input matrices are stored
nDims % number of image dimensions
ston % cell array for dimensions where a matrix is the only singleton
isComplex % is one of the inputs complex
isFloat % are the inputs float or integer
S % size of the image(s)
p % input parser
standardTitle % name of the figure, default depends on inputnames
varargin % member variable varargin
%% DISPLAYING
% link sliders to image dimensions
% mapSliderToDim(2) = 4 means, that slider 2 controls the slice along the
% 4th dimension.
mapSliderToDim
% link sliders to images
% mapSliderToImage{2} = 1 means, that slider 2 changes values in
% obj.sel{1, :}.
% mapSliderToImage{2} = ':' means, that slider 2 changes the
% selector for all shown images
mapSliderToImage
% showDims(1, :) = [3 4] means, that in axis 1, the first
% dimensions shows the input data along its third dimension, the
% second axis shows the input data along its fourth dimension.
showDims
% if fixedDim ~= 0, this dimension remains the dimension looped by
% the timer, and will never be one of the selected dimensions
fixedDim
% cell array that stores the location information in the input data
% of each currently shown slice.
% obj.sel{2, :} contains the subscripts to obtain the image shown
% in axis 2
sel
% cell array containing the current slice image information
slice
% index of the currently active dimension for slider scrolling
% etc...
activeDim
% index of the currently active ax element
activeAx
% which of the images is currently shown?
layerShown
% timer
t
% index of the slider that is inc- or decremented by the timer
% interrupt
interruptedSlider
% framerate of timer
fps
% stores the current complex representation mode
complexMode
% resize factor for the displayed data
resize
% used to rotate the view on the axes when necessary
azimuthAng
% coordinates of the current point under the cursor (third element
% is the axis, pt is empty when curser not over any axis)
pt
% contains the information, which contrast is currently chosen
contrast
% Foregroud color for text, similar in hue to the colormap of the
% associated image
COLOR_m
% colormap for all images as a cell array, containing Nx3 colormaps
cmap
% overlay mode (1: add, 2: multiply)
overlay
% label for each dimension
dimLabel
% values for each dimension
dimVal
%% GUI ELEMENTS
% array of images displayed in 'ax', the handle to the respective axis can always be obtained via
% get(obj.hImage(...), 'Parent')
hImage
% array of slider handles to navigate through the slices
hSlider
hTextSlider
hEditSlider
% cw-windowing element arrays
hTextC
hTextW
hEditC
hEditW
hBtnHide
hBtnToggle
hBtnCwCopy
hBtnCwHome
hBtnCwSlice
hBtnCwLink
hBtnRotL
hBtnRotR
% select the colormap for each input image
hPopCm
% select the overlay mode
hPopOverlay
% buttons array for complex data display
hBtnCmplx
% FFT button
hBtnFFT
% Roi
hBtnRoi
hTextRoi
hTextSNR
hTextSNRvals
hTextRoiType
hPopRoiType
hBtnDelRois
hBtnSaveRois
hLocAndVals
% colorbars
hAxCb
% animation elements
hBtnRun
hEditF
hTextFPS
% saving images and videos
hBtnSaveImg
hBtnSaveVid
% minimize/maximize control panel
hBtnMinimMaxim
%% GUI ELEMENT PROPERTIES
% number of sliders in the UI
nSlider
inputNames
valNames
% units of the input data
unit
% array with ROIs
rois
% mean in the signal rois
signal
% std-deviation in the noie rois
noise
% do we see the colorbars?
cbShown
% how are the colorbars oriented ('horizontal' or 'vertical')
cbDirection
% max number of letters for variable names in the locVal section
maxLetters
% colormaps available
availableCmaps
cmapStrings
% are both images windowed simultaneously?
linkedWindowing
% keeps the state of the control panel (bool)
maximized
%% other
% path to this file
resPath
end
properties (Constant = true, Hidden = true)
% color scheme of the GUI
COLOR_BG = [0.2 0.2 0.2];
COLOR_B = [0.1 0.1 0.1];
COLOR_F = [0.9 0.9 0.9];
COLOR_roi = [1.0 0.0 0.0;
0.0 0.0 1.0];
contrastList = {'green-magenta', 'PET', 'heat'};
zoomInFac = 1.1;
zoomOutFac = 0.9;
BtnHideKey = {'w' 'e'};
BtnTgglKey = 'q';
overlayStrings = {'add', 'multiply'}
end
%% abstract methods
methods (Abstract)
locVal(obj, axNo)
refreshUI(obj)
incDecActiveDim(obj, incDec)
mouseBtnNormal(obj, pt)
mouseBtnAlt(src, evtData)
recolor(obj)
saveImgBtn(obj)
minimMaximBtn(obj)
mouseBtnDouble(obj)
end
methods
function obj = nvisBase(in, varargin)
% CONSTRUCTOR
obj.varargin = varargin;
% check varargin for a sencond input matrix
obj.inputCheck(in)
% The slider for which dimension is initially active?
obj.activeDim = 1;
% necessary for view orientation, already needed when saving image or video
obj.azimuthAng = 0;
% set the default value for max Number of letters is locVal
% section
obj.maxLetters = 6;
% start control panel in maximized state
obj.maximized = true;
% prepare roi parameters
obj.rois = {[], []};
obj.signal = NaN(1, obj.nImages);
obj.noise = NaN(1, obj.nImages);
% initialize UI colors
obj.COLOR_m = [obj.COLOR_F; obj.COLOR_F];
% set the correct default value for complexMode
if any(obj.isComplex)
obj.complexMode = 1;
else
% if neither input is complex, display the real part of the
% data
obj.complexMode = 3;
end
% find min and max values in the input data
obj.findMinMax()
% prepare the input parser
obj.prepareParser()
% prepare the colormaps
obj.prepareColormaps()
[obj.resPath, ~, ~] = fileparts(mfilename('fullpath'));
obj.resPath = [obj.resPath filesep() 'res' filesep()];
end
function delete(obj)
if ~isempty(obj.f) && isvalid(obj.f)
close(obj.f)
% close also calls the destructor on obj (again)
else
delete(obj)
end
end
function findMinMax(obj)
% Calculates the minimal and maximal value in the upto two
% input matrices. If there are +-Inf values in the data, a
% slower, less memory efficient calculation is performed.
% min and max values are cast as doubles, to have their
% datatype be independet of the input datatype
if verLessThan('matlab', '9.5')
% version is older than 2018b, use slower, but not very
% slow max/min calculation implementation.
obj.Max = [double(obj.cleverMax(obj.img{1})), double(obj.cleverMax(obj.img{2}))];
obj.Min = [double(obj.cleverMin(obj.img{1})), double(obj.cleverMin(obj.img{2}))];
else
obj.Max = [double(max(obj.img{1}, [], 'all', 'omitnan')), double(max(obj.img{2}, [], 'all', 'omitnan'))];
obj.Min = [double(min(obj.img{1}, [], 'all', 'omitnan')), double(min(obj.img{2}, [], 'all', 'omitnan'))];
end
hasInf = obj.Max == Inf;
for ii = find(hasInf)
warning('Inf values present in input %d. For large input matrices this can cause memory overflow and long startup time.', ii)
obj.Max(ii) = double(max(obj.img{ii}(~isinf(obj.img{ii})), [], 'omitnan'));
end
hasInf = obj.Min == -Inf;
for ii = find(hasInf)
warning('Inf values present in input %d. For large input matrices this can cause memory overflow and long startup time.', ii)
obj.Min(ii) = min(obj.img{ii}(~isinf(obj.img{ii})), [], 'omitnan');
end
if obj.nImages == 1
obj.Min(2) = 0;
obj.Max(2) = 1;
end
obj.Max(obj.isComplex) = abs(obj.Max(obj.isComplex));
obj.Min(obj.isComplex) = abs(obj.Min(obj.isComplex));
end
function inputCheck(obj, in)
if ~isempty(obj.varargin) && ( isnumeric(obj.varargin{1}) || islogical(obj.varargin{1}) )
% two input matrices
obj.img{1} = in;
% get second matrix from varargin
obj.img{2} = obj.varargin{1};
% remove second matrix from varargin, should only contain
% NVPs now
obj.varargin(1) = [];
isEmpt = cellfun(@isempty, obj.img);
if ~sum( isEmpt )
% neither is empty
obj.nImages = 2;
obj.isComplex = ~cellfun(@isreal, obj.img);
obj.isFloat = cellfun(@isfloat, obj.img);
obj.layerShown = [1, 1];
obj.checkSize()
elseif sum(isEmpt) == 1
% one input matrix is empty
warning('Input %d is empty! Only non-empty iputs are shown', find(isEmpt))
obj.nImages = 1;
obj.isComplex = ~cellfun(@isreal, obj.img);
obj.isFloat = cellfun(@isfloat, obj.img);
obj.img(isEmpt) = [];
% for nImages=1, the second cell must be explicitly
% empty
obj.img{2} = [];
obj.S = size(obj.img{1});
obj.ston{1} = [];
else
error('Both input matrices are empty')
end
else
% one input matrix
obj.img{1} = in;
obj.img{2} = [];
obj.nImages = 1;
obj.isComplex = ~isreal(obj.img{1});
obj.isFloat = isfloat(obj.img{1});
obj.layerShown = [1, 0];
obj.S = size(obj.img{1});
obj.ston{1} = [];
end
obj.nDims = numel(obj.S);
if any(obj.isComplex .* ~obj.isFloat)
error('Complex integer types are currently not supported!')
end
end
function checkSize(obj)
% get the image sizes
s1 = size(obj.img{1});
s2 = size(obj.img{2});
if numel(s1) ~= numel(s2)
% and image dimensions (trailing singleton dimensions are not
% considered by size)
d1 = ndims(obj.img{1});
d2 = ndims(obj.img{2});
if d1 > d2
s2 = [s2 ones(1, d1-d2)];
else
s1 = [s1 ones(1, d2-d1)];
end
end
if any(s1 ~= s2)
% there are dimensions, in wich both matrices have different
% sizes
% combine the sizes into one array
s = [s1; s2];
% check that for those dimensions, where the size differs,
% one is equal to 1
if any(min(s(:, s1~=s2),[], 1) ~= 1)
% there are dimensions where the size differs and
% neither of the matrices has a size of one along that
% dimension
error('Matrix dimensions must agree.')
else
% find unique singleton dimensions in matrices
obj.ston{1} = find((s1 ~= s2) & s1 == 1);
obj.ston{2} = find((s1 ~= s2) & s2 == 1);
end
obj.S = max(s, [], 1);
else
% both input matrices have exactly the same size
obj.ston{1} = [];
obj.ston{2} = [];
obj.S = size(obj.img{1});
end
end
function prepareParser(obj)
% default figure position and size. is adapted to actual screensize
% and is separated from top/bottom by 10% of up/down screensize
screenS = get(0, 'ScreenSize');
defaultPosition = [ 300, round(0.1*screenS(4)), 800, round(0.8*screenS(4))];
obj.p = inputParser;
% add parameters to the input parser
addParameter(obj.p, 'Position', defaultPosition, @(x) isnumeric(x) && numel(x) == 4);
addParameter(obj.p, 'Overlay', 1, @(x) floor(x)==x && x >= 1); %is integer greater 1
addParameter(obj.p, 'Colormap', gray(256), @(x) iscell(x) | isnumeric(x) | ischar(x));
addParameter(obj.p, 'Contrast', 'green-magenta', @(x) obj.isContrast(x));
addParameter(obj.p, 'ComplexMode', obj.complexMode, @(x) isnumeric(x) && x <= 4);
addParameter(obj.p, 'AspectRatio', 'square', @(x) any(strcmp({'image', 'square'}, x)));
addParameter(obj.p, 'Resize', 1, @isnumeric);
addParameter(obj.p, 'Title', obj.standardTitle, @ischar);
addParameter(obj.p, 'CW', [(obj.Max(1) - obj.Min(1))/2+obj.Min(1), ...
obj.Max(1)-obj.Min(1); ...
(obj.Max(2) - obj.Min(2))/2+obj.Min(2), ...
obj.Max(2)-obj.Min(2)], @isnumeric);
addParameter(obj.p, 'widthMin', 0,@isnumeric);
addParameter(obj.p, 'Unit', {[], []}, @(x) (iscell(x) && numel(x) <= 2) | ischar(x));
addParameter(obj.p, 'DimLabel', strcat(repmat({}, 1, numel(obj.S))), @(x) iscell(x) && numel(x) >= obj.nDims);
addParameter(obj.p, 'DimVal', cellfun(@(x) 1:x, num2cell(obj.S), 'UniformOutput', false), @iscell);
addParameter(obj.p, 'SaveImage', '', @ischar);
addParameter(obj.p, 'SaveVideo', '', @ischar);
addParameter(obj.p, 'fps', 0, @isnumeric);
addParameter(obj.p, 'LoopDimension',3, @(x) isnumeric(x) && x <= obj.nDims && obj.nDims >= 3);
addParameter(obj.p, 'LoopCount', Inf, @isnumeric);
addParameter(obj.p, 'fixedDim', 0, @isnumeric);
addParameter(obj.p, 'InitRot', zeros(1, obj.nAxes), @(x) isnumeric(x));
end
function prepareColors(obj)
% Depending on the amount of input matrices and the provided
% colormaps or contrast information, the colorscheme for the UI
% is set and 'center', 'width', and 'widthMin' are given proper
% initial values.
%
% Called by constructor of inheriting class
%
% set the string values for the colormaps in the popdown menus.
% from the inputParser, get the initial colormaps
if ~contains('Colormap', obj.p.UsingDefaults)
% Colormap was used as NVP, this overrules any input from
% the 'Contrast' NVP
obj.setInitialColormap(obj.p.Results.Colormap)
elseif obj.nImages == 2
% get initial Contrast for the display from InputParser values
obj.setInitialContrast()
end
set(obj.hPopCm(1), 'String', obj.cmapStrings)
if obj.nImages == 2
set(obj.hPopCm(2), 'String', obj.cmapStrings)
end
obj.widthMin = [0 0];
obj.center = zeros(4, 2);
obj.width = zeros(4, 2);
for idh = 1:obj.nImages
obj.center([1 3 4], idh) = double(obj.p.Results.CW(idh, 1));
obj.width([1 3 4], idh) = double(obj.p.Results.CW(idh, 2));
% set phase to range 0 2*pi
obj.center(2, idh) = 0;
obj.width(2, idh) = 2*pi;
set(obj.hEditC(idh), 'String', num2sci(obj.center(1, idh), 'padding', 'right'));
set(obj.hEditW(idh), 'String', num2sci(obj.width(1, idh), 'padding', 'right'));
% apply the initial colormaps to the popdown menus
obj.setCmap(obj.hPopCm(idh))
end
obj.centerStep = double(obj.center);
obj.widthStep = double(obj.width);
% if any of the center/width values is zero, set the step to
% that of the other value
obj.centerStep(obj.centerStep == 0) = obj.widthStep(obj.centerStep == 0);
obj.widthStep(obj.centerStep == 0) = obj.centerStep(obj.centerStep == 0);
end
function prepareGUIElements(obj)
% create figure handle, but hide figure
obj.f = figure('Color', obj.COLOR_BG, ...
'Visible', 'off',...
'AutoResizeChildren', 'off');
% create UI elements for center and width
obj.hTextC = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hTextW = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
for idh = 1:obj.nImages
obj.hEditC(idh) = uicontrol( ...
'Style', 'edit', ...
'BackgroundColor', obj.COLOR_BG, ...
'Enable', 'on', ...
'Callback', @obj.setCW, ...
'Tooltip', 'center value for applied colormap');
obj.hEditW(idh) = uicontrol( ...
'Style', 'edit', ...
'BackgroundColor', obj.COLOR_BG, ...
'Enable', 'on', ...
'Callback', @obj.setCW, ...
'Tooltip', 'width value for applied colormap');
obj.hBtnCwHome(idh) = uicontrol( ...
'Style', 'pushbutton', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.BtnCwHomeCallback}, ...
'String', char(8962), ...
'Tooltip', 'use initial windowing');
obj.hBtnCwSlice(idh) = uicontrol( ...
'Style', 'pushbutton', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.BtnCwSliceCallback}, ...
'String', char(9633), ...
'Tooltip', 'window current slice');
if obj.nImages == 2
obj.hBtnHide(idh) = uicontrol( ...
'Style', 'togglebutton', ...
'BackgroundColor', obj.COLOR_BG, ...
'Callback', {@obj.BtnHideCallback});
num = {'second', 'first'};
arrows = [8592, 8594];
obj.hBtnCwCopy(idh) = uicontrol( ...
'Style', 'pushbutton', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'String', char(arrows(idh)), ...
'Tooltip', ['copy CW values form ' num{idh} ' image'], ...
'Callback', {@obj.BtnCwCopyCallback});
set(obj.hBtnCwHome(idh), 'Tooltip', ['use initial windowing on ' num{idh} ' image']);
set(obj.hBtnCwSlice(idh), 'Tooltip', ['window current slice of ' num{idh} ' image']);
end
end
if obj.nImages == 2
obj.hBtnCwLink = uicontrol( ...
'Style', 'pushbutton', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Tooltip', 'link windowing in both images', ...
'String', sprintf('<html><img src="file:/%sunlinked.png" height="15" width="15"/></html>', obj.resPath), ...
'Callback', {@obj.BtnCwLinkCallback});
obj.hBtnToggle = uicontrol( ...
'Style', 'pushbutton', ...
'String', ['Toggle (' obj.BtnTgglKey ')'], ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.BtnToggleCallback});
end
obj.linkedWindowing = false;
obj.hPopCm(1) = uicontrol( ...
'Style', 'popup', ...
'String', {''}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.changeCmap});
if obj.nImages == 2
obj.hPopOverlay = uicontrol( ...
'Style', 'popup', ...
'String', obj.overlayStrings, ...
'Value', obj.overlay, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.changeOverlay});
obj.hPopCm(2) = uicontrol( ...
'Style', 'popup', ...
'String', {''}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Callback', {@obj.changeCmap});
end
%--
% regions of interest
%--
% uicontrols must be initialized alone, cannot be done without
% loop (i guess...)
for iHdl = 1:2
obj.hBtnRoi(iHdl) = uicontrol( ...
'Style', 'pushbutton', ...
'Callback', {@obj.drawRoi}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
for jHdl = 1:obj.nImages
obj.hTextRoi(iHdl, jHdl) = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
end
end
for iHdl = 1:obj.nImages
obj.hTextSNRvals(iHdl) = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
end
obj.hTextSNR = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hTextRoiType = uicontrol( ...
'Style', 'text', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hPopRoiType = uicontrol( ...
'Style', 'popup', ...
'String', {'Polygon', 'Ellipse', 'Freehand'}, ...
'Value', 1, ...
'Tooltip', 'Select the ROI type', ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnDelRois = uicontrol( ...
'Style', 'pushbutton', ...
'Callback', {@obj.delRois}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnSaveRois = uicontrol( ...
'Style', 'pushbutton', ...
'Callback', {@obj.saveRois}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
%--
% FFT and complex mode display
%--
obj.hBtnFFT = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'FFT', ...
'Tooltip', 'Show the 2D FFT of the currently displayed data', ...
'Callback', {@obj.setFFTStatus}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnCmplx(1) = uicontrol( ...
'Style', 'togglebutton', ...
'String', 'abs', ...
'Tooltip', 'Show absolute value of data', ...
'Callback', {@obj.BtnCmplxCallback},...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnCmplx(2) = uicontrol( ...
'Style', 'togglebutton', ...
'String', 'phase', ...
'Tooltip', 'Show phase value of data', ...
'Callback', {@obj.BtnCmplxCallback},...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnCmplx(3) = uicontrol( ...
'Style', 'togglebutton', ...
'String', 'real', ...
'Tooltip', 'Show real part of data', ...
'Callback', {@obj.BtnCmplxCallback},...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnCmplx(4) = uicontrol( ...
'Style', 'togglebutton', ...
'String', 'imag', ...
'Tooltip', 'Show imaginary part of data', ...
'Callback', {@obj.BtnCmplxCallback},...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
%--
% animation
%--
if obj.nDims > 2
obj.hBtnRun = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'pixel', ...
'String', 'Run', ...
'Callback', { @obj.toggleTimer}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hEditF = uicontrol( ...
'Style', 'edit', ...
'Units', 'pixel', ...
'String', sprintf('%.2f', obj.fps), ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F, ...
'Enable', 'on', ...
'Tooltip', 'timer precision is 1 ms', ...
'Callback', @obj.setFPS);
obj.hTextFPS = uicontrol( ...
'Style', 'text', ...
'Units', 'pixel', ...
'String', sprintf('fps'), ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
end
%--
% saving
%--
obj.hBtnSaveImg = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'Save Image', ...
'Callback', {@obj.saveImgBtn}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnSaveVid = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'Save Video', ...
'Callback', {@obj.saveVidBtn}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.hBtnMinimMaxim = uicontrol( ...
'Style', 'pushbutton', ...
'String', '<', ...
'Callback', {@obj.minimMaximBtn}, ...
'BackgroundColor', obj.COLOR_BG, ...
'ForegroundColor', obj.COLOR_F);
obj.t = timer(...
'BusyMode', 'queue', ...
'ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'StartDelay', 0, ...
'TimerFcn', @(t, event) obj.interrupt, ...
'TasksToExecute', Inf);
end
function parseDimLabelsVals(obj)
% dimension labels
if ~contains('DimLabel', obj.p.UsingDefaults)
% check number of input labels equals dimensions of image
if numel(obj.p.Results.DimLabel) ~= obj.nDims
warning('Number of DimLabel is not equal to the number of image dimensions.')
end
% if cell entry is empty, use default value
emptyCell = cellfun(@isempty, obj.p.Results.DimLabel);
obj.dimLabel(~emptyCell) = obj.p.Results.DimLabel(~emptyCell);
end
% dimension values
% set default values via size of each dimension
obj.dimVal = cellfun(@(x) 1:x, num2cell(obj.S), 'UniformOutput', false);
if ~contains('DimVal', obj.p.UsingDefaults)
% check number of value arrays equals dimensions of image
if numel(obj.p.Results.DimVal) ~= obj.nDims
% allow for trailing singleton dimensions
% check for all surplus dimensions in DimVal, that
% their size is one.
if numel(obj.p.Results.DimVal) > obj.nDims && ~all(cellfun(@numel, obj.p.Results.DimVal(obj.nDims+1:end)))
error('Number of elements in DimVal must equal the number of image dimensions.')
end
end
% if cell entry is empty, use default value
emptyCell = cellfun(@isempty, obj.p.Results.DimVal);
obj.dimVal(~emptyCell) = obj.p.Results.DimVal(~emptyCell);
% value array for each dimension must have obj.S entries
if ~isequal(obj.S, cellfun(@numel, obj.dimVal))
error('Number of elements in DimVal for dimension(s) %s do not match image size', mat2str(find(obj.S ~= cellfun(@numel, obj.dimVal))))
end
end
obj.dimVal = valsToString(obj.dimVal);
end
function prepareSliceData(obj)
% obtain image information form
for iImg = 1:obj.nImages
for iax = 1:obj.nAxes
% get the image information of the current slice(s)
% from the input matrices
% for dimensions where one matrix is singleton, the
% selector-value must be adjusted to 1
select = obj.sel(iax, :);
select(obj.ston{iImg}) = {1};
if ~isempty(obj.ston{iImg})
% this matrix has singleton dimensions where the
% other does not
% is the matrix singleton along a shown dimension?
isect = intersect(obj.showDims(iax, :), obj.ston{iImg});
% if the matrix is singleton along one of the shown
% image dimensions, use repmat
repDims = ones(1, obj.nDims);
repDims(isect) = obj.S(isect);
else
% no unique singleton dimensions, no repetition
% necessary
repDims = ones(1, obj.nDims);
end
% use permute instead of squeeze, because squeeze
% reduces a matrix of size 1, 1, 100 to 100, 1
% instead of 1, 100
permDims = 1:obj.nDims;
permDims(obj.showDims(iax, :)) = [];
permDims = [obj.showDims(iax, :) permDims];
% repeat along singleton if necessary, then permute to
% get orientation right
obj.slice{iax, iImg} = permute(repmat(obj.img{iImg}(select{:}), repDims), permDims);
if obj.fftStatus == 1
% if chosen by the user, perform a 2D fft on the
% Data
obj.slice{iax, iImg} = fftshift(fftn(fftshift(obj.slice{iax, iImg})));
end
end
end
if any(obj.isComplex)
% convert the displayed data to the complex mode chosen by
% the user and then to datatype single
obj.slice = cellfun(@single, ...
cellfun(@obj.complexPart, obj.slice, 'UniformOutput', false), ...
'UniformOutput', false);
else
% convert the displayed data to datatype single
obj.slice = cellfun(@single, obj.slice, 'UniformOutput', false);
end
% Since the content in the axes changes, also recalculate the
% values inside the ROIs.
obj.calcROI
end
function cImage = sliceMixer(obj, axNo)
% calculates an RGB image depending on the windowing values,
% the used colormaps and the current slice position. when the
% slice position was changed, obj.prepareSliceData should be
% run before calling the slice mixer.
% axNo defines the axis for which the image is prepared.
if nargin == 1
axNo = 1;
end
imgSize = [obj.resize * size(obj.slice{axNo, 1}), 3];
switch (obj.overlay)
% for assignment of overlay modes, see
% obj.overlayStrings
case 1 % add
cImage = zeros(imgSize);
case 2 % multiply
cImage = ones(imgSize);
end
for idd = 1:obj.nImages
% map images to range [0, cmapResolution]
cLimLow = single(obj.center(obj.complexMode, idd) - obj.width(obj.complexMode, idd)/2);
imgMapped = (obj.slice{axNo, idd} - cLimLow)/single(obj.width(obj.complexMode, idd)) * size(obj.cmap{idd}, 1);
if obj.resize ~= 1
imgMapped = imresize(imgMapped, obj.resize);
end
imgRGB = ind2rgb(round(imgMapped+0.5), obj.cmap{idd}) * obj.layerShown(idd);
imgRGB(repmat(isnan(obj.slice{axNo, idd}), [1 1 3])) = 0;
switch (obj.overlay)
% for assignment of overlay modes, see
% obj.overlayStrings
case 1 % add
cImage = cImage + imgRGB;
case 2 % multiply
cImage = cImage .* imgRGB;
end
end
if obj.nImages ~= 1
cImage(isnan(obj.slice{axNo, 1}) & isnan(obj.slice{axNo, 2})) = NaN;
end
% make sure no channel has values above 1
cImage(cImage > 1) = 1;
% if at least one of the inputs is a gpuArray, gather it in
% order to be able to show it
cImage = gather(cImage);
end
function startDragFcn(obj, src, evtData)
% when middle mouse button is pressed, save current point and start