-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfloweddycurrent.m
1510 lines (1214 loc) · 39.6 KB
/
floweddycurrent.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 varargout = floweddycurrent(varargin)
%Eddy current code.
%Original code by Einar Heiberg.
%Refactured summer 2015 by Einar Heiberg
if nargin==0
init;
return;
end
macro_helper(varargin{:});
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
%----------------------
function init(plotonok)
%----------------------
%Initialize the main eddy current GUI
global DATA
%Check for errors
[ok,no] = safetychecks;
if ~ok
return;
end
%Check if we should plot after user press ok
if nargin<1
plotonok = false;
end
%Initalize
DATA.GUI.EddyCurrent = mygui('floweddycurrent.fig');
gui = DATA.GUI.EddyCurrent;
initvariables(no);
gui.plotonok = plotonok;
set(gui.fig, 'Pointer','arrow')
%Initialize graphics
graphicalinit;
%Find static tissue
findstatic;
%Calculate
recalculate_Callback;
%---------------------------
function initsmall(plotonok) %#ok<DEFNU>
%---------------------------
%Initialize the small automated eddy current GUI
global DATA
%Check for errors
[ok,no] = safetychecks;
if ~ok
return;
end
%Check if we should plot after user press ok
if nargin<1
plotonok = false;
end
%Initalize
DATA.GUI.EddyCurrent = mygui('floweddycurrentsmall.fig');
gui = DATA.GUI.EddyCurrent;
%Hide gui
%set(gui.fig,'visible','off');
initvariables(no);
gui.biggui = false; %Mark that the smaller GUI
gui.plotonok = plotonok;
%Find static tissue
findstatic;
%Calculate
recalculate_Callback; %This also calls update
%Show GUI
set(gui.fig,'visible','on','Pointer','arrow');
%-------------------------
function initvariables(no)
%-------------------------
global DATA SET
gui = DATA.GUI.EddyCurrent;
gui.phaseno = no;
gui.magno = SET(no).Flow.MagnitudeNo;
gui.slice = SET(no).CurrentSlice;
gui.timeframe = SET(no).CurrentTimeFrame;
gui.maxvel = 1;
gui.phasecorr = [];
gui.logim = [];
gui.badfit = [];
gui.biggui = true; %When true it is the main gui which is initialized.
gui.ignorerois = false; %If true then we do not care about stationary tissue or non stationary tissue
if isfield(SET(gui.phaseno).Flow, 'PhaseCorrRemoveBadFit')
gui.usebadfit = SET(gui.phaseno).Flow.PhaseCorrRemoveBadFit;
else
gui.usebadfit = true;
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorr')
SET(gui.no).Flow.PhaseCorr = [];
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorrPercentiles')
SET(gui.phaseno).Flow.PhaseCorrPercentiles = 0.6;
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorrMagnitudeThreshold')
SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold = 0.1;
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorrMethod')
SET(gui.phaseno).Flow.PhaseCorrMethod = 'linear';
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorrTimeResolved')
SET(gui.phaseno).Flow.PhaseCorrTimeResolved = false;
end
if ~isfield(SET(gui.phaseno).Flow,'PhaseCorrStaticTissueRois')
SET(gui.phaseno).Flow.PhaseCorrStaticTissueRois = false;
end
%---------------------
function graphicalinit
%---------------------
%Initialize graphics
global DATA SET
gui = DATA.GUI.EddyCurrent;
%This function only updates the big gui
if ~gui.biggui
return;
end
%Update boxes, sliders etc.
set(gui.handles.phaseslider,'Value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles);
set(gui.handles.phaseedit,'Value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles,'String',round(100*SET(gui.phaseno).Flow.PhaseCorrPercentiles));
imvals = SET(gui.magno).IM(:);
set(gui.handles.magnitudeslider, ...
'value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold, ...
'min',min(imvals(imvals > 0)));
set(gui.handles.magnitudeedit, ...
'Value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold, ...
'String',round(100*SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold)/100);
switch SET(gui.phaseno).Flow.PhaseCorrMethod
case 'linear'
v = 1;
case 'quadratic'
v = 2;
case 'static'
v = 1; %Backwards compability
SET(gui.phaseno).Flow.PhaseCorrMethod = 'linear';
case 'ge'
v = 3;
otherwise
myfailed(dprintf('Unknown method %s.',SET(gui.phaseno).Flow.PhaseCorrMethod));
return;
end
set(gui.handles.methodlistbox,'value',v);
set(gui.handles.timeresolvedcheckbox,'value',double(SET(gui.phaseno).Flow.PhaseCorrTimeResolved));
set(gui.handles.statictissueroischeckbox,'value',double(SET(gui.phaseno).Flow.PhaseCorrStaticTissueRois));
set(gui.handles.usebadfitcheckbox,'value',double(gui.usebadfit));
if SET(gui.phaseno).ZSize>1
set(gui.handles.sliceslider,'min',1,'max',SET(gui.phaseno).ZSize,'value',gui.slice);
else
set([gui.handles.slicetext gui.handles.sliceslider],'visible','off');
end
set(gui.handles.timeslider,'min',1,'max',SET(gui.phaseno).TSize,'value',gui.timeframe);
if ~SET(gui.phaseno).Flow.PhaseCorrTimeResolved
set([gui.handles.timetext gui.handles.timeslider],'visible','off');
end
%-------------------------------
function [res,no] = safetychecks
%-------------------------------
global SET NO
res = false;
no = NO;
%Check so that is single as the code requires it.
imissingle = classcheckim(NO);
if not(imissingle)
return;
end
%Error checking
if isempty(SET(NO).Flow)
myfailed('No flow data.');
return;
end
if isequal(NO,SET(NO).Flow.MagnitudeNo)
%Check if only one phase is available.
temp = [...
SET(NO).Flow.PhaseNo
SET(NO).Flow.PhaseX
SET(NO).Flow.PhaseY];
if length(temp)==1
no = temp;
else
myfailed('Can not do eddy current compensation on magnitude image with multiple phase images. Select a phase image stack.');
return;
end
end
%If this statement is reached then ok.
res = true;
%---------------------------
function ignorerois_Callback %#ok<DEFNU>
%---------------------------
%Turn on ignore ROIs mode. This will disable adding static and non static
%tissue ROIS
global DATA
gui = DATA.GUI.EddyCurrent;
gui.ignorerois = true;
%Find static tissue
findstatic;
%Calculate
recalculate_Callback;
%---------------------------
function timeslider_Callback
%---------------------------
%Timeslider Callback
global DATA SET
gui = DATA.GUI.EddyCurrent;
%Find temporal
SET(gui.phaseno).Flow.PhaseCorrTimeResolved = (1==get(gui.handles.timeresolvedcheckbox,'value'));
if SET(gui.phaseno).Flow.PhaseCorrTimeResolved
set([gui.handles.timetext gui.handles.timeslider],'visible','on');
gui.timeframe = mygetvalue(gui.handles.timeslider);
gui.timeframe = min(max(round(gui.timeframe),1),SET(gui.phaseno).TSize);
else
set([gui.handles.timetext gui.handles.timeslider],'visible','off');
gui.timeframe = 1;
end
update;
%-----------------------
function usebadfit_Callback %#ok<DEFNU>
%-----------------------
% Remove bad fit pixels checkbox callback
global DATA SET
gui = DATA.GUI.EddyCurrent;
chkboxvalue = get(gui.handles.usebadfitcheckbox, 'Value');
gui.usebadfit = logical(chkboxvalue);
SET(gui.phaseno).Flow.PhaseCorrRemoveBadFit = gui.usebadfit;
if ~gui.usebadfit
gui.badfit = [];
end
findstatic;
recalculate_Callback;
update;
%-----------------------
function static_Callback %#ok<DEFNU>
%-----------------------
%Static tissue checkbox callback
global DATA SET
gui = DATA.GUI.EddyCurrent;
SET(gui.phaseno).Flow.PhaseCorrStaticTissueRois = get(gui.handles.statictissueroischeckbox,'value');
findstatic;
recalculate_Callback;
update;
%-----------------------------
function sliceslider_Callback %#ok<DEFNU>
%-----------------------------
%Slice slider callback
global DATA SET
gui = DATA.GUI.EddyCurrent;
%Find slice
if SET(gui.phaseno).ZSize>1
gui.slice = mygetvalue(gui.handles.sliceslider);
gui.slice = min(max(round(gui.handles.slice),1),SET(gui.phaseno).ZSize);
else
gui.slice = 1;
end
update;
%------------------------------
function methodlistbox_Callback %#ok<DEFNU>
%------------------------------
%Update the method listbox
global SET DATA
gui = DATA.GUI.EddyCurrent;
%Find method
v = mygetvalue(gui.handles.methodlistbox);
switch v
case 1
SET(gui.phaseno).Flow.PhaseCorrMethod = 'linear';
case 2
SET(gui.phaseno).Flow.PhaseCorrMethod = 'quadratic';
case 3
SET(gui.phaseno).Flow.PhaseCorrMethod = 'ge';
end
recalculate_Callback;
%----------------------------------
function magnitudeslider_Callback %#ok<DEFNU>
%----------------------------------
%Magnitude threshold slider
global SET DATA
gui = DATA.GUI.EddyCurrent;
SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold = mygetvalue(gui.handles.magnitudeslider);
%Update
set(gui.handles.magnitudeslider,'value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold);
set(gui.handles.magnitudeedit,'Value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold,'String',round(100*SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold)/100);
findstatic;
recalculate_Callback;
update;
%------------------------------
function magnitudeedit_Callback %#ok<DEFNU>
%------------------------------
%Magnitude edit
global SET DATA
gui = DATA.GUI.EddyCurrent;
SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold = max(min(str2num(get(gui.handles.magnitudeedit,'String')), ...
get(gui.handles.magnitudeslider,'max')),get(gui.handles.magnitudeslider,'min'));
%Update
set(gui.handles.magnitudeslider,'value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold);
set(gui.handles.magnitudeedit,'Value',SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold,'String',round(100*SET(gui.phaseno).Flow.PhaseCorrMagnitudeThreshold)/100);
%----------------------------
function phaseslider_Callback %#ok<DEFNU>
%----------------------------
%Phase threshold slider
global SET DATA
gui = DATA.GUI.EddyCurrent;
SET(gui.phaseno).Flow.PhaseCorrPercentiles = min(1,max(0,mygetvalue(gui.handles.phaseslider)/100));
%Update
set(gui.handles.phaseslider,'value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles);
set(gui.handles.phaseedit,'Value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles,'String',round(100*SET(gui.phaseno).Flow.PhaseCorrPercentiles));
findstatic;
recalculate_Callback;
update;
%--------------------------
function phaseedit_Callback %#ok<DEFNU>
%--------------------------
%Phase threshold editor
global SET DATA
gui = DATA.GUI.EddyCurrent;
SET(gui.phaseno).Flow.PhaseCorrPercentiles = min(1,max(0,str2num(get(gui.handles.phaseedit,'String'))/100));
%Update
set(gui.handles.phaseslider,'value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles);
set(gui.handles.phaseedit,'Value',100*SET(gui.phaseno).Flow.PhaseCorrPercentiles,'String',round(100*SET(gui.phaseno).Flow.PhaseCorrPercentiles));
findstatic;
recalculate_Callback;
update;
%------------------------------
function timedependent_Callback %#ok<DEFNU>
%------------------------------
%User clicks on checkbox time dependent
findstatic;
timeslider_Callback;
recalculate_Callback;
update;
%--------------------------------
function [varargout] = findstatic
%--------------------------------
%Find static pixels store in gui variable
global SET DATA
gui = DATA.GUI.EddyCurrent;
myworkon;
varargout = cell(1,nargout);
%To simplify the code
no = gui.phaseno;
%Depending on method do different stuff.
switch SET(no).Flow.PhaseCorrMethod
case 'ge'
%Phase correction is given directly corresponding phantom
%experiment.
type = '';
if isequal(SET(no).Flow.PhaseX,no)
type = 'x';
end
if isequal(SET(no).Flow.PhaseY,no)
type = 'y';
end
if isequal(SET(no).Flow.PhaseNo,no)
type = 'z';
end
if isempty(type)
myfailed(dprintf('Could not determine direction of flow (x,y,z).'));
myworkoff;
return;
end
%--- Find corresponding phase image
point = SET(no).ImagePosition;
match = [];
for loop=1:length(SET)
if not(isempty(SET(loop).Flow))
%Flow data set at least...
if ~isequal(SET(loop).Flow.MagnitudeNo,SET(no).Flow.MagnitudeNo)
%They do not point to the same magnitude image i.e
%candidates.
candidate = false;
switch type
case 'x'
if isequal(SET(loop).Flow.PhaseX,loop)
candidate = true;
end
case 'y'
if isequal(SET(loop).Flow.PhaseY,loop)
candidate = true;
end
case 'z'
if isequal(SET(loop).Flow.PhaseNo,loop)
candidate = true;
end
end
if candidate
tempdir = cross(...
SET(loop).ImageOrientation(1:3),...
SET(loop).ImageOrientation(4:6));
temppoint = SET(loop).ImagePosition;
d = sum(tempdir(:).*temppoint(:));
%Check point
if abs(sum(point(:).*tempdir(:))-d)<1e-4
%The point fits in this plane, add it!.
match = [match loop]; %#ok<AGROW>
end
end
end %not same magnitude
end %Flow data set
end %Loop over image stacks
if isempty(match)
myfailed(dprintf('No matches found.'));
SET(no).Flow.PhaseCorrMethod = 'linear';
try
set(gui.handles.methodlistbox,'value',1);
catch
end
myworkoff;
return;
end
%Create cell array
matchcell = cell(1,length(match));
for loop=1:length(match)
matchcell{loop} = match(loop);
end
if length(match)>1
m = mymenu('More than one plane candidate found. Choose image stack',mathcell);
else
m = 1;
end
if isequal(m,0)
myfailed(dprintf('Aborted.'));
myworkoff;
return;
end
match = match(m);
gui.phasecorr = SET(match).IM-0.5; %Changed 2021-03-25 after being notified by Erik Andreas Rye Berg <[email protected]>. Checked it was incorrect: was 0.5-SET(match).IM;
gui.logim = SET(SET(match).Flow.MagnitudeNo).IM(:,:,1)>0.05; %ge limit
temp = gui.phasecorr;
%templog = repmat(~gui.logim,[1 1 SET(match).TSize]);
%temp(templog) = 0;
gui.phasecorr = temp;
clear templog;
gui.maxvel = max(temp(:));
gui.minvel = min(temp(:));
case {'linear','quadratic','fourth'}
%--- Find static pixels
if SET(no).Flow.PhaseCorrStaticTissueRois
if ~gui.ignorerois
%--- Use static tissue ROI's
rois = [];
for rloop=1:SET(gui.magno).RoiN
if isequal(SET(gui.magno).Roi(rloop).Name,'Static tissue')
rois = [rois rloop]; %#ok<AGROW>
end
end
if isempty(rois)
myfailed(dprintf('No static tissue regions marked.'));
SET(gui.phaseno).Flow.PhaseCorrStaticTissueRois = false;
try
set(gui.handles.statictissueroischeckbox,'value',0);
catch
end
return;
end
gui.logim = false([SET(no).XSize SET(no).YSize SET(no).ZSize]);
for rloop=1:length(rois)
frames = SET(gui.magno).Roi(rois(rloop)).T(1); %handles.NO is the magnitude image stack
slice = SET(gui.magno).Roi(rois(rloop)).Z(1);
roix = SET(gui.magno).Roi(rois(rloop)).X;
roiy = SET(gui.magno).Roi(rois(rloop)).Y;
% check if ROI is time resolved
numframes = length(frames);
nany = isnan(roiy(:));
nanx = isnan(roix(:));
if any(nanx) || any(nany)
% roi is not time resolved
% -> find time frame that has segmentation
[~,tf] = find(~isnan(roix));
if ~isempty(tf)
tf = min(tf);
SET(gui.magno).Roi(rois(rloop)).X = repmat(roix(:,tf),[1,SET(no).TSize]);
SET(gui.magno).Roi(rois(rloop)).Y = repmat(roiy(:,tf),[1,SET(no).TSize]);
roiarea = SET(gui.magno).Roi(rois(rloop)).Area(1,tf);
SET(gui.magno).Roi(rois(rloop)).Area = repmat(roiarea,[1,SET(no).TSize]);
roistd = SET(gui.magno).Roi(rois(rloop)).StD(1,tf);
SET(gui.magno).Roi(rois(rloop)).StD = repmat(roistd,[1,SET(no).TSize]);
roimean = SET(gui.magno).Roi(rois(rloop)).Mean(1,tf);
SET(gui.magno).Roi(rois(rloop)).Mean = repmat(roimean,[1,SET(no).TSize]);
end
end
for tloop = 1:numframes
gui.logim(:,:,slice) = gui.logim(:,:,slice) | segment('createmask',...
[SET(no).XSize SET(no).YSize],...
SET(gui.magno).Roi(rois(rloop)).Y(:,frames(tloop)),...
SET(gui.magno).Roi(rois(rloop)).X(:,frames(tloop)));
end
end
end
else
%Find ROI's to exclude
if ~gui.ignorerois
rois = SET(gui.magno).Roi;
rois = rois(strcmp({rois.Name},'Non-static tissue'));
excludeim = false([SET(no).XSize SET(no).YSize SET(no).ZSize]);
for rloop=1:length(rois)
frames = rois(rloop).T(1); %handles.NO is the magnitude image stack
slice = rois(rloop).Z(1);
for tloop = 1:length(frames)
excludeim(:,:,slice) = segment('createmask',...
[SET(no).XSize SET(no).YSize],...
rois(rloop).Y(:,frames(tloop)),...
rois(rloop).X(:,frames(tloop)));
end
end
else
excludeim = false([SET(no).XSize SET(no).YSize SET(no).ZSize]);
end
%Calculate the temporal standard deviation
stdmap = std(SET(no).IM,0,3);
magmap = mean(SET(gui.magno).IM,3);
stdmap(magmap<SET(no).Flow.PhaseCorrMagnitudeThreshold) = 0; %Exact zero
stdmap(excludeim) = 0;
%Divide into quadrants and find cut off value
xdiv = round(SET(no).XSize/2);
ydiv = round(SET(no).YSize/2);
q1 = stdmap(1:xdiv,1:ydiv,:,:); qv1 = sort(q1(:));
q2 = stdmap(1:xdiv,ydiv:end,:,:); qv2 = sort(q2(:));
q3 = stdmap(xdiv:end,1:ydiv,:,:); qv3 = sort(q3(:));
q4 = stdmap(xdiv:end,ydiv:end,:,:); qv4 = sort(q4(:));
%Remove zeros
qv1 = qv1(qv1~=0);
qv2 = qv2(qv2~=0);
qv3 = qv3(qv3~=0);
qv4 = qv4(qv4~=0);
%Find 10% lower percentile in each quadrant
qq = {qv1 qv2 qv3 qv4};
qp = zeros(1,4);
for qi = 1:4
qv = qq{qi};
lfpcp = round(length(qv)*SET(no).Flow.PhaseCorrPercentiles);
if ~isempty(qv) && lfpcp > 0
qp(qi) = qv(lfpcp);
else
qp(qi) = 0;
end
end
q1p = qp(1); q2p = qp(2); q3p = qp(3); q4p = qp(4);
%Use threshold in the four different quadrants
logim = false(size(stdmap));
logim(1:xdiv,1:ydiv,:,:) = (q1<q1p);
logim(1:xdiv,ydiv:end,:,:) = (q2<q2p);
logim(xdiv:end,1:ydiv,:,:) = (q3<q3p);
logim(xdiv:end,ydiv:end,:,:) = (q4<q4p);
%Remove zeros
logim(stdmap==0) = false;
gui.logim = logim;
end
%Recalculate
%floweddycurrent_Callback('recalculate');
otherwise
myfailed(dprintf('Method %s not implemented.',SET(no).Flow.PhaseCorrMethod));
myworkoff;
return;
end
%--- Exclude non static tissue
if ~gui.ignorerois
%Find ROI's
rois = [];
for rloop=1:SET(gui.magno).RoiN
if isequal(SET(gui.magno).Roi(rloop).Name,'Non-static tissue')
rois = [rois rloop]; %#ok<AGROW>
end
end
%Loop over ROI's and exclude them
for rloop=1:length(rois)
frames = SET(gui.magno).Roi(rois(rloop)).T(1);
slice = SET(gui.magno).Roi(rois(rloop)).Z(1);
for tloop = 1:length(frames)
gui.logim(:,:,slice) = gui.logim(:,:,slice) & ~segment('createmask',...
[SET(no).XSize SET(no).YSize],...
SET(gui.magno).Roi(rois(rloop)).Y(:,frames(tloop)),...
SET(gui.magno).Roi(rois(rloop)).X(:,frames(tloop)));
end
end
%Later fix for multiple slices
gui.logim = staticfix(gui.logim);
%--- Include static tissue
%Find ROI's
rois = [];
for rloop=1:SET(gui.magno).RoiN
if isequal(SET(gui.magno).Roi(rloop).Name,'Static tissue')
rois = [rois rloop]; %#ok<AGROW>
end
end
%Loop over ROI's and exclude them
for rloop=1:length(rois)
frames = SET(gui.magno).Roi(rois(rloop)).T(1);
slice = SET(gui.magno).Roi(rois(rloop)).Z(1);
for tloop = 1:length(frames)
gui.logim(:,:,slice) = gui.logim(:,:,slice) | segment('createmask',...
[SET(no).XSize SET(no).YSize],...
SET(gui.magno).Roi(rois(rloop)).Y(:,frames(tloop)),...
SET(gui.magno).Roi(rois(rloop)).X(:,frames(tloop)));
end
end
%Later fix for multiple slices
gui.logim = staticfix(gui.logim);
end %do not do this if ignorerois are true
%Calculate number of static pixels, this is only for research purposes and
%can later be deleted.
if nargout>1
rows = size(gui.logim,1);
half = round(rows/2);
numupper = sum(sum(gui.logim(1:half,:)));
numlower = sum(sum(gui.logim((half+1):end,:)));
varargout{1} = numupper;
varargout{2} = numlower;
end
myworkoff;
%----------------------------
function recalculate_Callback
%----------------------------
%Recalculate, called on top level
global SET DATA
gui = DATA.GUI.EddyCurrent;
%Find method
if isequal(SET(gui.phaseno).Flow.PhaseCorrMethod,'ge')
%Just to recalculate as we assume no bad fit
stop = recalculate;
if stop
return;
end
else
%First recalculation
stop = recalculate;
numtimes = 10;
if SET(gui.phaseno).Flow.PhaseCorrTimeResolved
numtimes = 2;
end
if stop
return;
end
if gui.usebadfit
for loop = 1:numtimes
%Find pixels with bad fit
findbadfit;
%Repeat recalculation with bad fit pixels removed
stop = recalculate;
if stop
return
end
end
end
end
%Graphical update
update;
%-------------------
function stop = recalculate
%-------------------
%Workhorse in recalculation
global SET DATA
stop = false;
gui = DATA.GUI.EddyCurrent;
no = gui.phaseno;
myworkon;
%Calculate phase correction map;
switch SET(no).Flow.PhaseCorrMethod
case 'linear'
order = 1;
case 'quadratic'
order = 2;
case 'fourth'
myfailed('Not yet implemented.');
return;
case 'ge'
findstatic; %Check that this works
return;
end
%Creat grid
[x,y,z] = ndgrid(1:SET(no).XSize,1:SET(no).YSize,1:SET(no).ZSize);
%Extract static pixels
if isempty(gui.badfit)
ind = find(gui.logim);
else
ind = find(gui.logim & (~gui.badfit)); %Remove badfit pixels
end
if length(ind)<50
myfailed('Too few points, aborting.');
stop = true;
myworkoff;
return;
end
%Cut away coordinates
xi = x(:); yi=y(:); zi=z(:);
xi = xi(ind);
yi = yi(ind);
zi = zi(ind);
%Set up equation system
switch order
case 1
if SET(no).ZSize>1
A = [ones(size(xi)) xi yi zi];
else
A = [ones(size(xi)) xi yi];
end
case 2
if SET(no).ZSize>1
A = [ones(size(xi)) xi yi zi xi.*yi xi.*zi yi.*zi xi.^2 yi.^2 zi.^2];
else
A = [ones(size(xi)) xi yi xi.*yi xi.^2 yi.^2];
end
%case 3
% if SET(handles.no).ZSize>1
% myfailed('Not yet implemented.');
% return;
% else
% A = zeros(length(xi),1+9);
% A(:,1) = 1;
% A(:,2) = xi; %x
% A(:,3) = A(:,2).*xi; %x^2
% A(:,4) = A(:,3).*xi; %x^3
% A(:,5) = yi; %y
% A(:,6) = A(:,5).*yi; %y^2
% A(:,7) = A(:,6).*yi; %y^3
% end
end
%Build matrix
switch order
case 1
if SET(no).ZSize>1
Abuild = [ones([size(x,1)*size(x,2)*size(x,3) 1]) x(:) y(:) z(:)];
else
Abuild = [ones([size(x,1)*size(x,2) 1]) x(:) y(:)];
end
case 2
if SET(no).ZSize>1
Abuild = [ones([size(x,1)*size(x,2)*size(x,3) 1]) x(:) y(:) z(:) x(:).*y(:) x(:).*z(:) y(:).*z(:) (x(:)).^2 (y(:)).^2 (z(:)).^2];
else
Abuild = [ones([size(x,1)*size(x,2) 1]) x(:) y(:) x(:).*y(:) (x(:)).^2 (y(:)).^2];
end
case 3
end
%Check if timeresolved
if SET(no).Flow.PhaseCorrTimeResolved
timeframes = SET(no).TSize;
handles.phasecorr = repmat(single(0),size(SET(no).IM));
else
timeframes = 1;
end
%--- Loop over timeframes if timeresolved other wise loop just once.
handles.maxvel = 0;
h = mywaitbarstart(timeframes,'Please wait.',1);
gui.maxvel = 0;
for tloop=1:timeframes
if timeframes>1
%Timeresolved
phase = SET(no).IM(:,:,tloop,:)-0.5;
else
phase = mean(SET(no).IM(:,:,:,:),3)-0.5;
end
%Make as vector
phase = phase(:);
%Take only stationary points
phase = phase(ind);
B = phase;
tempstate = warning;
warning('off');
c = A\double(B); %Solve!
warning(tempstate);
%Build back and store
if timeframes>1
gui.phasecorr(:,:,tloop,:) = reshape(Abuild*c,[size(x,1) size(x,2) 1 SET(no).ZSize]);
temp = gui.phasecorr(:,:,tloop,:);
else
gui.phasecorr = reshape(Abuild*c,[size(x,1) size(x,2) 1 SET(no).ZSize]);
temp = gui.phasecorr;
end
temp(gui.logim) = 0;
gui.maxvel = max(gui.maxvel,max(temp(:)));
h = mywaitbarupdate(h);
end %End of tloop
mywaitbarclose(h);
myworkoff;
%--------------------------------
function [varargout] = findbadfit
%--------------------------------
%Find pixels with bad fit, optionally returns how many unconnected badfits
%there were.
global DATA SET
gui = DATA.GUI.EddyCurrent;
varargout = cell(1,nargout);
%Calculate phase image, take mean phase
vel = 2*SET(gui.phaseno).VENC*(mean(SET(gui.phaseno).IM,3)-0.5);
%Calculate difference
if SET(gui.phaseno).Flow.PhaseCorrTimeResolved
diffvel = vel-mean(gui.phasecorr,3)*2*SET(gui.phaseno).VENC;
else
diffvel = vel-gui.phasecorr*2*SET(gui.phaseno).VENC;
end
%Take only static pixels
diffvel(~gui.logim) = 0;
%--- Do this also in quadrants (later)
%Sort the fitting error by size
diffthreshold = sort(abs(diffvel(:)));
diffthreshold = diffthreshold(diffthreshold~=0); %Remove zeros
%Remove the 25% worst fits
diffthreshold = diffthreshold(round(length(diffthreshold)*0.75));
mask = (diffvel>diffthreshold) | (diffvel<-diffthreshold);
%Store
gui.badfit = mask;
if nargout>0
%--- warning messages if not enough (bad balanced support).
[bwim] = bwlabel(mask,4); %4 connected regions
varargout{1} = max(bwim(:));
end
%----------------------
function clear_Callback %#ok<DEFNU>
%----------------------
%Clear phase background data
global SET DATA
gui = DATA.GUI.EddyCurrent;
no = gui.phaseno;
SET(no).Flow.PhaseCorr = [];