-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathecv.m
1791 lines (1615 loc) · 63.2 KB
/
ecv.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] = ecv(varargin)
%-----------------------------------
% Calculate ECV from MOLLI T1 stacks pre and post Gd
if nargin == 0
varargin{1} = 'init_Callback';
end
macro_helper(varargin{:});
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
%---------------------
function init_Callback %#ok<DEFNU>
%---------------------
%initialization of the ECV analysis
global SET NO
%find the thumbnail number of preT1map and postT1map
prenos = strcmp(strtrim({SET.ImageType}),'T1 map Pre');
postnos = strcmp(strtrim({SET.ImageType}),'T1 map Post');
prenos = find(prenos);
postnos = find(postnos);
%find image stacks with ROI blood
preno = [];
for no = prenos
islvendo = existfunctions('existendoinslices',no);
islvepi = existfunctions('existepiinslices',no);
if SET(no).RoiN >= 1 && any(strcmp({SET(no).Roi.Name},'Blood')) && sum(islvendo+islvepi);
preno = [preno no];
end
end
postno = [];
for no = postnos
islvendo = existfunctions('existendoinslices',no);
islvepi = existfunctions('existepiinslices',no);
if SET(no).RoiN >= 1 && any(strcmp({SET(no).Roi.Name},'Blood')) && sum(islvendo+islvepi);
postno = [postno no];
end
end
multislice = false;
singslicenos = [SET.ZSize] == 1;
if (numel(preno) > 1 && any(not(singslicenos(preno)))) || ...
(numel(postno) > 1 && any(not(singslicenos(postno))))
if ismember(NO,[preno postno])
if ismember(NO,preno)
preno = NO;
if numel(postno) > 1
for k =1:length(postno),postnostri{k}=postno(k);end
postnonbr = mymenu('Select post T1 map NO',postnostri);
if isempty(postnonbr)
mywarning('Not correctly defined image stack number');
return;
else
postno = postno(postnonbr);
end
end
else
postno = NO;
if numel(preno) > 1
for k =1:length(preno),prenostri{k}=preno(k);end
prenonbr = mymenu('Select post T1 map NO',prenostri);
if isempty(prenonbr)
mywarning('Not correctly defined image stack number');
return;
else
preno = preno(prenonbr);
end
end
end
else
myfailed('Multi-slice data. Select the image stack to analyze.');
return;
end
end
%check if multi-slice data, if then only use one image stack pair
if any(not(singslicenos(preno))) || any(not(singslicenos(postno)))
multislice = true;
if length(preno) > 1 || length(postno) > 1
%if multi-slice data, use only NO if that is applicable
if ismember(NO,[preno postno])
if ismember(NO,preno)
prenonbr = find(ismember(NO,preno));
suggestedpostno = postno(min(prenonbr,length(postno)));
if length(postno) > 1
for k =1:length(postno),postnostri{k}=postno(k);end
postnonbr = mymenu('Select post T1 map NO',postnostri);
if isempty(postnonbr)
mywarning('Not correctly defined image stack number');
return;
else
postno = postno(postnonbr);
end
else
postno = suggestedpostno;
end
preno = NO;
elseif ismember(NO,postno)
postnonbr = find(ismember(NO,postno));
suggestedpreno = preno(min(postnonbr,length(preno)));
if length(preno) > 1
for k =1:length(preno),prenostri{k}=preno(k);end
prenonbr = mymenu('Select pre T1 map NO',prenostri);
if isempty(prenonbr)
mywarning('Not correctly defined image stack number');
return;
else
preno = preno(prenonbr);
end
else
preno = suggestedpreno;
end
postno = NO;
end
else
myfailed('Multi-slice data. Select the image stack to analyze.');
return;
end
end
end
%error checks
if isempty(preno) || isempty(postno)
myfailed(['Could not find T1 map pre or T1 map post image stacks with ROI''s labelled ''Blood''.']);
return
end
if length(preno) ~= length(postno) %SET(preno).ZSize ~= SET(postno).ZSize
mywarning('Number of image stacks in pre- and post-T1 map must be equal');
return;
end
% if length(preno) > 3
% mywarning('Only supports up to 3 T1 map image stack pairs.');
% return;
% end
%ask for the hematocrit value
while true
hem = myinputdlg('Hematocrit [0 1]','Input value');
if isempty(hem)
return
else
hem = str2double(hem{1});
if hem < 0 && hem > 1
mywarning('The hematocrit value must be within [0 1]');
end
if hem >= 0 && hem <= 1
break
end
end
end
%find the pre T1 map image, ask if there are more than one image (tf>1)
askno = [];
loop = 1;
for sliceloop = 1:length(preno)
if SET(preno(sliceloop)).TSize > 1
askno = [askno preno(sliceloop)];
if loop==1
askstri{loop} = dprintf('Time frame for\n\npre T1 map NO %s',num2str(preno(sliceloop)));
else
askstri{loop} = dprintf('pre T1 map NO %s',num2str(preno(sliceloop)));
end
loop = loop+1;
end
end
if ~isempty(askno)
%initial guess on NO based on existens of LV segmentation
isLV=[];
for j=1:length(preno)
if ~isempty(SET(preno(j)).EndoX)
isLV(:,j)=sum(~isnan(SET(preno(j)).EndoX(1,:,:)),3)';
end
end
for j=1:length(preno)
if ~isempty(isLV) && sum(isLV(:,j))>1
[~,isLVind] = max(isLV(:,j));
guess{j} = num2str(isLVind);
else
guess{j}='';
end
end
%find time frame
while true
premaptftemp = myinputdlg(askstri,'Pre T1 map',1,guess);
if isempty(premaptftemp)
return;
end
ok = [];
premaptf = zeros(1,length(preno));
for k = 1:length(premaptftemp)
premaptf(k) = str2double(premaptftemp{k});
if isnan(premaptf(k)) || isempty(premaptf(k)) || premaptf(k)<1 || premaptf(k)>SET(askno(k)).TSize
ok = [ok 0];
else
ok = [ok 1];
end
end
if sum(ok) == length(premaptf)
break
else
mywarning('Not correctly defined time frame number');
end
end
else
premaptf = ones(1,length(preno));
end
%find the post T1 map image, ask if there are more than one image (tf>1)
askno = [];
askstri = {};
loop = 1;
for sliceloop = 1:length(postno)
if SET(postno(sliceloop)).TSize > 1
askno = [askno postno(sliceloop)];
if sliceloop == 1
askstri{loop} = dprintf('Time frame for \n\npost T1 map NO %s',num2str(postno(sliceloop)));
else
askstri{loop} = dprintf('post T1 map NO %s',num2str(postno(sliceloop)));
end
loop = loop+1;
end
end
if ~isempty(askno)
%initial guess on NO based on existens of LV segmentation
isLV=[];
for j=1:length(postno)
if ~isempty(SET(postno(j)).EndoX)
isLV(:,j)=sum(~isnan(SET(postno(j)).EndoX(1,:,:)),3)';
end
end
for j=1:length(postno)
if ~isempty(isLV)
[~,isLVind] = max(isLV(:,j));
guess{j} = num2str(isLVind);
else
guess{j}='';
end
end
while true
postmaptftemp = myinputdlg(askstri,'Post T1 maps',1,guess);
if isempty(postmaptftemp)
return;
end
ok = [];
postmaptf = zeros(1,length(postno));
for k = 1:length(postmaptftemp)
postmaptf(k) = str2double(postmaptftemp{k});
if isnan(postmaptf(k)) || isempty(postmaptf(k)) || postmaptf(k)<1 || postmaptf(k)>SET(askno(k)).TSize
ok = [ok 0];
else
ok = [ok 1];
end
end
if sum(ok) == length(postmaptf)
break
else
mywarning('Not correctly defined time frame number');
end
end
else
postmaptf = ones(1,length(postno));
end
%If more than one time frame, ask so the pre- and post images are correctly
%coupled
if length(preno) > 1
for k=1:length(postno)
guess{k} = num2str(postno(k));
if k==1
askstri{1} = dprintf('Coupled post T1 map for\n\npre T1 map NO %s',num2str(preno(k)));
else
askstri{k} = dprintf('pre T1 map NO %s',num2str(preno(k)));
end
end
while true
coupledpostnotemp = myinputdlg(askstri,'Pre T1 maps',1,guess);
if isempty(coupledpostnotemp)
return;
end
ok = [];
coupledpostno = zeros(1,length(preno));
for k = 1:length(coupledpostnotemp)
coupledpostno(k) = str2double(coupledpostnotemp{k});
if isnan(coupledpostno(k)) || isempty(coupledpostno(k)) || ~ismember(coupledpostno(k),postno)
ok = [ok 0];
else
ok = [ok 1];
end
end
if sum(ok) == length(coupledpostno)
postno = coupledpostno; %sort the postno according to input
break
else
mywarning('Not correctly defined post T1 map NO');
end
end
end
%check so the images have the same resolution
notok = 0;
for k = 1:length(preno)
if abs(SET(preno(k)).ResolutionX - SET(postno(k)).ResolutionX) > 1e-2 || ...
abs(SET(preno(k)).ResolutionY - SET(postno(k)).ResolutionY) > 1e-2
notok = 1;
end
end
if notok
mywarning('The pre T1 map and post T1 map must have the same image resolution');
return;
end
if multislice
%find slices with LV segmentation
endosegslicepre = find(existfunctions('existendoinslices',preno,premaptf));
endosegslicepost = find(existfunctions('existendoinslices',postno,postmaptf));
episegslicepre = find(existfunctions('existepiinslices',preno,premaptf));
episegslicepost = find(existfunctions('existepiinslices',postno,postmaptf));
lvsegslicepre = intersect(endosegslicepre,episegslicepre);
lvsegslicepost = intersect(endosegslicepost,episegslicepost);
%need to be the LV segmentation in the same number of slices in pre and
%post
if length(lvsegslicepre) ~= length(lvsegslicepost)
myfailed('Need to have LV segmentation in the same number of slices in the pre and post T1 maps');
return;
end
else
%check so there is LV segmentation in both images
for k = 1:length(preno)
if isempty(SET(preno(k)).EndoX) || isempty(SET(preno(k)).EpiX) || ...
all(isnan(SET(preno(k)).EndoX(1,premaptf(k),:))) || all(isnan(SET(preno(k)).EpiX(1,premaptf(k),:)))
notok = 1;
end
end
if notok
mywarning('No LV segmentation in pre T1 map found');
return
end
lvsegslicepre = 1;
for k = 1:length(preno)
if isempty(SET(postno(k)).EndoX) || isempty(SET(postno(k)).EpiX) || ...
all(isnan(SET(postno(k)).EndoX(1,postmaptf(k),:))) || all(isnan(SET(postno(k)).EpiX(1,postmaptf(k),:)))
notok = 1;
end
end
if notok
mywarning('No LV segmentation in post T1 map found');
return
end
lvsegslicepost = 1;
end
%open the GUI for visualization
opengui(preno,postno,hem,premaptf,postmaptf,multislice,lvsegslicepre,lvsegslicepost);
%------------------------
function roiinit_Callback %#ok<DEFNU>
%------------------------
% Calculate ECV from MOLLI T1 stacks pre and post Gd within predefined ROIs
global SET
rois = {};
prenos = strcmp(strtrim({SET.ImageType}),'T1 map Pre');
postnos = strcmp(strtrim({SET.ImageType}),'T1 map Post');
% prenos = strcmp(strtrim({SET.SeriesDescription}),'MOLLI pre-Gd');
% postnos = strcmp(strtrim({SET.SeriesDescription}),'MOLLI post-Gd');
singframenos = [SET.TSize] == 1;
prenos = find(prenos & singframenos);
postnos = find(postnos & singframenos);
preno = [];
for no = prenos
if SET(no).RoiN > 1 && any(strcmp({SET(no).Roi.Name},'Blood'))
preno = [preno no];
end
end
postno = [];
for no = postnos
if SET(no).RoiN > 1 && any(strcmp({SET(no).Roi.Name},'Blood'))
postno = [postno no];
end
end
if isempty(preno) || isempty(postno)
myfailed(['Could not find single frame pre- and post-Gd image stacks' ...
' containing multiple ROI''s including one labelled ''Blood''.']);
return
elseif numel(preno) > 1 || numel(postno) > 1
mywarning(['Found more than one stack of properly prepared ' ...
'pre- or post-Gd images. Taking first (arbitrary decision).']);
preno = preno(1);
postno = postno(1);
end
while true
hem = myinputdlg('Hematocrit','Input value');
if isempty(hem)
return
else
hem = str2double(hem{1});
if hem >= 0 && hem <= 1
break
end
end
end
premyo = [];
postmyo = [];
for preroi = SET(preno).Roi
roiindinpost = find(strcmp(preroi.Name,{SET(postno).Roi.Name}));
if numel(roiindinpost) > 1
mywarning(['Found multiple ROI''s with same label in post-Gd ' ...
'image stack. Taking first (arbitrary decision).']);
roiindinpost = roiindinpost(1);
end
if ~isempty(roiindinpost)
postroi = SET(postno).Roi(roiindinpost);
premask = segment('createmask',...
[SET(preno).XSize SET(preno).YSize],...
preroi.Y,preroi.X);
postmask = segment('createmask',...
[SET(postno).XSize SET(postno).YSize],...
postroi.Y,postroi.X);
preim = calcfunctions('calctruedata',SET(preno).IM(:,:,1,preroi.Z),preno);
postim = calcfunctions('calctruedata',SET(postno).IM(:,:,1,postroi.Z),postno);
if strcmp(preroi.Name,'Blood')
preblood = mean(preim(premask));
postblood = mean(postim(postmask));
else
rois = [rois preroi.Name];
premyo = [premyo mean(preim(premask))];
postmyo = [postmyo mean(postim(postmask))];
end
end
end
extracellularvolume = (1-hem).*(1./postmyo-1./premyo)/(1/postblood-1/preblood);
if nargout == 0
stri = '';
for i = 1:numel(rois)
stri = [stri sprintf('%s: %0.4f\n',rois{i},extracellularvolume(i))];
end
mymsgbox(stri,'ECV Report');
segment('cell2clipboard',[rois' num2cell(extracellularvolume')]);
end
%----------------------------------------------------
function opengui(preno,postno,hem,premaptf,postmaptf,multislice,lvsegslicepre,lvsegslicepost)
%----------------------------------------------------
%Open the ECV registration GUI. Performs automatic rigid registration of
%pre and post Gd images
%written by Helen Fransson 2014-06-12, updated 2015-06-23
global DATA SET
for k = 1:length(preno)
%create image and myocardial mask
preT1maptemp = SET(preno(k)).IM(:,:,premaptf(k),:);
postT1maptemp = SET(postno(k)).IM(:,:,postmaptf(k),:);
% preendomask = segment('createmask',...
% [SET(preno(k)).XSize SET(preno(k)).YSize],...
% SET(preno(k)).EndoY(:,premaptf(k)),SET(preno(k)).EndoX(:,premaptf(k)));
% preepimask = segment('createmask',...
% [SET(preno(k)).XSize SET(preno(k)).YSize],...
% SET(preno(k)).EpiY(:,premaptf(k)),SET(preno(k)).EpiX(:,premaptf(k)));
% premyomasktemp = preepimask-preendomask;
%calculate blood intensity
bloodroispost = find(strcmp({SET(postno(k)).Roi.Name},'Blood'));
bloodroispre = find(strcmp({SET(preno(k)).Roi.Name},'Blood'));
%post blood
if multislice
if numel(bloodroispost) > 1
for roiloop = 1:length(bloodroispost)
postroi = SET(postno(k)).Roi(bloodroispost(roiloop));
roiz(roiloop) = postroi.Z;
end
for sliceloop = 1:length(lvsegslicepre)
slicerois = find(roiz == lvsegslicepre(sliceloop));
roiim = [];
if isempty(slicerois)
%no roi in this slice, take mean in rois from all other sices
for roiloop = 1:length(bloodroispost)
postroi = SET(postno(k)).Roi(bloodroispost(roiloop));
postmask = segment('createmask',...
[SET(postno(k)).XSize SET(postno(k)).YSize],...
postroi.Y(:,postmaptf(k)),postroi.X(:,postmaptf(k)));
postim = calcfunctions('calctruedata',postT1maptemp(:,:,1,postroi.Z),postno(k));
roiim = [roiim;postim(postmask)];
end
else
%take mean of the rois in this slice
for roiloop = 1:length(slicerois)
postroi = SET(postno(k)).Roi(bloodroispost(slicerois(roiloop)));
postmask = segment('createmask',...
[SET(postno(k)).XSize SET(postno(k)).YSize],...
postroi.Y(:,postmaptf(k)),postroi.X(:,postmaptf(k)));
postim = calcfunctions('calctruedata',postT1maptemp(:,:,1,postroi.Z),postno(k));
roiim = [roiim;postim(postmask)];
end
end
postblood(lvsegslicepre(sliceloop)) = mean(roiim);
end
elseif numel(bloodroispost) == 1
postroi = SET(postno(k)).Roi(bloodroispost);
postmask = segment('createmask',...
[SET(postno(k)).XSize SET(postno(k)).YSize],...
postroi.Y(:,postmaptf(k)),postroi.X(:,postmaptf(k)));
postim = calcfunctions('calctruedata',postT1maptemp(:,:,1,postroi.Z),postno(k));
postbloodtemp = repmat(mean(postim(postmask)),[1 length(lvsegslicepre)]);
postblood(lvsegslicepre) = postbloodtemp;
else
myfailed('No ROI labeled ''Blood'' found in post T1 map');
return;
end
%pre blood
if numel(bloodroispre) > 1
for roiloop = 1:length(bloodroispre)
preroi = SET(preno(k)).Roi(bloodroispre(roiloop));
roiz(roiloop) = preroi.Z;
end
for sliceloop = 1:length(lvsegslicepre)
slicerois = find(roiz == lvsegslicepre(sliceloop));
roiim = [];
if isempty(slicerois)
%no roi in this slice, take mean in rois from all other sices
for roiloop = 1:length(bloodroispre)
preroi = SET(preno(k)).Roi(bloodroispre(roiloop));
premask = segment('createmask',...
[SET(preno(k)).XSize SET(preno(k)).YSize],...
preroi.Y(:,premaptf(k)),preroi.X(:,premaptf(k)));
preim = calcfunctions('calctruedata',preT1maptemp(:,:,1,preroi.Z),preno(k));
roiim = [roiim;preim(premask)];
end
else
%take mean of the rois in this slice
for roiloop = 1:length(slicerois)
preroi = SET(preno(k)).Roi(bloodroispre(slicerois(roiloop)));
premask = segment('createmask',...
[SET(preno(k)).XSize SET(preno(k)).YSize],...
preroi.Y(:,premaptf(k)),preroi.X(:,premaptf(k)));
preim = calcfunctions('calctruedata',preT1maptemp(:,:,1,preroi.Z),preno(k));
roiim = [roiim;preim(premask)];
end
end
preblood(lvsegslicepre(sliceloop)) = mean(roiim);
end
elseif numel(bloodroispre) == 1
preroi = SET(preno(k)).Roi(bloodroispre);
premask = segment('createmask',...
[SET(preno(k)).XSize SET(preno(k)).YSize],...
preroi.Y(:,premaptf(k)),preroi.X(:,premaptf(k)));
preim = calcfunctions('calctruedata',preT1maptemp(:,:,1,preroi.Z),preno(k));
prebloodtemp = repmat(mean(preim(premask)),[1 length(lvsegslicepre)]);
preblood(lvsegslicepre) = prebloodtemp;
else
myfailed('No ROI labeled ''Blood'' found in pre T1 map');
return;
end
else
if numel(bloodroispost) > 1
disp('Found multiple ROI''s labeled ''Blood''. Taking first (arbitrary decision).');
bloodroipost = bloodroispost(1);
else
bloodroipost = bloodroispost;
end
if numel(bloodroispre) > 1
disp('Found multiple ROI''s labeled ''Blood''. Taking first (arbitrary decision).');
bloodroipre = bloodroispre(1);
else
bloodroipre = bloodroispre;
end
postroi = SET(postno(k)).Roi(bloodroipost);
postmask = segment('createmask',...
[SET(postno(k)).XSize SET(postno(k)).YSize],...
postroi.Y(:,postmaptf(k)),postroi.X(:,postmaptf(k)));
preroi = SET(preno(k)).Roi(bloodroipre);
premask = segment('createmask',...
[SET(preno(k)).XSize SET(preno(k)).YSize],...
preroi.Y(:,premaptf(k)),preroi.X(:,premaptf(k)));
preim = calcfunctions('calctruedata',preT1maptemp(:,:,1,preroi.Z),preno(k));
postim = calcfunctions('calctruedata',postT1maptemp(:,:,1,postroi.Z),postno(k));
preblood(k) = mean(preim(premask));
postblood(k) = mean(postim(postmask));
end
% for preroi = SET(preno(k)).Roi
% roiindinpost = find(strcmp(preroi.Name,{SET(postno(k)).Roi.Name}));
% if numel(roiindinpost) > 1
% mywarning(['Found multiple ROI''s with same label in post-Gd ' ...
% 'image stack. Taking first (arbitrary decision).']);
% roiindinpost = roiindinpost(1);
% end
% if ~isempty(roiindinpost) && strcmp(preroi.Name,'Blood')
% postroi = SET(postno(k)).Roi(roiindinpost);
% premask = segment('createmask',...
% [SET(preno(k)).XSize SET(preno(k)).YSize],...
% preroi.Y(:,premaptf(k)),preroi.X(:,premaptf(k)));
% postmask = segment('createmask',...
% [SET(postno(k)).XSize SET(postno(k)).YSize],...
% postroi.Y(:,postmaptf(k)),postroi.X(:,postmaptf(k)));
% preim = calcfunctions('calctruedata',preT1maptemp(:,:,1,preroi.Z),preno(k));
% postim = calcfunctions('calctruedata',postT1maptemp(:,:,1,postroi.Z),postno(k));
% preblood = mean(preim(premask));
% postblood = mean(postim(postmask));
% end
% end
%calculate crop size (equal for all images)
[cropcenterpretemp,cropcenterposttemp,cropradiustemp] = calccropsize(preno(k),postno(k),premaptf(k),postmaptf(k));
if multislice
cropcenterpre = squeeze(cropcenterpretemp);
cropcenterpost = squeeze(cropcenterposttemp);
cropradius = squeeze(cropradiustemp)';
else
cropcenterpre(:,k) = cropcenterpretemp;
cropcenterpost(:,k) = cropcenterposttemp;
cropradius(:,k) = cropradiustemp;
end
imagesizepre(:,k) = [SET(preno(k)).XSize SET(preno(k)).YSize];
imagesizepost(:,k) = [SET(postno(k)).XSize SET(postno(k)).YSize];
end
cropradiusshared = calcsharedcropsize(cropcenterpre,cropcenterpost,cropradius,imagesizepre,imagesizepost);
for k = 1:length(preno)
%crop images and LV segmentation and ROIs
if multislice
for slice=1:length(lvsegslicepre)
[preT1mapcroptemp(:,:,1,lvsegslicepre(slice)),postT1mapcroptemp(:,:,1,lvsegslicepre(slice)), ...
endoxtemp(:,1,lvsegslicepre(slice)),endoytemp(:,1,lvsegslicepre(slice)), ...
epixtemp(:,1,lvsegslicepre(slice)),epiytemp(:,1,lvsegslicepre(slice)), ...
roitemp,roimasktemp{slice},roiareatemp{slice}] = ...
cropmaps(preno(k),postno(k),premaptf(k),postmaptf(k), ...
cropcenterpre(:,lvsegslicepre(slice)),cropcenterpost(:,lvsegslicepre(slice)),cropradiusshared,lvsegslicepre(slice));
roistemp{slice} = roitemp;
end
preT1mapcrop = preT1mapcroptemp;
postT1mapcrop = postT1mapcroptemp;
endox{k} = endoxtemp;
endoy{k} = endoytemp;
epix{k} = epixtemp;
epiy{k} = epiytemp;
rois{k} = roistemp;
roimask{k} = roimasktemp;
roiarea{k} = roiareatemp;
else
% preT1mapcrop = []; postT1mapcrop = [];
[preT1mapcrop,postT1mapcrop,endox{k},endoy{k},epix{k},epiy{k},rois{k},roimask{k},roiarea{k}] = ...
cropmaps(preno(k),postno(k),premaptf(k),postmaptf(k),cropcenterpre(:,k),cropcenterpost(:,k),cropradiusshared,1);
end
%calculates true intensity data
preT1map{k} = calcfunctions('calctruedata',preT1mapcrop,preno(k));
postT1map{k} = calcfunctions('calctruedata',postT1mapcrop,postno(k));
% %align images
% setstr = SET(preno(1));
% setstr.ImageType = [SET(preno(1)).ImageType ' Aligned'];
% setstr.IM = [];
% setstr.IM(:,:,1) = preT1map{k};
% setstr.IM(:,:,2) = postT1map{k};
% setstr.TSize = 2;
% setnew = alignSlides(setstr,1,2);
% postT1mapaligned{k} = setnew.IM(:,:,2);
postT1mapaligned{k} = postT1map{k};
%calculate ECV map
if multislice %&& length(postblood)>1
postT1mapslice = postT1map{k};
preT1mapslice = preT1map{k};
postT1mapalignedslice = postT1mapaligned{k};
for slice = 1:length(lvsegslicepre)
ecvmaptemp(:,:,1,lvsegslicepre(slice)) = (1-hem).*(1./postT1mapslice(:,:,1,lvsegslicepre(slice))-1./preT1mapslice(:,:,1,lvsegslicepre(slice)))/ ...
(1/postblood(lvsegslicepre(slice))-1/preblood(lvsegslicepre(slice)));
ecvmapalignedtemp(:,:,1,lvsegslicepre(slice)) = (1-hem).*(1./postT1mapalignedslice(:,:,1,lvsegslicepre(slice))-1./preT1mapslice(:,:,1,lvsegslicepre(slice)))/ ...
(1/postblood(lvsegslicepre(slice))-1/preblood(lvsegslicepre(slice)));
end
ecvmap{k} = ecvmaptemp;
ecvmapaligned{k} = ecvmapalignedtemp;
else
ecvmap{k} = (1-hem).*(1./postT1map{k}-1./preT1map{k})/(1/postblood(k)-1/preblood(k));
ecvmapaligned{k} = (1-hem).*(1./postT1mapaligned{k}-1./preT1map{k})/(1/postblood(k)-1/preblood(k));
end
% ecvmapmasked = ecvmap{k}.*premyomask{k};
%calculate ECV values for each ROI
preimroiall = preT1map{k};
postimroiall = postT1map{k};
postimroialignedall = postT1mapaligned{k};
roimaskz = roimask{k};
roiecvtemp = [];
roiecvalignedtemp = [];
if multislice
allrois = rois{k};
for sliceloop = 1:length(allrois)
roislices = allrois{sliceloop};
for roiloop = 1:length(roislices)
roi = roislices(roiloop);
slice = roi.Z;
preimroi = preimroiall(:,:,1,slice);
postimroi = postimroiall(:,:,1,slice);
postimroialigned = postimroialignedall(:,:,1,slice);
roimaskzslice = roimaskz{sliceloop};
preroimyo = preimroi(roimaskzslice{roiloop});
postroimyo = postimroi(roimaskzslice{roiloop});
postroimyoaligned = postimroialigned(roimaskzslice{roiloop});
temproiecvtemp = (1-hem).*(1./postroimyo-1./preroimyo)/(1/postblood(slice)-1/preblood(slice));
roiecvtemp{sliceloop}{roiloop} = max(0,min(1,temproiecvtemp(not(isinf(temproiecvtemp))))); %exclude Inf values, ECV within 0-100
temproiecvalignedtemp = (1-hem).*(1./postroimyoaligned-1./preroimyo)/(1/postblood(slice)-1/preblood(slice));
roiecvalignedtemp{sliceloop}{roiloop} = max(0,min(1,temproiecvalignedtemp(not(isinf(temproiecvalignedtemp))))); %exclude Inf values, ECV within 0-100
end
end
else
j = 1;
for roi = rois{k}
preimroi = preimroiall;
postimroi = postimroiall;
postimroialigned = postimroialignedall;
preroimyo = preimroi(roimaskz{j});
postroimyo = postimroi(roimaskz{j});
postroimyoaligned = postimroialigned(roimaskz{j});
temproiecvtemp = (1-hem).*(1./postroimyo-1./preroimyo)/(1/postblood(k)-1/preblood(k));
roiecvtemp{j} = max(0,min(1,temproiecvtemp(not(isinf(temproiecvtemp))))); %ECV within 0-100
temproiecvalignedtemp = (1-hem).*(1./postroimyoaligned-1./preroimyo)/(1/postblood(k)-1/preblood(k));
roiecvalignedtemp{j} = max(0,min(1,temproiecvalignedtemp(not(isinf(temproiecvalignedtemp))))); %ECV within 0-100
j = j+1;
end
end
roiecv{k} = roiecvtemp;
roiecvaligned{k} = roiecvalignedtemp;
end
%%%%%%%%%%%%%%%% Open GUI %%%%%%%%%%%%%%%%%
if isopengui('ecvregistration.fig')
gui = DATA.GUI.ECVRegistration;
figure(gui.fig);
else
DATA.GUI.ECVRegistration = mygui('ecvregistration.fig');
gui = DATA.GUI.ECVRegistration;
myadjust(gui.fig,DATA.GUI.Segment);
end
gui.preno = preno;
gui.postno = postno;
gui.preT1map = preT1map;
gui.postT1map = postT1map;
gui.postT1mapaligned = postT1mapaligned;
gui.postT1mapplot = gui.postT1mapaligned;
gui.ecvmap = ecvmap;
gui.ecvmapaligned = ecvmapaligned;
gui.ecvmapplot = gui.ecvmapaligned;
gui.endox = endox;
gui.endoy = endoy;
gui.epix = epix;
gui.epiy = epiy;
gui.hem = hem;
gui.rois = rois;
% gui.roimask = roimask;
gui.roiarea = roiarea;
gui.roiecv = roiecv;
gui.roiecvaligned = roiecvaligned;
gui.multislice = multislice;
%for the reporter the following is stored into the SET
for i = 1:length(preno)
SET(preno(i)).ECV.preno = 1;
SET(postno(i)).ECV.preno = 0;
SET(preno(i)).ECV.postno = 0;
SET(postno(i)).ECV.postno = 1;
SET(postno(i)).ECV.hem = hem;
SET(preno(i)).ECV.hem = hem;
SET(postno(i)).ECV.roiecv = roiecv;
SET(preno(i)).ECV.roiecv = roiecv;
SET(preno(i)).ECV.roiarea = roiarea;
SET(postno(i)).ECV.roiarea = roiarea;
SET(postno(i)).ECV.multislice = multislice;
SET(preno(i)).ECV.multislice = multislice;
SET(postno(i)).ECV.rois = rois;
SET(preno(i)).ECV.rois = rois;
end
gui.premaptf = premaptf;
gui.postmaptf = postmaptf;
gui.lvsegslicepre = lvsegslicepre;
gui.lvsegslicepost = lvsegslicepost;
%set the view max T1 map values sliders and editboxes
gui.viewmaxpre = 2000;
gui.viewmaxpost = 2000;
set(gui.handles.viewmaxpreslider,'value',gui.viewmaxpre);
set(gui.handles.viewmaxpreedit,'value',gui.viewmaxpre);
set(gui.handles.viewmaxpreedit,'String',gui.viewmaxpre);
set(gui.handles.viewmaxpostslider,'value',gui.viewmaxpost);
set(gui.handles.viewmaxpostedit,'value',gui.viewmaxpost);
set(gui.handles.viewmaxpostedit,'String',gui.viewmaxpost);
%plot T1 maps and ECV map
set(gui.handles.applyalignmentradiobutton,'value',1);
set(gui.handles.disablealignmentradiobutton,'value',0);
plotimages(multislice,lvsegslicepre,lvsegslicepost);
plotecv(multislice,lvsegslicepre);
%plot LV segmentation and ROIs from the pre T1 map in alla images
set(gui.handles.showsegmentationradiobutton,'value',1);
set(gui.handles.hidesegmentationradiobutton,'value',0);
plotLVsegmentation(multislice,lvsegslicepre);
plotROI(multislice,lvsegslicepre);
%-------------------------------------------
function z = reshape2layout(im,rows,cols,sz)
%-------------------------------------------
%Convert a 3D array to an layout:ed image with cols, and rows
z = repmat(im(1),rows*sz(1),cols*sz(2));
loop = 1;
for slice = 1:sz(3)
c = 1+mod(loop-1,cols);
r = ceil(loop/cols);
z((1+(r-1)*sz(1)):(r*sz(1)),...
(1+(c-1)*sz(2)):(c*sz(2))) = im(:,:,slice);
loop = loop+1;
end
%-------------------------------------------------------------------------
function [cropcenterpre,cropcenterpost,cropradius] = calccropsize(preno,postno,premaptf,postmaptf)
%-------------------------------------------------------------------------
%calculate crop size for the maps based on the LV segmentation
global SET
%find proper crop size from preT1map
preLVcenterX = mean(SET(preno).EpiX(:,premaptf,:));
preLVcenterY = mean(SET(preno).EpiY(:,premaptf,:));
preLVwidth = max([max(SET(preno).EpiX(:,premaptf,:))-min(SET(preno).EpiX(:,premaptf,:)) ...
max(SET(preno).EpiY(:,premaptf,:))-min(SET(preno).EpiY(:,premaptf,:))]);
prexmin = max(1,round(preLVcenterX-1.2*preLVwidth));
prexmax = min(SET(preno).XSize,round(preLVcenterX+1.2*preLVwidth));
preymin = max(1,round(preLVcenterY-1.2*preLVwidth));
preymax = min(SET(preno).YSize,round(preLVcenterY+1.2*preLVwidth));
prexsize = prexmax-prexmin;
preysize = preymax-preymin;
%find proper crop size from postT1map
postLVcenterX = mean(SET(postno).EpiX(:,postmaptf,:));
postLVcenterY = mean(SET(postno).EpiY(:,postmaptf,:));
postLVwidth = max([max(SET(postno).EpiX(:,postmaptf,:))-min(SET(postno).EpiX(:,postmaptf,:)) ...
max(SET(postno).EpiY(:,postmaptf,:))-min(SET(postno).EpiY(:,postmaptf,:))]);
postxmin = max(1,round(postLVcenterX-1.2*postLVwidth));
postxmax = min(SET(postno).XSize,round(postLVcenterX+1.2*postLVwidth));
postymin = max(1,round(postLVcenterY-1.2*postLVwidth));
postymax = min(SET(postno).YSize,round(postLVcenterY+1.2*postLVwidth));
postxsize = postxmax-postxmin;
postysize = postymax-postymin;
%find final crop size, the same for preT1map and postT1map
minxsize = min(prexsize,postxsize);
minysize = min(preysize,postysize);
if any(minxsize-prexsize<0)
reducex = prexsize-minxsize;
prexmin = prexmin+ceil(reducex/2);
prexmax = prexmax-floor(reducex/2);
end
if any(minxsize-postxsize < 0)
reducex = postxsize-minxsize;
postxmin = postxmin+ceil(reducex/2);
postxmax = postxmax-floor(reducex/2);
end
if any(minysize-preysize < 0)
reducey = preysize-minysize;
preymin = preymin+ceil(reducey/2);
preymax = preymax-floor(reducey/2);
end
if any(minysize-postysize < 0)
reducey = postysize-minysize;
postymin = postymin+ceil(reducey/2);
postymax = postymax-floor(reducey/2);
end
%exclude slices with no LV segmentation
prexmin(isnan(preLVcenterX)) = NaN;
prexmax(isnan(preLVcenterX)) = NaN;
preymin(isnan(preLVcenterX)) = NaN;
preymax(isnan(preLVcenterX)) = NaN;
postxmin(isnan(preLVcenterX)) = NaN;
postxmax(isnan(preLVcenterX)) = NaN;
postymin(isnan(preLVcenterX)) = NaN;
postymax(isnan(preLVcenterX)) = NaN;
% cropcenterpre = round([mean([prexmin prexmax]) mean([preymin preymax])]);
% cropcenterpost = round([mean([postxmin postxmax]) mean([postymin postymax])]);
cropcenterpre = round([preLVcenterX preLVcenterY]);
cropcenterpost = round([postLVcenterX postLVcenterY]);
cropradius = round((prexmax-prexmin)/2);
%-------------------------------------------------------------------------
function maxcropradius = calcsharedcropsize(cropcenterpre,cropcenterpost,cropradius,imagesizepre,imagesizepost)
%---------------------------------------------------------------------
%calculate shared crop size for the maps based on proposed crop sizes for
%each image
maxcropradius = max(cropradius);
xmin = min([cropcenterpre(1,:)-maxcropradius cropcenterpost(1,:)-maxcropradius]);
ymin = min([cropcenterpre(2,:)-maxcropradius cropcenterpost(2,:)-maxcropradius]);
xmaxpre = max(cropcenterpre(1,:)+maxcropradius);
xmaxpost = max(cropcenterpost(1,:)+maxcropradius);
ymaxpre = max(cropcenterpre(2,:)+maxcropradius);
ymaxpost = max(cropcenterpost(2,:)+maxcropradius);
if xmin<1 || ymin<1 || xmaxpre>min(imagesizepre(1,:)) || xmaxpost>min(imagesizepost(1,:)) || ...
ymaxpre>min(imagesizepre(2,:)) || ymaxpost>min(imagesizepost(2,:))
%reduce the radius so the crop is inside all images
%find largest exceeding value
exceeding = [xmin-1 ymin-1 min(imagesizepre(1,:))-xmaxpre min(imagesizepost(1,:))-xmaxpost min(imagesizepre(2,:))-ymaxpre min(imagesizepost(2,:))-ymaxpost];
minval = min(exceeding);
maxcropradius = maxcropradius+minval;
disp('Coorecting common crop size');
end
%----------------------------------------------------------------
function [preT1map,postT1map,endox,endoy,epix,epiy,rois,roimask,roiarea] = ...
cropmaps(preno,postno,premaptf,postmaptf,cropcenterpre,cropcenterpost,cropradius,slice)
%----------------------------------------------------------------
%crop the T1 maps based on the LV segmentation, so the two T1 maps have the
%same size afterwards
global SET
%crop images
preT1map = SET(preno).IM(cropcenterpre(1)-cropradius:cropcenterpre(1)+cropradius, ...
cropcenterpre(2)-cropradius:cropcenterpre(2)+cropradius,premaptf,slice);
postT1map = SET(postno).IM(cropcenterpost(1)-cropradius:cropcenterpost(1)+cropradius, ...
cropcenterpost(2)-cropradius:cropcenterpost(2)+cropradius,postmaptf,slice);
%crop LV segmentation
endox = SET(preno).EndoX(:,premaptf,slice)-(cropcenterpre(1)-cropradius)+1;
endoy = SET(preno).EndoY(:,premaptf,slice)-(cropcenterpre(2)-cropradius)+1;
epix = SET(preno).EpiX(:,premaptf,slice)-(cropcenterpre(1)-cropradius)+1;
epiy = SET(preno).EpiY(:,premaptf,slice)-(cropcenterpre(2)-cropradius)+1;
if SET(preno).RoiN > 1 %ROIs in addition to blood pool ROI
rois = [];
j = 1;
for preroi = SET(preno).Roi
if ~strcmp(preroi.Name,'Blood')
if preroi.Z == slice
temproi = preroi;
temproi.X = temproi.X-(cropcenterpre(1)-cropradius)+1;
temproi.Y = temproi.Y-(cropcenterpre(2)-cropradius)+1;
rois = [rois temproi];
roimask{j} = segment('createmask',...
[size(preT1map,1) size(preT1map,2)],...
preroi.Y(:,premaptf)-(cropcenterpre(2)-cropradius)+1,preroi.X(:,premaptf)-(cropcenterpre(1)-cropradius)+1);
roiarea(j) = temproi.Area(:,premaptf);
j = j+1;
end
end
end
else
rois = [];
roimask = [];
roiarea = [];
end
%----------------------------------
function setviewmax_Callback(button) %#ok<DEFNU>
%----------------------------------
%read in translation and rotation
global DATA
gui = DATA.GUI.ECVRegistration;
switch button
case 'viewmaxpreslider' %left-right translation
viewmaxpre = mygetslider(gui.handles.viewmaxpreslider);
set(gui.handles.viewmaxpreedit,'String',round(viewmaxpre));
set(gui.handles.viewmaxpreedit,'value',viewmaxpre);
gui.viewmaxpre = viewmaxpre;
case 'viewmaxpreedit'
viewmaxpre = min(max(str2num(mygetedit(gui.handles.viewmaxpreedit)),get(gui.handles.viewmaxpreslider,'min')),get(gui.handles.viewmaxpreslider,'max')); %#ok<ST2NM>
set(gui.handles.viewmaxpreslider,'value',viewmaxpre);
set(gui.handles.viewmaxpreedit,'String',round(viewmaxpre));
set(gui.handles.viewmaxpreedit,'value',viewmaxpre);
gui.viewmaxpre = viewmaxpre;
case 'viewmaxpostslider' %left-right translation
viewmaxpost = mygetslider(gui.handles.viewmaxpostslider);
set(gui.handles.viewmaxpostedit,'String',round(viewmaxpost));
set(gui.handles.viewmaxpostedit,'value',viewmaxpost);
gui.viewmaxpost = viewmaxpost;
case 'viewmaxpostedit'
viewmaxpost = min(max(str2num(mygetedit(gui.handles.viewmaxpostedit)),get(gui.handles.viewmaxpostslider,'min')),get(gui.handles.viewmaxpostslider,'max')); %#ok<ST2NM>
set(gui.handles.viewmaxpostslider,'value',viewmaxpost);
set(gui.handles.viewmaxpostedit,'String',round(viewmaxpost));
set(gui.handles.viewmaxpostedit,'value',viewmaxpost);
gui.viewmaxpost = viewmaxpost;
end