-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MRIqual.m
3499 lines (3442 loc) · 177 KB
/
MRIqual.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 MRIqual
% MRIqual % A GUI for converting DICOM's, running visual checks on
% structural & functional neuroimaging data, and checking various quality
% measures such as tSNR, SFNR,
% (file types: .nii, .nii.gz, .img/.hdr), checking temporal SNR (tSNR) of
% functional images, average power within resting-state frequencies,
% ghost ratio of structural images, visualization of local tSNR or SNR,
% functional ROI correlation checking, frequency spectrum visualization.
% Various options for statistics output include .csv, .txt, & command line.
%
% Author: Elliot Layden, The University of Chicago, 2016-2019
%
% Usage:
% To begin, simply type "MRIqual" into the command line.
% Alternatively, simply right-click on the MRIqual.m
% script, and choose Run.
%
% FEATURES:
%
% I. DICOM Conversion
%
% II. View/Edit 3D Image
% This functionality calls neuroimage_editor, a major external function
% used throughout the modules below. It enables manually scanning through
% 2D slices with arrow keys, zooming with the mouse scrollwheel, drawing
% ROIs via manual tracing or shapes (ovals,circles,rectangles) which can
% be propogated through slices, displaying statistical or ROI-based
% overlays, and many other functions. A more thorough description can be
% found within the neuroimage_editor.m file.
%
% III. Structural Quality
% Signal to Noise Ratio (SNR)
% 1. SNR_SD = mean voxel intensity in specified signal ROI divided by
% SD of voxel intensities within a noise (background) ROI,
% multiplied by a correction factor
% 2. SNR_mean = mean voxel intensity in specified signal ROI divided
% by mean of voxel intensities within a noise (background) ROI,
% multiplied by a correction factor
% *Reference:
% Dietrich, O., Raya, J. G., Reeder, S. B., Reiser, M. F., &
% Schoenberg, S. O. (2007). Measurement of signal?to?noise
% ratios in MR images: Influence of multichannel coils,
% parallel imaging, and reconstruction filters. Journal of
% Magnetic Resonance Imaging, 26(2), 375-385. (see A11 & A12)
% Visualize local SNR
%
% IV. Functional Quality
%
% 1. Temporal Signal-to-Noise Ratio (tSNR)
% = mean voxel signal across time divided by SD across time
% *References:
% Triantafyllou, C., Hoge, R. D., Krueger, G., Wiggins,
% C. J., Potthast, A., Wiggins, G. C., & Wald, L. L. (2005).
% Comparison of physiological noise at 1.5 T, 3 T and 7 T
% and optimization of fMRI acquisition parameters.
% Neuroimage, 26(1), 243-250.
% Murphy, K., Bodurka, J., & Bandettini, P. A. (2007).
% How long to scan? The relationship between fMRI temporal
% signal to noise ratio and necessary scan duration.
% Neuroimage, 34(2), 565-574.
% <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2223273/>
%
% 2. Temporal Signal-to-Background Noise Ratio (tSBNR)
% = spatial average across a signal ROI of temporal mean voxel
% signals / spatial average across temporal standard deviation of
% noise/background ROI voxels
% -whereas tSNR divides mean signal by the temporal SD of the same
% voxels, this measure compares the mean signal to the average
% temporal SD of background/noise voxels, which may provide a more
% accurate measure of temporal fluctuations due specifically to noise
%
% 3. Signal-to-Fluctuation-Noise Ratio (SFNR)
% = mean voxel signal across time divided by temporal standard deviation
% of residuals obtained by fitting second-order polynomial to voxel
% signal data
% *Reference:
% Friedman, L., & Glover, G. H. (2006). Report on a multicenter
% fMRI quality assurance protocol. Journal of Magnetic
% Resonance Imaging, 23(6), 827-839.
%
% 4. Signal-to-Noise Ratio (SNR-functional)
% = mean voxel signal of signal ROI (averaged across time) divided by
% the spatial SD of a noise ROI signal (averaged across time),
% multiplied by a correction factor
% Reference:
% http://wagerlab.colorado.edu/wiki/doku.php/help/fmri_quality_control_overview
%
% 5. Calculate average power within a given frequency range
%
% Visualize local tSNR, SFNR, local power, & power spectrum
%
% Power Spectrum & Power Spectral Density Estimation
% MRIqual implements two methods for obtaining these
% quantities:
% 1. Welch's periodogram is used to calculate the power spectrum,
% utilizing a Hamming window with 50% overlap. Specifically, time
% courses are by default divided into the longest segments possible
% so as to yield close to, but not exceeding, 8 segments w/ 50%
% overlap. Power spectral density estimates are then scaled by the
% equivalent noise bandwidth (ENBW) of the window (in hertz). To
% obtain the ENBW using Matlab, e.g., "enbw(hamming(N), fs)", where
% fs = sampling frequency, N = # of samples.
% 2. The Multitaper method (Matlab: pmtm.m) is used to calculate the
% power spectral density estimate using default parameters. The power
% spectrum, scaling by ENBW, is not easily obtainable by this method.
%
% Miscellaneous
% *Determine ROIs using rectangular selection tool, manual tracing,
% loading a mask image, or specifying an intensity threshold.
% *ROIs may be propogated through slices or through slices and time
% *Generate Report: Calculates all metrics for either a functional or
% structural image, and saves output as a text file with accompanying
% .png images.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialize GUI Window & Data:
% Identify Function Path and Add Helper Scripts:
script_fullpath = mfilename('fullpath');
[script_path,~,~] = fileparts(script_fullpath);
addpath(genpath(script_path)) % adds dicm2nii, neuroimage_editor, etc.
% Check for Dependencies: neuroimage_editor, dicm2nii, load_untouch_nii
if ~exist('neuroimage_editor_tbx.m','file')
errordlg('ERROR: neuroimage_editor_tbx.m not found. Image viewing functionality disabled.',...
'Missing Dependency','modal');
end
if ~exist('nifti_studio.m','file')
errordlg('ERROR: nifti_studio.m not found. Image viewing functionality disabled.',...
'Missing Dependency','modal');
end
if ~exist('dicm2nii.m','file')
errordlg('ERROR: dicm2nii.m not found. DICOM conversion disabled.',...
'Missing Dependency','modal')
end
if ~exist('load_untouch_nii.m','file')
errordlg('ERROR: NIFTI_Tools not found. Image loading disabled.',...
'Missing Dependency','modal')
end
% Initialize Figure (& auto-detect screen size):
screen_res = get(0, 'MonitorPositions'); % get(0,'ScreenSize');
figure_pos = [0.36948, 0.2541, 0.25, 0.55];
if any(figure_pos<=0) % avoid ScreenSize errors
figure_pos = [342,50,700,680];
screen_res = [1,1,(figure_pos(3:4)./[.5,.86])];
figure_pos = [.37*screen_res(3), .255*screen_res(4), ...
.25*screen_res(3), .55*screen_res(4)];
end
main_figure = figure('units','normalized','Position',[0.36948, 0.2541, 0.25, 0.55],'MenuBar','none',...
'Name','MRIqual','NumberTitle','off','Color',ones(1,3),...
'Visible','on','doublebuffer','on','CloseRequestFcn',@mainfig_closereq);
% Try loading background image:
try
background = imread(fullfile(script_path,'res','background_brain.png'));
imagesc(background(:,:,1)); colormap('gray'); caxis([0,260]);
set(gca,'Position',[0,0,1,1],'TickLength',[0,0],'XTickMode','manual',...
'YTickMode','manual','XTick',[],'YTick',[]); grid off;
catch
end
% Intialize GUI Data:
guidata1 = guidata(main_figure);
guidata1.script_path = script_path;
guidata1.screen_res = screen_res;
guidata1.main_figure = main_figure;
guidata1.figure_pos = figure_pos;
guidata1.background_color = [.8,.88,.98];
guidata1.extensions={'*.img';'*.nii';'*nii.gz'};
guidata1.apply_header = 0;
guidata1.viewer_figure = 28.1773; % initialize false handle
guidata1.font_color = [.873,.546,.347]; % copper
% Directory Memory Initialization:
guidata1.first_dir = pwd;
guidata1.last_dicom_dir = pwd;
guidata1.last_output_dir = pwd;
guidata1.last_struct_dir = pwd;
guidata1.last_funct_dir = pwd;
guidata1.funct_path = [];
% Main Figures Handle Initialization:
guidata1.dicom_figure = 1.48437;
guidata1.structurals_figure = 2.48482;
guidata1.funct_figure = 3.484727;
guidata1.h = 10.18384; % non-handle for view image
numvox_total = [];
all_sig = [];
all_detrended = [];
guidata1.SFNR_all = [];
guidata1.signal_amp_all = [];
% Structural Specific Initialization:
guidata1.struct_name = '';
guidata1.struct_name_string = '';
guidata1.structurals_state = 0;
guidata1.struct = [];
guidata1.struct_ROI = [];
guidata1.struct_mean_signal = [];
guidata1.struct_ROI_type = '';
guidata1.struct_SNR_sd = [];
guidata1.struct_SNR_sd_local = [];
guidata1.struct_SNR_mean = [];
guidata1.struct_SNR_mean_local = [];
guidata1.struct_report_options = ones(1,5);
guidata(main_figure,guidata1)
% Functional Initialization
funct_initialize
%% Initalize Main Buttons:
% Create I. "Convert DICOMs" Button:
guidata1.dicom_button = uicontrol(guidata1.main_figure,'style',...
'togglebutton','Units','normalized','Position',[.275,.78,.45,.1],...
'BackgroundColor', [0,0,0],'FontName','Segoe UI Black','FontSize',14,'FontWeight','normal','String',...
'Convert DICOMs','callback',@dicoms_button_callback,'ForegroundColor',guidata1.font_color);
% Create II. "View/Edit 3D Images" Button:
guidata1.viewEdit_button = uicontrol(guidata1.main_figure,'style',...
'togglebutton','Units','normalized','Position',[.275,.56,.45,.1],...
'BackgroundColor', [0,0,0],'FontName','Segoe UI Black','FontSize',14,'FontWeight','normal','String',...
'View/Edit 3D Images','callback',@viewEdit_button_callback,'ForegroundColor',guidata1.font_color);
% Create III. "Structural Quality" Button:
guidata1.structurals_button = uicontrol(guidata1.main_figure,'style',...
'togglebutton','Units','normalized','Position',[.275,.34,.45,.1],...
'BackgroundColor', [0,0,0],'FontName','Segoe UI Black','FontSize',14,'FontWeight','normal','String',...
'Structural Quality','callback',@structurals_button_callback,'ForegroundColor',guidata1.font_color);
% Create IV. "Functional Quality" Button:
guidata1.functionals_button = uicontrol(guidata1.main_figure,'style',...
'togglebutton','Units','normalized','Position',[.275,.12,.45,.1],...
'BackgroundColor', [0,0,0],'FontName','Segoe UI Black','FontSize',14,'FontWeight','normal','String',...
'Functional Quality','callback',@functionals_button_callback,'ForegroundColor',guidata1.font_color);
guidata(main_figure,guidata1)
function mainfig_closereq(hObject, eventdata, overlay_num) %#ok
selection = questdlg('Are you sure you want to exit?','Exit MRIqual',...
'Yes','No','Yes');
switch selection
case 'Yes'
guidata1 = guidata(main_figure);
if isgraphics(guidata1.dicom_figure)
delete(guidata1.dicom_figure)
end
if isgraphics(guidata1.structurals_figure)
delete(guidata1.structurals_figure)
end
if isgraphics(guidata1.funct_figure)
delete(guidata1.funct_figure)
end
if isgraphics(guidata1.main_figure)
delete(guidata1.main_figure)
end
case 'No'
return;
end
end
function funct_initialize
% Retrieve Data
guidata1 = guidata(main_figure);
guidata1.numvoxel_limit = 500; % limit for plotting power spectrum, signals
guidata1.chunk_num = 50000; % number of voxels to chunk by
guidata1.funct_name = '';
guidata1.funct_name_string = '';
guidata1.functionals_state = 0;
guidata1.funct = []; % 4D
guidata1.funct3D = []; % 3D after temporal averaging
guidata1.numvox = 0;
guidata1.funct_dim = [];
guidata1.multi_select = false;
guidata1.n_4D = [];
guidata1.funct_mean_signal = [];
guidata1.funct_ROI_type = [];
guidata1.ROI_ind = [];
guidata1.opts = false(1,5);
guidata1.ax1 = 28.3928;
guidata1.ROI_x = [];
guidata1.ROI_y = [];
guidata1.ROI_z = [];
guidata1.ROI_ind_back = [];
guidata1.roi_sig = [];
guidata1.roi_background = [];
guidata1.roi_sig_filt = [];
guidata1.sig_detrended = [];
guidata1.background_detrended = [];
guidata1.roi_background_filt = [];
guidata1.mean_signal_amp = [];
guidata1.h_plots = [34.1242,33.8974,12.2349,13.3593,14.4859,65.6456];
guidata1.legend_str = {'Signal (Mean Centered)','Detrended (Linear)',...
'Detrended (Quadratic)','Linear Trend','Quadratic Trend',...
'Band-Pass Filtered'};
guidata1.Fs = [];
guidata1.HighPass = [];
guidata1.LowPass = [];
guidata1.report_dir = [];
guidata1.funct_tSNR = [];
guidata1.funct_tSNR_local = [];
guidata1.tSBNR = [];
guidata1.funct_tSBNR = [];
guidata1.funct_SFNR = [];
guidata1.use_linear = [];
guidata1.funct_SFNR_local = [];
guidata1.funct_SNR_sd = [];
guidata1.funct_SNR_sd_local = [];
guidata1.funct_power = [];
guidata1.funct_power_local = [];
guidata1.mean_background_amp = [];
guidata1.funct_report_options = ones(1,7);
guidata(main_figure,guidata1)
end
%% I. CONVERT DICOMS
function dicoms_button_callback(hObject, eventdata, overlay_num) %#ok
% Retrieve Data
guidata1 = guidata(main_figure);
% Determine State
guidata1.dicoms_state = get(hObject,'Value');
if guidata1.dicoms_state
set(guidata1.viewEdit_button,'Value',false)
set(guidata1.structurals_button,'Value',false);
set(guidata1.functionals_button,'Value',false);
if isgraphics(guidata1.viewer_figure)
delete(guidata1.viewer_figure)
end
if isgraphics(guidata1.structurals_figure)
delete(guidata1.structurals_figure)
end
if isgraphics(guidata1.funct_figure)
delete(guidata1.funct_figure)
end
else
if isgraphics(guidata1.dicom_figure)
delete(guidata1.dicom_figure)
end
return;
end
% Generate Dicom Figure
% figure_pos1 = [.225*guidata1.screen_res(3), .45*guidata1.screen_res(4),...
% .55*guidata1.screen_res(3), .18*guidata1.screen_res(4)];
guidata1.dicom_figure = figure('units','normalized','Position',[.2245,.4491,.55,.18],'MenuBar','none',...
'Name','Convert DICOMs','NumberTitle','off','Color',[0,0,0],...
'Visible','on','doublebuffer','on','CloseRequestFcn',@dicom_closereq);
% Create "Select DICOMs Folder(s)" Button:
guidata1.select_dicoms_button = uicontrol(guidata1.dicom_figure,'style',...
'pushbutton','Units','normalized','Position',[.01,.65,.21,.23],...
'BackgroundColor', [0,0,0],'ForegroundColor',guidata1.font_color,...
'FontSize',10,'FontWeight','bold','String',...
'Select DICOM Folder(s)','callback',@select_dicoms_callback);
% Create "Select Output Folder(s)" Text Box:
guidata1.select_dicoms_text = uicontrol(guidata1.dicom_figure,'style',...
'edit','Units','normalized','Position',[.23,.65,.75,.23],...
'BackgroundColor', [1,1,1]);
% Create "Select Output Folder(s)" Button:
guidata1.select_output_button = uicontrol(guidata1.dicom_figure,'style',...
'pushbutton','Units','normalized','Position',[.01,.35,.21,.23],...
'BackgroundColor', [0,0,0],'ForegroundColor',guidata1.font_color,'FontSize',10,'FontWeight','bold','String',...
'Select Output Folder(s)','callback',@select_output_callback);
% Create "Select Output Folder(s)" Text Box:
guidata1.select_output_text = uicontrol(guidata1.dicom_figure,'style',...
'edit','Units','normalized','Position',[.23,.35,.75,.23],...
'BackgroundColor', [1,1,1]);
% Create "File Type:" Text
uicontrol('Parent',guidata1.dicom_figure,'Style','text','String','File Type:',...
'Units', 'normalized','FontSize',10,'FontName','Helvetica','Position',...
[.02,.025,.1,.2],'BackgroundColor',[0,0,0],'ForegroundColor',guidata1.font_color,...
'FontWeight','Bold','HorizontalAlignment','left');
% Create File Type Selection Drop-Down:
guidata1.filetype_dropdown = uicontrol('Parent',guidata1.dicom_figure,...
'Style','popupmenu','String','.img/hdr|.nii|.nii.gz|3D.nii|3D.nii.gz',...
'Units', 'normalized','FontSize',9,'FontName','Helvetica','Position',...
[.12,.05,.1,.2],'BackgroundColor',[1,1,1],'FontWeight','Bold',...
'HorizontalAlignment','center');
% Create "Output File Name:" Text
uicontrol('Parent',guidata1.dicom_figure,'Style','text','String','Output File Name:',...
'Units', 'normalized','FontSize',10,'FontName','Helvetica','Position',...
[.35,.025,.2,.2],'BackgroundColor',[0,0,0],'ForegroundColor',guidata1.font_color,...
'FontWeight','Bold','HorizontalAlignment','left');
% Create Output File Name Entry Box:
guidata1.output_name_text = uicontrol(guidata1.dicom_figure,'style',...
'edit','Units','normalized','Position',[.52,.08,.2,.18],...
'BackgroundColor', [1,1,1]);
% Create "Convert" Button:
guidata1.run_dicoms_button = uicontrol(guidata1.dicom_figure,'style',...
'pushbutton','Units','normalized','Position',[.85,.08,.13,.18],...
'BackgroundColor', guidata1.font_color,'ForegroundColor',[0,0,0] ,...
'FontSize',10,'FontWeight','bold','String',...
'Convert','callback',@run_dicoms_callback);
% Save Data
guidata(main_figure,guidata1)
end
function dicom_closereq(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
guidata1.dicom_button.Value=0;
if isgraphics(guidata1.dicom_figure)
delete(guidata1.dicom_figure)
end
guidata(main_figure,guidata1)
end
function select_dicoms_callback(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
[guidata1.dicom_dir] = uigetdir(guidata1.last_dicom_dir,'Select DICOM Folder:');
if guidata1.dicom_dir~=0
[guidata1.last_dicom_dir,~,~] = fileparts(guidata1.dicom_dir);
guidata1.select_dicoms_text.String = guidata1.dicom_dir;
end
guidata(main_figure,guidata1)
end
function select_output_callback(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
[guidata1.output_dir] = uigetdir(guidata1.last_output_dir,'Select Output Folder:');
if guidata1.output_dir~=0
[guidata1.last_output_dir,~,~] = fileparts(guidata1.output_dir);
guidata1.select_output_text.String = guidata1.output_dir;
end
guidata(main_figure,guidata1)
end
function run_dicoms_callback(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
if isdir(guidata1.select_dicoms_text.String) %#ok
if ~isdir(guidata1.select_output_text.String) %#ok
mkdir(guidata1.select_output_text.String)
end
switch guidata1.filetype_dropdown.Value
case 1
out_type = 2; % .img/hdr
case 2
out_type = 0; % .nii
case 3
out_type = 1; % .nii.gz
case 4
out_type = 4; % 3D.nii
case 5
out_type = 5; % 3D.nii.gz
end
dicm2nii(guidata1.select_dicoms_text.String, ...
guidata1.select_output_text.String, out_type)
% Rename if Output Name Specified:
S = load(fullfile(guidata1.select_output_text.String,'dcmHeaders.mat'));
field_names = fieldnames(S.h); h = S.h; %#ok
if ~isempty(guidata1.output_name_text.String)
% Rename dcmHeaders.mat
movefile(fullfile(guidata1.select_output_text.String,'dcmHeaders.mat'),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,'dcmHeaders.mat']))
% Rename Image Files:
listing = dir(fullfile(guidata1.select_output_text.String,[field_names{1},'*']));
[~,fname,ext] = fileparts(listing(1).name);
nseries = numel(listing);
if nseries<100
numzero = '%02g';
elseif nseries<1000
numzero = '%03g';
elseif nseries<10000
numzero = '%04g';
elseif nseries<100000
numzero = '%05g';
end
if out_type==2 % .img/.hdr
movefile(fullfile(guidata1.select_output_text.String,[fname,'.img']),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,'.img']))
movefile(fullfile(guidata1.select_output_text.String,[fname,'.hdr']),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,'.hdr']))
elseif out_type==0
movefile(fullfile(guidata1.select_output_text.String,[fname,ext]),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,ext]))
elseif out_type==1
movefile(fullfile(guidata1.select_output_text.String,[fname,ext]),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,'.nii.gz']))
elseif out_type==4
for ix = 1:nseries
movefile(fullfile(guidata1.select_output_text.String,listing(ix).name),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,sprintf(numzero,ix),ext]))
end
elseif out_type==5
for ix = 1:nseries
movefile(fullfile(guidata1.select_output_text.String,listing(ix).name),...
fullfile(guidata1.select_output_text.String,...
[guidata1.output_name_text.String,sprintf(numzero,ix),'.nii.gz']))
end
end
else
movefile(fullfile(guidata1.select_output_text.String,'dcmHeaders.mat'),...
fullfile(guidata1.select_output_text.String,...
[field_names{1},'_dcmHeaders.mat']))
end
else
errordlg('ERROR: One or more invalid directories. Respecify.')
end
guidata(main_figure,guidata1)
end
%% II. View/Edit 3D Image
% Simply call neuroimage_editor
function viewEdit_button_callback(hObject, ~)
% Retrieve Data
guidata1 = guidata(main_figure);
% Determine State
guidata1.viewEdit_state = get(hObject,'Value');
if isgraphics(guidata1.h); delete(guidata1.h); end
if guidata1.viewEdit_state
set(guidata1.dicom_button,'Value',false);
set(guidata1.structurals_button,'Value',false);
set(guidata1.functionals_button,'Value',false);
if isgraphics(guidata1.dicom_figure)
delete(guidata1.dicom_figure)
end
if isgraphics(guidata1.structurals_figure)
delete(guidata1.structurals_figure)
end
if isgraphics(guidata1.funct_figure)
delete(guidata1.funct_figure)
end
else
if isgraphics(guidata1.viewer_figure)
delete(guidata1.viewer_figure)
end
return;
end
guidata1.viewer_figure = nifti_studio;
if ~isempty(guidata1.viewer_figure.figure)
set(guidata1.viewer_figure.figure,'DeleteFcn',@closed_viewer_figure)
else
set(guidata1.viewEdit_button,'Value',false)
end
% Save Data
guidata(main_figure,guidata1)
end
% If Viewer/Editor is closed, Return Button Value to 'false'
function closed_viewer_figure(~,~)
guidata1 = guidata(main_figure);
guidata1.viewEdit_button.Value=false;
guidata(main_figure,guidata1)
end
%% III. STRUCTURALS
function structurals_button_callback(hObject, eventdata, overlay_num) %#ok
% Retrieve Data
guidata1 = guidata(main_figure);
% Determine State
guidata1.structurals_state = get(hObject,'Value');
if isgraphics(guidata1.h); delete(guidata1.h); end
if guidata1.structurals_state
set(guidata1.viewEdit_button,'Value',false)
set(guidata1.dicom_button,'Value',false);
set(guidata1.functionals_button,'Value',false);
if isgraphics(guidata1.viewer_figure)
delete(guidata1.viewer_figure)
end
if isgraphics(guidata1.dicom_figure)
delete(guidata1.dicom_figure)
end
if isgraphics(guidata1.funct_figure)
delete(guidata1.funct_figure)
end
else
if isgraphics(guidata1.structurals_figure)
delete(guidata1.structurals_figure)
end
return;
end
% Figure: Structurals
% figure_pos1 = [.27*guidata1.screen_res(3), .4*guidata1.screen_res(4), ...
% .46*guidata1.screen_res(3), .32*guidata1.screen_res(4)];
guidata1.structurals_figure = figure('units','normalized','Position',...
[.2695,.3991,.46,.32],'MenuBar','none','Name','Structural Quality',...
'NumberTitle','off','Color',[0,0,0],'Visible','on','doublebuffer',...
'on','CloseRequestFcn',@structurals_closereq);
% Button: "Select Structural"
guidata1.select_struct_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.01,.85,.2,.12],...
'BackgroundColor', [0,0,0],'ForegroundColor',[1,1,1],...
'FontSize',10,'FontWeight','bold','String',...
'Select Structural','callback',@select_struct_callback,'Interruptible','off');
% Input Box: Select Structural
guidata1.select_struct_text = uicontrol(guidata1.structurals_figure,'style',...
'edit','Units','normalized','Position',[.22,.85,.7,.12],...
'BackgroundColor', [1,1,1],'callback',@struct_input_box);
if ~isempty(guidata1.struct_name_string)
guidata1.select_struct_text.String = guidata1.struct_name_string;
end
% Button: "Load"
guidata1.load_struct_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.93,.85,.06,.12],...
'BackgroundColor', [0,0,0],'ForegroundColor',[1,1,1],...
'FontSize',10,'FontWeight','bold','String',...
'Load','callback',@load_struct_callback,'Interruptible','off');
% ButtonGroup: "ROIs" Section
guidata1.struct_rois_bg = uibuttongroup('Title','ROIs','Position',...
[.01,.02,.48,.8],'BackgroundColor',[0,0,0],'ForegroundColor',guidata1.font_color,...
'FontName','Helvetica','FontSize',11,'FontWeight','bold','TitlePosition',...
'centertop');
% Text: "Automatically specify..."
uicontrol('Parent',guidata1.structurals_figure,'Style','text','String',...
'Automatically specify ROIs based on...','Units','normalized',...
'FontSize',10,'FontName','Helvetica','Position',[.018,.63,.39,.08],...
'BackgroundColor',[0,0,0],'ForegroundColor',[1,1,1],'FontWeight','Bold',...
'HorizontalAlignment','left');
% Button: "Image Region"
guidata1.struct_ROI_auto_region_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.025,.49,.21,.12],...
'BackgroundColor',[0,0,0],'FontSize',10,'FontWeight','bold','String',...
'Image Region','ForegroundColor',guidata1.font_color,'callback',{@struct_ROI_spec,1},'Interruptible','off');
% Button: "Signal Intensity"
guidata1.struct_ROI_auto_intensity_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.265,.49,.21,.12],...
'BackgroundColor',[0,0,0],'FontSize',10,'FontWeight','bold','String',...
'Signal Intensity','ForegroundColor',guidata1.font_color,'callback',{@struct_ROI_spec,2},'Interruptible','off');
% Text: "Manually specify..."
uicontrol('Parent',guidata1.structurals_figure,'Style','text','String',...
'Manually specify ROIs based on...','Units','normalized',...
'FontSize',10,'FontName','Helvetica','Position',[.018,.36,.35,.08],...
'BackgroundColor',[0,0,0],'ForegroundColor',[1,1,1],'FontWeight','Bold',...
'HorizontalAlignment','left');
% Button: "Intensity Threshold"
guidata1.struct_ROI_manual_intensity_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.025,.22,.21,.12],...
'BackgroundColor',[0,0,0],'FontSize',10,'FontWeight','bold','String',...
'Intensity Threshold','ForegroundColor',guidata1.font_color,'callback',{@struct_ROI_spec,3},'Interruptible','off');
% Button: "Manual Tracing"
guidata1.struct_ROI_manual_trace_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.265,.22,.21,.12],...
'BackgroundColor',[0,0,0],'FontSize',10,'FontWeight','bold','String',...
'Manual Tracing','ForegroundColor',guidata1.font_color,'callback',{@struct_ROI_spec,4},'Interruptible','off');
% Button: "View Structural"
guidata1.struct_view_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.025,.06,.21,.12],...
'BackgroundColor',guidata1.font_color,'FontSize',10,'FontWeight','bold','String',...
'View Structural','ForegroundColor','k','callback',@struct_view_struct,'Interruptible','off');
% Button: "View ROIs"
guidata1.struct_view_ROIs_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.265,.06,.21,.12],...
'BackgroundColor',guidata1.font_color,'FontSize',10,'FontWeight','bold','String',...
'View ROIs','ForegroundColor','k','callback',@struct_view_ROIs,'Interruptible','off');
% ButtonGroup: "Calculate" Section
guidata1.struct_rois_bg = uibuttongroup('Title','Calculate','Position',...
[.51,.02,.48,.8],'BackgroundColor',[0,0,0],'FontName',...
'Helvetica','FontSize',11,'FontWeight','bold','TitlePosition',...
'centertop','ForegroundColor',guidata1.font_color);
% Button: "SNR (Noise SD)"
guidata1.struct_SNR_sd_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.525,.58,.21,.12],...
'BackgroundColor', [0,0,0],'FontSize',10,'FontWeight','bold','String',...
'SNR (Noise SD)','ForegroundColor',guidata1.font_color,'callback',{@struct_SNR_callback,1,1},'Interruptible','off');
% Button: "Display Image"
guidata1.struct_display_SNR_sd_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.765,.58,.21,.12],...
'BackgroundColor',guidata1.font_color,'FontSize',10,'FontWeight','bold','String',...
'Display Image','ForegroundColor','k','callback',{@struct_display_SNR,1},'Interruptible','off');
% Button: "SNR (Noise Mean)"
guidata1.struct_SNR_mean_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.525,.4,.21,.12],...
'BackgroundColor', [0,0,0],'FontSize',10,'FontWeight','bold','String',...
'SNR (Noise Mean)','ForegroundColor',guidata1.font_color,'callback',{@struct_SNR_callback,2,1},'Interruptible','off');
% Button: "Display Image"
guidata1.struct_display_SNR_mean_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.765,.4,.21,.12],...
'BackgroundColor',guidata1.font_color,'FontSize',10,'FontWeight','bold','String',...
'Display Image','ForegroundColor','k','callback',{@struct_display_SNR,2},'Interruptible','off');
% Button: "Generate Report:"
guidata1.struct_report_button = uicontrol(guidata1.structurals_figure,'style',...
'pushbutton','Units','normalized','Position',[.645,.24,.21,.12],...
'BackgroundColor', [0,0,0],'FontSize',10,'FontWeight','bold','String',...
'Generate Report:','ForegroundColor','w','callback',@struct_report,'Interruptible','off');
box_x = linspace(.57,.91,5);
% Checkbox 1: Stats.txt
guidata1.struct_box1 = uicontrol(guidata1.structurals_figure,'style',...
'checkbox','Units','normalized','Position',[box_x(1),.105,.05,.05],...
'BackgroundColor',[0,0,0],'FontSize',8,...
'FontWeight','Normal','ForegroundColor','w','Value',1);
% Checkbox 2: SNR img Mosaics
guidata1.struct_box2 = uicontrol(guidata1.structurals_figure,'style',...
'checkbox','Units','normalized','Position',[box_x(2),.105,.05,.05],...
'BackgroundColor',[0,0,0],'FontSize',8,...
'FontWeight','Normal','ForegroundColor','w','Value',1);
% Checkbox 3: ROIs img
guidata1.struct_box3 = uicontrol(guidata1.structurals_figure,'style',...
'checkbox','Units','normalized','Position',[box_x(3),.105,.05,.05],...
'BackgroundColor',[0,0,0],'FontSize',8,...
'FontWeight','Normal','ForegroundColor','w','Value',0);
% Checkbox 4: SNR (SD) img
guidata1.struct_box4 = uicontrol(guidata1.structurals_figure,'style',...
'checkbox','Units','normalized','Position',[box_x(4),.105,.05,.05],...
'BackgroundColor',[0,0,0],'FontSize',8,...
'FontWeight','Normal','ForegroundColor','w','Value',0);
% Checkbox 5: SNR (Mean) img
guidata1.struct_box5 = uicontrol(guidata1.structurals_figure,'style',...
'checkbox','Units','normalized','Position',[box_x(5),.105,.05,.05],...
'BackgroundColor',[0,0,0],'FontSize',8,...
'FontWeight','Normal','ForegroundColor','w','Value',0);
% Add Checkbox Labels:
box_labels = {'Stats.txt','SNR Mosaics','ROIs img','SNR (SD) img','SNR (Mean) img'};
box_pos = [.515,.05,.13,.06];
box_x_pos = linspace(.52,.855,5);
box_y_pos = [.16,.035,.16,.035,.16];
for ix = 1:5
box_pos(1) = box_x_pos(ix);
box_pos(2) = box_y_pos(ix);
uicontrol('Parent',guidata1.structurals_figure,'Style','text','String',...
box_labels{ix},'Units','normalized','FontSize',8,'FontName',...
'Helvetica','Position',box_pos,'BackgroundColor',...
[0,0,0],'ForegroundColor','w','FontWeight','Normal',...
'HorizontalAlignment','center');
end
% Save Data
guidata(main_figure,guidata1)
end
function select_struct_callback(~,~,~)
guidata1 = guidata(main_figure);
cd(guidata1.last_struct_dir)
[guidata1.struct_name, new_path] = uigetfile(guidata1.extensions,...
'Select Structural Image:','MultiSelect','off');
cd(guidata1.first_dir)
if guidata1.struct_name~=0
guidata1.last_struct_dir = new_path;
[~,~,ext] = fileparts(guidata1.struct_name);
for ix = 1:3
if ~isempty(strfind(guidata1.extensions{ix},ext))
type = ix;
others = setdiff(1:3,ix);
break;
end
end
guidata1.extensions = guidata1.extensions([type,others]);
if guidata1.last_struct_dir~=0
guidata1.select_struct_text.String = fullfile(guidata1.last_struct_dir,guidata1.struct_name);
guidata1.struct_name_string = guidata1.select_struct_text.String;
end
end
guidata(main_figure,guidata1)
end
function struct_input_box(~, ~, ~)
guidata1 = guidata(main_figure);
if ~strcmp(guidata1.select_struct_text.String,'')
guidata1.struct_name_string = guidata1.select_struct_text.String;
guidata(main_figure,guidata1)
end
end
function load_struct_callback(~, ~, ~)
guidata1 = guidata(main_figure);
if ~isempty(guidata1.struct_name_string)
if exist(guidata1.struct_name_string,'file')
try
guidata1.struct = load_nii(guidata1.struct_name_string);
disp('Structural image successfully loaded.')
guidata1.apply_header = 1;
catch
guidata1.struct = load_untouch_nii(guidata1.struct_name_string);
disp('Non-orthogonal shearing detected in affine matrix. Successfully loaded raw image data.')
guidata1.apply_header = 0;
end
guidata1.struct.img = single(guidata1.struct.img); % change data type for later functions
else
errordlg('Structural image not found. Please respecify.'); return;
end
else
errordlg('Please specify a structural image.'); return;
end
guidata(main_figure,guidata1)
end
function struct_ROI_spec(~, ~, ROI_type)
guidata1 = guidata(main_figure);
if ~isempty(guidata1.struct)
switch ROI_type
case 1 %'Image Region' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata1.struct_ROI_type = 'Automatic - Image Region';
disp('Automatically calculating ROI locations based on standard image regions.')
% Initialize struct_ROI:
guidata1.struct_dim = guidata1.struct.hdr.dime.dim(2:4);
guidata1.struct_ROI = guidata1.struct;
guidata1.struct_ROI.img = zeros(guidata1.struct_dim);
% Signal ROI:
center_x = round(guidata1.struct_dim(1)/2);
center_y = round(guidata1.struct_dim(2)/2);
center_z = round(guidata1.struct_dim(3)/2);
central_x = round(.1*guidata1.struct_dim(1));
central_y = round(.1*guidata1.struct_dim(2));
central_z = round(.15*guidata1.struct_dim(3));
guidata1.struct_ROI.img(center_x-central_x:center_x+central_x,...
center_y-central_y:center_y+central_y,center_z-central_z:center_z+central_z) = 1;
% Noise ROIs
border_x = round(.08*guidata1.struct_dim(1));
border_y = round(.75*guidata1.struct_dim(2));
guidata1.struct_ROI.img(3:border_x,3:border_y,2:guidata1.struct_dim(3)-1) = 2;
guidata1.struct_ROI.img(guidata1.struct_dim(1)-border_x:guidata1.struct_dim(1)-2,...
3:border_y,2:guidata1.struct_dim(3)-1) = 2;
case 2 %'Signal Intensity' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata1.struct_ROI_type = 'Automatic - Signal Intensity';
% disp('Automatically calculting ROI locations based on signal intensities.')
% Initialize struct_ROI:
guidata1.struct_dim = guidata1.struct.hdr.dime.dim(2:4);
guidata1.struct_ROI = guidata1.struct;
guidata1.struct_ROI.img = zeros(guidata1.struct_dim);
% Determine Thresholds:
intensities = sort(guidata1.struct.img(:));
sig_min_perc = .75; sig_max_perc = .995;
sig_min = intensities(round(sig_min_perc*length(intensities)));
sig_max = intensities(round(sig_max_perc*length(intensities)));
noise_perc = .7;
noise_thresh = intensities(round(noise_perc*length(intensities)));
% Signal ROI:
guidata1.struct_ROI.img(((guidata1.struct.img>=sig_min) + (guidata1.struct.img<=sig_max))==2) = 1;
% Noise ROI:
guidata1.struct_ROI.img(guidata1.struct.img<=noise_thresh) = 2;
fprintf(['ROIs: Intensity thresholds for signal ROI were ',...
'%.02f - %.02f. Maximum intensity for background ROI was %.02f.'],...
[sig_min,sig_max,noise_thresh])
case 3 %'Intensity Threshold' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata1.struct_ROI_type = 'Manual - Intensity Threshold';
% Initialize struct_ROI:
guidata1.struct_dim = guidata1.struct.hdr.dime.dim(2:4);
guidata1.struct_ROI = guidata1.struct;
guidata1.struct_ROI.img = zeros(guidata1.struct_dim);
max_sig = max(guidata1.struct.img(:));
% Specify thresholds for inclusive mask:
prompt = {sprintf('Enter intensity thresholds for SIGNAL ROI: \n\nMin:'),'Max:'};
dlg_title = 'Signal ROI'; num_lines = [1,50;1,50]; defaultans = {'0',num2str(max_sig)};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
if isempty(answer)
disp('User cancelled intensity threshold specification');
return;
end
sig_min = str2double(answer(1)); sig_max = str2double(answer(2));
prompt = {'Enter maximum intensity for BACKGROUND ROI: '};
dlg_title = 'Background ROI'; num_lines = [1,50;]; defaultans = {num2str(max_sig)};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
if isempty(answer)
disp('User cancelled intensity threshold specification');
return;
end
noise_thresh = str2double(answer(1));
% Signal ROI:
guidata1.struct_ROI.img(((guidata1.struct.img>=sig_min) + (guidata1.struct.img<=sig_max))==2) = 1;
% Noise ROI:
guidata1.struct_ROI.img(guidata1.struct.img<=noise_thresh) = 2;
disp('Calculting ROI locations based on specified signal intensities.')
case 4 %'Manual Tracing' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata1.struct_ROI_type = 'Manual - Tracing';
% if isgraphics(guidata1.h); close(guidata1.h); end
instructions_text = sprintf(['First, draw a SIGNAL ROI (green). ',...
'Second, draw a BACKGROUND/NOISE ROI (red). ',...
'To accomplish this, select "Draw" menu -> "Draw Color". ',...
'Once a color is selected, simply trace the outline of a shape on the image, ',...
'and this shape will automatically fill with color. Alternatively, select ',...
'"Draw" menu -> "Shapes" to draw using a specified shape. ',...
'Click and drag on the image to create shapes. To navigate between slices, ',...
'use the up and down arrow keys. An ROI drawn on one slice can be propagated through to other ',...
'slices by selecting "Draw" menu -> "Edit Drawing" -> "Propagate Through Slices" -> choose slice range. ',...
'Note that this will only apply to the most recent drawing. When you are finished drawing ',...
'select "Confirm ROIs" -> "Confirm". The ROIs will be automatically saved.']);
h_mbox = msgbox(instructions_text,'ROI Tracing Instructions');
uiwait(h_mbox,60)
[guidata1.h,~,drawing_idx] = neuroimage_editor_tbx('apply_header',...
guidata1.apply_header,'background',guidata1.struct,...
'colorbar',1,'title_on',1,'axis_tick_on',1); %#ok
uiwait(guidata1.h.figure,120) % wait until neuroimage_editor closes, or 60 sec
drawing_idx = evalin('base','drawing_idx'); % pull last_draw in from 'base' workspace
drawing_idx = permute(drawing_idx,[2,1,3]);
guidata1.struct_ROI = guidata1.struct;
guidata1.struct_ROI.img = zeros(guidata1.struct_dim);
guidata1.struct_ROI.img(drawing_idx(:)==2) = 1;
guidata1.struct_ROI.img(drawing_idx(:)==3) = 2;
end
if ROI_type~=4
guidata1.struct_mean_signal = nanmean(guidata1.struct.img(guidata1.struct_ROI.img(:)==1));
end
else
errordlg('Please load a structural image.'); return;
end
guidata(main_figure,guidata1)
end
function struct_view_struct(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
if isgraphics(guidata1.h); close(guidata1.h); end
if ~isempty(guidata1.struct)
handles = nifti_studio('apply_header',guidata1.apply_header,...
'background',guidata1.struct,'colormap','gray',...
'colorbar_on',1,'title_on',1,'axis_tick_on',1);
guidata1.h = handles.figure;
guidata(main_figure,guidata1)
else
errordlg('Please load a structural image.'); return;
end
end
function struct_view_ROIs(hObject, eventdata, overlay_num) %#ok
guidata1 = guidata(main_figure);
if isgraphics(guidata1.h); close(guidata1.h); end
if ~isempty(guidata1.struct)
if ~isempty(guidata1.struct_ROI)
handles = nifti_studio('apply_header',guidata1.apply_header,...
'background',guidata1.struct,'colormap','gray',...
'overlay',guidata1.struct_ROI,'colorbar_on',0,'title_on',0,'axis_tick_on',0);
guidata1.h = handles.figure;
guidata(main_figure,guidata1)
else
errordlg('Please Specify Signal & Noise ROIs.'); return;
end
else
errordlg('Please load a structural image.'); return;
end
end
function struct_SNR_callback(~, ~, SNR_type, verbose)
guidata1 = guidata(main_figure);
% Retrieve ROIs:
if ~isempty(guidata1.struct_ROI)
% if isgraphics(guidata1.h); close(guidata1.h); end
else
errordlg('ERROR: No ROIs found.'); return;
end
% Calculate SNR:
switch SNR_type
case 1 % SNR (SD)
% Calculate Stat:
noise_SD = nanstd(guidata1.struct.img(guidata1.struct_ROI.img(:)==2));
denominator = noise_SD; % sqrt(2/(4-pi))*noise_SD;
if noise_SD == 0
warning('The signal standard deviation within the noise ROI was 0. Has this image been thresholded or filtered?')
end
guidata1.struct_SNR_sd = guidata1.struct_mean_signal / denominator; % scalar
if verbose
disp(['Structural SNR (SD) = ',num2str(guidata1.struct_SNR_sd)])
end
% Calculate Local:
guidata1.struct_SNR_sd_local = guidata1.struct;
guidata1.struct_SNR_sd_local.img(guidata1.struct_ROI.img(:)==1) = guidata1.struct_SNR_sd_local.img(guidata1.struct_ROI.img(:)==1)./denominator;
guidata1.struct_SNR_sd_local.img(guidata1.struct_ROI.img(:)~=1) = 0;
% Calculate Global:
guidata1.struct_SNR_sd_global = guidata1.struct;
guidata1.struct_SNR_sd_global.img = guidata1.struct_SNR_sd_global.img./denominator;
case 2 % SNR (MEAN)
% Calculate Stat:
noise_mean = nanmean(guidata1.struct.img(guidata1.struct_ROI.img(:)==2));
denominator = noise_mean; % sqrt(2/pi)*noise_mean;
if noise_mean == 0
warning('The mean signal within the noise ROI was 0. Has this image been thresholded or filtered?')
end
guidata1.struct_SNR_mean = guidata1.struct_mean_signal / denominator;
if verbose
disp(['Structural SNR (Mean) = ',num2str(guidata1.struct_SNR_mean)])
end
% Calculate Local:
guidata1.struct_SNR_mean_local = guidata1.struct;
guidata1.struct_SNR_mean_local.img(guidata1.struct_ROI.img(:)==1) = guidata1.struct_SNR_mean_local.img(guidata1.struct_ROI.img(:)==1)./denominator;
guidata1.struct_SNR_mean_local.img(guidata1.struct_ROI.img(:)~=1) = 0;
% Calculate Global:
guidata1.struct_SNR_mean_global = guidata1.struct;
guidata1.struct_SNR_mean_global.img = guidata1.struct_SNR_mean_global.img./denominator;
end
guidata(main_figure,guidata1)
end
function struct_display_SNR(hObject, eventdata, SNR_type) %#ok
guidata1 = guidata(main_figure);
% if isgraphics(guidata1.h); close(guidata1.h); end
switch SNR_type
case 1 % SNR (SD)
if isempty(guidata1.struct_SNR_sd_local) || isempty(guidata1.struct_SNR_sd_global)
errordlg('ERROR: First, calculate SNR (Noise SD)'); return;
end
% Check whether to display signal ROI only or Full Image:
ans1 = 'Signal ROI'; ans2 = 'Full Image';
answer = questdlg('Display which?','Display',ans1,ans2,ans2);
switch answer
case ans1
handles = nifti_studio('apply_header',guidata1.apply_header,...
'background',guidata1.struct_SNR_sd_local,...
'colorbar_on',1,'title_on',1,'axis_tick_on',1,...
'colormap','jet','title','SNR (Noise SD)',...
'background_caxis',[min(guidata1.struct_SNR_sd_local.img(:)),max(guidata1.struct_SNR_sd_local.img(:))]);
guidata1.h = handles.figure;
case ans2
handles = nifti_studio('apply_header',guidata1.apply_header,...
'background',guidata1.struct,...
'overlay',guidata1.struct_SNR_sd_global,...
'colorbar_on',1,'title_on',1,'axis_tick_on',1,...
'colormap','jet','title','SNR (Noise SD)',...
'overlay_caxis',[0,max(guidata1.struct_SNR_sd_local.img(:))]);
guidata1.h = handles.figure;
end