-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEACH_FF_R2.m
1897 lines (1465 loc) · 58.1 KB
/
BEACH_FF_R2.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 = BEACH_FF_R2(varargin)
%% Dr Tim Bray
%
% This code implements the tool for ADC measurement described in:
% Histographic analysis of oedema and fat in inflamed bone marrow based on qMRI.
% Bray et al. European Radiology April 2020
%
% The code is based on the earlier 'roianal' software with additional
% functionality to enable
% (1) semi-automated ROI placement once the observer has defined the joint
% (2) histographic analysis of voxel values in the ROI
% (3) in BEACH_FF, the ROIs are automatically propagated onto R2* maps
% (acquired together with the PDFF maps) to enable 2D histogram / density
% analysis - this version therefore requires two inputs
%
%%
% BEACH_FF_R2(FFvol,R2vol)
% BEACH_FF_R2(FFvol,R2vol, matp, 'parameter',value,...)
%
% [handles, roi] = BEACH_FF_R2(...)
%
% outp is a structure from d2mat with outp.geom.XData and YData, or
% outp.XData and YData
%
% Parameter value pairs allowed:
% 'ismodal' true | {false}
% 'Name'
%
% BEACH_FF_R2, by itself, creates a new BEACH_FF_R2 or raises the existing
% singleton*.
%
% H = BEACH_FF_R2 returns the handle to a new BEACH_FF_R2 or the handle to
% the existing singleton*.
%
% BEACH_FF_R2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BEACH_FF_R2 with the given input arguments.
%
% BEACH_FF_R2('Property','Value',...) creates a new BEACH_FF_R2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before roianal_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to roianal_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES, profile_anal
% Edit the above text to modify the response to help roianal
% Last Modified by GUIDE v2.5 17-Jul-2020 16:22:11
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @roianal_OpeningFcn, ...
'gui_OutputFcn', @roianal_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before roianal is made visible.
function roianal_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to roianal (see VARARGIN)
% Choose default command line output for roianal
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes roianal wait for user response (see UIRESUME)
% uiwait(handles.figure1);
hf = figure('Name','roianalfig') ;
set(hf,'Tag','roianalfig')
disp(['Opened fig: ',num2str(fignum(hf))])
vol = varargin{1} ;
vol2 = varargin{2};
if size(vol,3) == 1
vol = squeeze(vol) ;
vol2=squeeze(vol2) ;
end
nslice = size(vol,3) ;
%initial slice
slice1 = floor(nslice/2) + 1;
%set slider and edit box
if nslice > 1
set(handles.slider_slice,'Min',1,'Max',nslice,'SliderStep',[1/(nslice-1) max(0.1,1/(nslice-1)) ],...
'Value', slice1) ;
else
set(handles.slider_slice,'Visible','off')
end
set(handles.edit_slice,'String',num2str(slice1)) ;
if length(varargin) < 2 || ~isstruct(varargin{2})
XData(1) = 1 ; YData(1) = 1 ;
XData(2) = XData(1) + size(vol,2)-1 ;
YData(2) = YData(1) + size(vol,1)-1 ;
else
if isfield(varargin{2},'geom')
geom = varargin{2}.geom ;
else
geom = varargin{2} ;
end
XData = geom.XData ;
YData = geom.YData ;
if isfield(varargin{2},'TinSeriesVec')
UDfig.TinSeries = varargin{2}.TinSeriesVec/1e6 ;
end
end
%warm-up axes
himshow = imshow(vol(:,:,slice1),[],'XData',XData,'YData',YData) ;
ha = gca ;
set(himshow,'Visible','off')
%apply all slices to axes, but make in visible
ip = {'XData',XData,'YData',YData,'CDataMapping','scaled','Visible','off','CData'} ;
for islice = 1:size(vol,3)
him(islice) = image(ip{:},vol(:,:,islice)) ;
end
ismodal= false ;
if nargin > 2
for ip =3 : 2 : length(varargin)
switch varargin{ip}
case 'ismodal'
if varargin{ip+1} ~= false
ismodal = true ;
set(handles.pushbutton_finish,'Visible','on')
end
case {'Name','name'}
Name = varargin{ip+1} ;
set(hf,'Name',Name)
otherwise
warning(['Unknown parameter: ',varargin{ip}])
end
end
end
if ~exist('buildroianal.m','file')
set(handles.pushbutton_report,'Enable','off')
end
UDcont.ismodal = ismodal ;
UDfig.nslice = nslice ;
UDfig.him = him ;
UDcont.hf = hf ; % control panel records active figure
UDfig.ha = ha ;
UDfig.vol = vol ;
UDfig.vol2= vol2 ;
set(handles.figure1,'UserData',UDcont) ;
set(hf,'UserData',UDfig) ;
draw_slice(handles) ;
update_activefig(handles) ;
turn_off_link(handles) ;
update_listbox(handles)
if ismodal
uiwait(handles.figure1);
end
%-------------------------------------
function draw_slice(handles)
slicestr = get(handles.edit_slice,'String') ;
islice = str2num(slicestr) ;
UDcont = get(handles.figure1,'UserData') ;
hf = UDcont.hf ;
UD = get(hf,'UserData') ;
draw_slice_one(UD,islice,hf) ;
islinked = get(handles.checkbox1,'Value');
if islinked
val = get(handles.popupmenu_linkfig,'Value') ;
str = get(handles.popupmenu_linkfig,'String') ;
fig = sscanf(str{val},'Fig: %d%s') ;
hflink = fig(1) ;
UDlink = get(hflink,'UserData') ;
draw_slice_one(UDlink,islice,hflink) ;
end
function roi_on_slice = draw_slice_one(UD,islice,hf)
set(UD.him,'Visible','off')
set(UD.him(islice),'Visible','on')
UD.disp_slice = islice ;
set(hf,'UserData',UD) ;
roi_on_slice = false ;
if isfield(UD,'hroi')
hrois = [UD.hroi] ;
for iroi = 1: length(hrois)
if ~isempty(hrois{iroi})
sl = UD.roislice(iroi) ;
if islice == sl
set(hrois{iroi},'Visible','on')
roi_on_slice = true ;
else
set(hrois{iroi},'Visible','off')
end
end
end
end
function draw_montage(handles)
% DRAW_MONTAGE Compiles montage from slices with ROI drawn
slicestr = get(handles.edit_slice,'String') ;
curr_slice = str2num(slicestr) ;
UDcont = get(handles.figure1,'UserData') ;
hf = UDcont.hf ;
UD = get(hf,'UserData') ;
nslice = size(UD.vol , 3) ;
iframe = 0 ;
M = [] ;
for islice = 1:nslice
roi_on_slice = draw_slice_one(UD,islice,hf) ;
if roi_on_slice
iframe = iframe + 1 ;
f = getframe(UD.ha) ;
[im,map] = frame2im(f); %Return associated image data
if isempty(map) %Truecolor system
rgb = im;
else %Indexed system
rgb = ind2rgb(im,map); %Convert image data
end
M = cat(4,M,rgb) ;
end
end
draw_slice_one(UD,curr_slice,hf) ;
if iframe > 0
figure('Name','roianal_montage','Tag','roianal_montage')
montage(M)
end
% --- Outputs from this function are returned to the command line.
function varargout = roianal_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
if nargout > 1
UDcont = get(handles.figure1,'UserData') ;
UD = get(UDcont.hf,'Userdata') ;
lstr = get(handles.listbox_rois,'String') ;
lval = get(handles.listbox_rois,'Value') ;
nsel = length(lval) ;
rois = zeros([1 nsel]) ;
for isel = 1:nsel
sstr = lstr{lval(isel)} ;
rois(isel) = strread(sstr(1:4),'%u') ;
end
posc = cell([1 nsel]) ;
slice = zeros([1 nsel]) ;
BWc = cell([1 nsel]) ;
labelc = cell([1 nsel]) ;
for isel = 1:nsel
pos = getPosition(UD.hroi{rois(isel)}) ;
posc{isel} = pos ;
slice(isel) = UD.roislice(rois(isel)) ;
BW = createMask(UD.hroi{rois(isel)}, UD.him(slice(isel))) ;
BWc{isel} = BW ;
labelc{isel} = UD.roilabel(rois(isel));
end
roi.posc = posc ;
roi.slice = slice ;
roi.BWc = BWc ;
roi.labelc = labelc ;
varargout{2} = roi ;
end
% --- Executes on button press in pushbutton_close.
function pushbutton_close_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(handles.figure1)
% --- Executes on slider movement.
function slider_slice_Callback(hObject, eventdata, handles)
% hObject handle to slider_slice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
islice = get(hObject,'Value') ;
islice = round(islice) ;
set(hObject,'Value',islice) ;
set(handles.edit_slice,'String',num2str(islice) ) ;
draw_slice(handles) ;
% --- Executes during object creation, after setting all properties.
function slider_slice_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider_slice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit_slice_Callback(hObject, eventdata, handles)
% hObject handle to edit_slice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_slice as text
% str2double(get(hObject,'String')) returns contents of edit_slice as a double
islice = str2double(get(hObject,'String')) ;
set(handles.slider_slice,'Value',islice)
draw_slice(handles) ;
% --- Executes during object creation, after setting all properties.
function edit_slice_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_slice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_imcontrast.
function pushbutton_imcontrast_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_imcontrast (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
UDcont = get(handles.figure1,'UserData') ;
islice = str2double(get(handles.edit_slice,'String')) ;
UD = get(UDcont.hf,'UserData') ;
imcontrast(UD.him(islice))
% --- Executes on selection change in listbox_rois.
function listbox_rois_Callback(hObject, eventdata, handles)
% hObject handle to listbox_rois (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox_rois contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox_rois
% --- Executes during object creation, after setting all properties.
function listbox_rois_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox_rois (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'Max',2) ; % allow multiple selection
% --- Executes on button press in pushbutton_newroi.
function pushbutton_newroi_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_newroi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
UDcont = get(handles.figure1,'UserData') ;
hf = UDcont.hf ;
UD = get(hf,'UserData') ; %UD contains stack of images, number of slices, current slice, and some scaling info
[hroi_1,hroi_2] = draw_imroi(handles,UD.ha) ; %hroi_this is a polygon
if isfield(UD,'hroi')
hrois = UD.hroi ;
nroi = length(hrois) ;
else
nroi = 0 ;
end
islice = str2double(get(handles.edit_slice,'String')) ; %islice is the slice for the roi?
labstr = get(handles.edit_roilabel,'String') ; %labstr is label
UD.roislice(nroi+1) = islice ;
UD.roislice(nroi+2) = islice ;
UD.hroi{nroi+1} = hroi_1 ; %UD.hroi{} is a list of polygons
UD.hroi{nroi+2} = hroi_2 ;
UD.roilabel{nroi+1} = strcat(labstr,' Left');
UD.roilabel{nroi+2} = strcat(labstr, ' Right') ;
set(UDcont.hf,'UserData',UD)
update_listbox(handles, nroi+1)
%------------------------------------------
function [hroi,hroi2] = draw_imroi(handles, varargin)
if length(varargin)==3
roitype = varargin{3} ;
vin{1} = varargin{1};
vin{2} = varargin{2};
else
lval = get(handles.popupmenu_roitype,'Value') ;
lstr = get(handles.popupmenu_roitype,'String') ;
roitype = lstr{lval} ;
vin = varargin ;
end
switch roitype
case 'impoly'
hroi = impoly(vin{:},'Closed',false) ;
setColor(hroi,'red');
posn = getPosition(hroi) ;
if size(posn,1) < 3
warning(['Less than 3 points in impoly'])
delete(hroi)
hroi = draw_imroi(handles, varargin{:}) ;
end
get(handles.edit5)
width=handles.edit5.Value
get(handles.edit8)
widthinner=handles.edit8.Value
im=imgcf
im=double(im)
polypoints=hroi.getPosition
delete(hroi)
%Get wide and narrow polygons
[xy1wide,xy2wide]=create2poly(polypoints,width,im);
[xy1narrow,xy2narrow]=create2poly(polypoints,widthinner,im);
%Create left and right polygons
xyL=[xy1wide; flipud(xy1narrow)];
xyR=[xy2wide; flipud(xy2narrow)];
hroi=impoly(gca,xyL,'Closed',true);
hroi2=impoly(gca,xyR,'Closed',true);
setColor(hroi,'red');
setColor(hroi2,'red');
set(hroi,'Tag','impoly')
set(hroi2,'Tag','impoly')
end
function hroi = draw_normroi(handles, varargin)
if length(varargin)==3
roitype = varargin{3} ;
vin{1} = varargin{1};
vin{2} = varargin{2};
else
lval = get(handles.popupmenu_roitype,'Value') ;
lstr = get(handles.popupmenu_roitype,'String') ;
roitype = lstr{lval} ;
vin = varargin ;
end
hroi = impoly(vin{:}) ;
posn = getPosition(hroi) ;
if size(posn,1) < 3
warning(['Less than 3 points in impoly'])
delete(hroi)
hroi = draw_normroi(handles, varargin{:}) ;
end
set(hroi,'Tag','normroi')
%------------------------------------------
function update_listbox(handles, varargin)
if length(varargin) > 0
roi_selected = varargin{1} ;
else
roi_selected = 0 ;
end
UDcont = get(handles.figure1,'UserData') ;
UD = get(UDcont.hf,'UserData') ;
if ~isfield(UD,'hroi')
lstr{1} = 'ROI information';
lbval = 1 ;
else
hrois = [UD.hroi] ;
ilstr = 0 ;
lbline = 0 ;
lbval = [] ;
lstr = {'ROI information'} ;
nroi = length(hrois);
for iroi = 1: nroi
if ~isempty(hrois{iroi})
ilstr = ilstr+1;
sl = UD.roislice(iroi) ;
labstr = UD.roilabel{iroi};
roitype = get(hrois{iroi},'Tag') ;
switch roitype
case 'impoly'
BW = createMask(hrois{iroi},UD.him(sl));
stats = regionprops(BW,UD.vol(:,:,sl), ...
'Area','MeanIntensity','MaxIntensity','MinIntensity') ;
tempim=double(UD.vol(:,:,sl));
masked=BW.*tempim;
masked(masked==0)=NaN;
values=(masked(~isnan(masked)));
p95 = prctile(values,95);
dstr = [ '. IMPOLY. Area ',num2str(stats.Area), ...
'. Mean ',num2str(stats.MeanIntensity), ...
', Min ',num2str(stats.MinIntensity), ...
', Max ',num2str(stats.MaxIntensity), ...
', P95 ',num2str(p95)];
case 'normroi'
BW = createMask(hrois{iroi},UD.him(sl));
stats = regionprops(BW,UD.vol(:,:,sl), ...
'Area','MeanIntensity','MaxIntensity','MinIntensity') ;
tempim=double(UD.vol(:,:,sl));
masked=BW.*tempim;
masked(masked==0)=NaN;
values=(masked(~isnan(masked)));
p95 = prctile(values,95);
dstr = [ '. IMPOLY. Area ',num2str(stats.Area), ...
'. Mean ',num2str(stats.MeanIntensity), ...
', Min ',num2str(stats.MinIntensity), ...
', Max ',num2str(stats.MaxIntensity), ...
', P95 ',num2str(p95)];
%Set p95 for use by histogram button
get(handles.edit9);
handles.edit9.Value=p95;
otherwise
warning(['Unknown imroi type'])
end
lstr{ilstr} = [num2str(iroi,'%04u'), ...
'. Slice ',num2str(sl,'%04u'), ...
'. <',labstr,'>', ...
dstr ] ;
if ismember(iroi,roi_selected)
lbval = [ lbval ilstr] ;
end
end
end
end
set(handles.listbox_rois,'String',lstr)
set(handles.listbox_rois,'Value',lbval)
% --- Executes on button press in pushbutton_roidelete.
function pushbutton_roidelete_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_roidelete (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% removes "deleted" from list (doesn't actually delete object)
rois = get_selected_rois(handles) ;
UDcont = get(handles.figure1,'UserData') ;
UD = get(UDcont.hf,'UserData') ;
hrois = UD.hroi ;
for iroi = 1:length(rois)
set(hrois{rois(iroi)},'Visible','off')
hrois{rois(iroi)} = [] ;
end
UD.hroi = hrois ;
set(UDcont.hf,'UserData',UD) ;
update_listbox(handles)
% --- Executes on button press in pushbutton_roisave.
function pushbutton_roisave_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_roisave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%loop over all rois in listbox, save pos, XData, YData, vol
%size
UDcont = get(handles.figure1,'UserData') ;
UD = get(UDcont.hf,'UserData') ;
% update_listbox(handles)
lstr = get(handles.listbox_rois,'String') ;
% lval = [1:length(lstr)]; % fakes all selected in listbox
lval = get(handles.listbox_rois,'Value') ;
if length(lval)<length(lstr)
warndlg(['Selected ',num2str(length(lval)),' out of ',...
num2str(length(lstr)),' ROIs.'],'Warning on Save','replace')
end
nsel = length(lval) ;
rois = zeros([1 nsel]) ;
for isel = 1:nsel
sstr = lstr{lval(isel)} ;
rois(isel) = strread(sstr(1:4),'%u') ;
end
posc = cell([1 nsel]) ;
labstrc = cell([1 nsel]);
roitype = cell([1 nsel]);
slice = zeros([1 nsel]) ;
roistats = cell([1 nsel]);
BW = cell([1 nsel]) ;
hrois = [UD.hroi];
for isel = 1:nsel
pos = getPosition(UD.hroi{rois(isel)}) ;
posc{isel} = pos ;
labstrc{isel} = UD.roilabel(rois(isel)) ;
slice(isel) = UD.roislice(rois(isel)) ;
roitype{isel} = get(hrois{rois(isel)},'Tag') ;
roistats{isel} = calc_roistats(handles,roitype{isel}, pos, slice(isel)) ;
BW{isel} = createMask(hrois{rois(isel)}, UD.him(slice(isel)) ) ;
end
if ispref('roianal','save_dir')
defdir = getpref('roianal','save_dir');
else
defdir = [] ;
end
% in future could also save in OsiriX format
[fn,pn,FilterIndex] = uiputfile({'*.mat','MATLAB file (*.mat)'; ...
'*.txt','Text file (*.txt)' },'Enter filename',defdir) ;
if fn == 0
warning(['No file specified'])
return
end
if FilterIndex == 1 % *.mat
sz_vol = size(UD.vol) ;
XData = get(UD.him(1),'XData') ;
YData = get(UD.him(1),'YData') ;
save(fullfile(pn,fn),'posc','slice','sz_vol','XData','YData',...
'labstrc','roitype','roistats','BW') ;
elseif FilterIndex==2 % *.txt
[fid, msg] = fopen(fullfile(pn,fn),'w') ;
if fid == -1
error(['File opening error: ',msg])
end
fprintf(fid,'%s\n',datestr(now)) ;
for isel = 1:nsel
fprintf(fid,'%s Label: %s, Slice: %s Profile\n',roitype{isel}, ...
labstrc{isel}{1}, num2str(slice(isel)) ) ;
fprintf(fid,'Distance , Profile \n')
if ~isempty(roistats{isel})
prof = roistats{isel}.profile ;
dist = roistats{isel}.dist ;
for ip = 1:length(prof)
fprintf(fid,'%f , %f\n',dist(ip), prof(ip)) ;
end
end
fprintf(fid,'\n') ;
fprintf(fid,'%s Label: %s, Slice: %s Area\n',roitype{isel}, ...
labstrc{isel}{1}, num2str(slice(isel)) ) ;
fprintf(fid,'Threshold , Area \n')
if ~isempty(roistats{isel})
A = roistats{isel}.areas ;
ths = roistats{isel}.threshs ;
for ip = 1:length(A)
fprintf(fid,'%f , %f\n',ths(ip),A(ip)) ;
end
end
fprintf(fid,'\n') ;
end
fclose(fid) ;
disp(['Written to file: ',fullfile(pn,fn)])
else
warning('FilterIndex ??')
end
setpref('roianal','save_dir',pn)
% --- Executes on button press in pushbutton_roiload.
function pushbutton_roiload_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_roiload (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ispref('roianal','load_dir')
defdir = getpref('roianal','load_dir');
else
defdir = [] ;
end
% in future could also load in OsiriX format
[fn,pn,FilterIndex] = uigetfile({'*.mat','MATLAB mat file'},'Select ROI filename',defdir) ;
if fn == 0
warning(['No roi file specified'])
return
end
setpref('roianal','load_dir',pn) ;
UDcont = get(handles.figure1,'UserData') ;
hf = UDcont.hf ;
UD = get(hf,'UserData') ;
sz_vol = size(UD.vol) ;
XData = get(UD.him(1),'XData') ;
YData = get(UD.him(1),'YData') ;
S = load(fullfile(pn,fn)) ;
if ~isequal(S.sz_vol,sz_vol)
warning(['Input ROIs created on different sized volume'])
if S.sz_vol(3) ~= sz_vol(3)
return
end
end
if ~isequal(S.XData,XData)
warning(['Input ROIs created on differently scaled image in X'])
end
if ~isequal(S.YData,YData)
warning(['Input ROIs created on differently scaled image in Y'])
end
currsl = str2num(get(handles.edit_slice,'String')) ;
if isfield(UD,'hroi')
hrois = [UD.hroi] ;
nrois = length(hrois) ;
else
nrois = 0 ;
end
nroi_load = length(S.posc) ;
for iroi = 1:nroi_load
h_this = draw_imroi(handles,UD.ha, S.posc{iroi}, S.roitype{iroi}) ;
if S.slice(iroi) == currsl
set(h_this,'Visible','on')
else
set(h_this,'Visible','off')
end
nrois = nrois + 1 ;
UD.hroi{nrois} = h_this ;
UD.roislice(nrois) = S.slice(iroi) ;
UD.roilabel(nrois) = S.labstrc{iroi} ;
end
set(hf,'UserData',UD) ;
draw_slice(handles)
update_listbox(handles)
% --- Executes on button press in pushbutton_histogram.
function pushbutton_histogram_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_histogram (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rois = get_selected_rois(handles) ;
nroi = length(rois) ;
if nroi == 0
disp(['No ROIs selected'])
return
end
UDcont = get(handles.figure1,'UserData') ;
UD = get(UDcont.hf,'UserData') ;
hrois = [UD.hroi] ;
pv = [] ;
pv2 = [];
for iroi = 1: nroi
roi_this = rois(iroi) ;
if ~isempty(hrois{roi_this})
slice_this = UD.roislice(roi_this) ;
BW = createMask(hrois{roi_this},UD.him(slice_this)) ;
%Get stats for vol 1
stats = regionprops(BW,UD.vol(:,:,slice_this), ...
'PixelValues') ;
statinf=stats.PixelValues;
pv = [pv ; statinf] ;
%Get stats for vol 2
stats2 = regionprops(BW,UD.vol2(:,:,slice_this), ...
'PixelValues') ;
pv2 = [pv2 ; stats2.PixelValues] ;
else
warning(['Empty ROI: ', num2str(roi_this)])
end
end
%Calculate key FF values
FFmean=round(nanmean(pv),2);
FF10=round(prctile(pv,10),2);
FF25=round(prctile(pv,25),2);
FFmedian=round(prctile(pv,50),2);
FF75=round(prctile(pv,75),2);
FF90=round(prctile(pv,90),2);
pvmax=round(max(pv),2);
%Percentage above thresholds
thresh1=69.7;
totalpixels=numel(pv);
highFFpixels=sum(pv>thresh1);
highFFperc=highFFpixels/totalpixels;
thresh2=28.2;
lowFFpixels=sum(pv<thresh2);
lowFFperc=lowFFpixels/totalpixels;
%Calculate key R2* values
R2mean=round(nanmean(pv2),3);
R2_10=round(prctile(pv2,10),3);
R2_25=round(prctile(pv2,25),3);
R2median=round(prctile(pv2,50),3);
R2_75=round(prctile(pv2,75),3);
R2_90=round(prctile(pv2,90),3);
R2max=round(max(pv2),3);
%Create matrix with data for easy export
HistData(1,1)=FFmean;
HistData(1,2)=FFmedian;
HistData(1,3)=FF10;
HistData(1,4)=FF25;
HistData(1,5)=FF75;
HistData(1,6)=FF90;
HistData(1,7)=highFFperc;
HistData(1,8)=lowFFperc;
HistData(1,9)=R2mean;
HistData(1,10)=R2median;
HistData(1,11)=R2_10;
HistData(1,12)=R2_25;
HistData(1,13)=R2_75;
HistData(1,14)=R2_90;
HistData=HistData.'
%Create column names
Param={'FFmean';'FFmedian';'FF10';'FF25';'FF75';'FF90';'HighFF';'LowFF';'R2mean';'R2median';'R2_10';'R2_25';'R2_75';'R2_90'};
T=table(HistData,'RowNames',Param)
%Calculate fixed free diffusion fraction
%diffthresh=957;
%totalpixels=numel(pv);
%abovethreshpixels=sum(pv>diffthresh); %957 is defined threshold for free diffusion fraction
%FDFfixed=abovethreshpixels/totalpixels;
%Calculate variable free diffusion fraction (above P95 normal)
%get(handles.edit9);
%N95=handles.edit9.Value;
%abovethreshpixels2=sum(pv>N95);
%FDFvar=abovethreshpixels2/totalpixels;
%%%% CREATE FIGURES
figure ('Name',['Histogram ROI # ',num2str(rois)], 'Tag', 'roianal_histogram')
% (1) FF histogram
subplot(2,2,1)
%Specific bin widths
xbins=-10:5:110;
hist(double(pv),xbins, 'FontSize', 14)
xlim([0 100]);
hhist = gca ;
hold on
title(['Fat Fraction Histogram'])
xlabel(['FF'])
ylabel(['Number of voxels'])
set(gca,'FontSize',16);
%Plot key values
yl=ylim;
plot([FFmedian FFmedian],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %median
plot([FF10 FF10],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF10
plot([FF25 FF25],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF25
plot([FF75 FF75],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF75
plot([FF90 FF90],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF90
%title([' Mean= ',num2str(meanpv), ' Median = ', num2str(pvmedian), ' IQR = ', num2str(IQR), ' P95 = ',num2str(pv95th), ' Max = ',num2str(pvmax)])
%loc = find(pv > level) ;
%xlabel(['Fixedthresh = ',num2str(diffthresh), 'Varthresh = ',num2str(N95), ' FDFfixed = ',num2str(FDFfixed), ' FDFvar = ',num2str(FDFvar)])
% (2) R2* histogram
subplot(2,2,2)
%Specific bin widths
xbins2=0:0.5:10;
hist(double(pv2),xbins2)
xlim([0 10]);
hhist = gca ;
hold on
title(['R2star Histogram'])
xlabel(['R2star'])
ylabel(['Number of voxels'])
set(gca,'FontSize',16);
%Plot key values
yl=ylim;
plot([R2median R2median],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %median
plot([R2_10 R2_10],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF10
plot([R2_25 R2_25],[yl(1) yl(2)],'LineWidth',2,'color','red','Linestyle','--') %FF25