-
Notifications
You must be signed in to change notification settings - Fork 7
/
Segmentation.m
1207 lines (935 loc) · 43.9 KB
/
Segmentation.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 = Segmentation(varargin)
% SEGMENTATION M-file for Segmentation.fig
% SEGMENTATION, by itself, creates a new SEGMENTATION or raises the existing
% singleton*.
%
% H = SEGMENTATION returns the handle to a new SEGMENTATION or the handle to
% the existing singleton*.
%
% SEGMENTATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SEGMENTATION.M with the given input arguments.
%
% SEGMENTATION('Property','Value',...) creates a new SEGMENTATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before segmentation_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to segmentation_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
%
% Author: Zeynep Akalin Acar, SCCN, 2008
% Copyright (C) 2007 Zeynep Akalin Acar, SCCN, [email protected]
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% Edit the above text to modify the response to help segmentation
% Last Modified by GUIDE v2.5 01-Nov-2011 07:48:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @segmentation_OpeningFcn, ...
'gui_OutputFcn', @segmentation_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
% ---------- Display Function -----------
function view_slice_Display(handles)
% This function displays handles.volume
vol = handles.volume;
% XXX mask = mask.*filteredvol;
if get(handles.MRradiobutton, 'Value') == 1
vol = handles.volume;
elseif get(handles.Scalpradiobutton, 'Value') == 1
% vol = handles.segm.scalpmask .* handles.filteredvol;
vol = handles.segm.scalpmask;
elseif get(handles.Filteredradiobutton, 'Value') ==1
vol = handles.filteredvol;
elseif get(handles.Brainradiobutton, 'Value') == 1
% vol = handles.segm.brainmask .* handles.filteredvol;
vol = handles.segm.brainmask;
elseif get(handles.OSradiobutton, 'Value') == 1
% vol = handles.segm.outerskullmask .* handles.filteredvol;
vol = handles.segm.outerskullmask;
elseif get(handles.ISradiobutton, 'Value') == 1
% vol = handles.segm.innerskullmask .* handles.filteredvol;
vol = handles.segm.innerskullmask;
end
sz = size(vol);
pos1 = ceil(get(handles.slider1,'Value'));
pos2 = ceil(get(handles.slider2,'Value'));
pos3 = ceil(get(handles.slider3,'Value'));
pos_val = sprintf('( %d, %d, %d )', pos3, pos1, pos2);
set(handles.text_xyz,'String',pos_val);
v1 = reshape(vol(pos1,:,:), sz(2), sz(3));
v2 = reshape(vol(:,pos2,:), sz(1), sz(3));
v3 = vol(:,:,pos3);
b1 = get(handles.axes1, 'ButtonDownFcn');
b2 = get(handles.axes2, 'ButtonDownFcn');
b3 = get(handles.axes3, 'ButtonDownFcn');
col = [0 1 0]; % green
imagesc(v1, 'Parent',handles.axes1); colormap pink;
set(handles.axes1,'YDir','normal'); % new 12/16/2008
line([pos3 pos3], [1 sz(2)],'Parent',handles.axes1, 'color', col);
line([1 sz(3)], [pos2 pos2],'Parent',handles.axes1, 'color', col);
imagesc(v2,'Parent',handles.axes2); colormap pink;
line([pos3 pos3], [1 sz(1)],'Parent',handles.axes2, 'color', col);
line([1 sz(3)], [pos1 pos1],'Parent',handles.axes2, 'color', col);
imagesc(v3,'Parent',handles.axes3); colormap pink;
line([1 sz(2)], [pos1 pos1], 'Parent',handles.axes3, 'color', col);
line([pos2 pos2], [1 sz(1)], 'Parent',handles.axes3, 'color', col);
set(handles.axes1, 'ButtonDownFcn', b1);
set(get(handles.axes1,'Children'), 'ButtonDownFcn',b1);
set(handles.axes2, 'ButtonDownFcn', b2);
set(get(handles.axes2,'Children'), 'ButtonDownFcn',b2);
set(handles.axes3, 'ButtonDownFcn', b3);
set(get(handles.axes3,'Children'), 'ButtonDownFcn',b3);
set(handles.text33,'String','y');
set(handles.text32,'String','x');
set(handles.text35,'String','z');
set(handles.text37,'String','y');
set(handles.text36,'String','x');
set(handles.text34,'String','z');
% --- Executes just before segmentation is made visible.
function segmentation_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 segmentation (see VARARGIN)
% Parse arguments and set handles as necessary
for i = 1:length(varargin)
if strcmp(varargin{i}, 'subjectdir')
i = i + 1;
handles.OutputFolder = varargin{i};
elseif strcmp(varargin{i}, 'subject')
i = i + 1;
handles.arg_subject = varargin{i};
elseif strcmp(varargin{i}, 'session')
i = i + 1;
handles.arg_session = varargin{i};
end
end
% Choose default command line output for segmentation
handles.output = hObject;
guidata(hObject, handles);
if isfield(handles,'OutputFolder')
set(handles.textFolder, 'String', handles.OutputFolder);
end
%if strcmp(get(handles.figure1,'Visible'),'off')
% view_slice_Display(handles);
%end
function handles = segmentation_SetVolume(handles, volume)
% Sets a volume for display
handles.volume = volume;
sz = size(handles.volume);
pos = ceil(sz/2);
set(handles.slider1, 'Max', sz(1));
set(handles.slider1, 'SliderStep', [1/sz(1) 1/sz(1)]);
set(handles.slider2, 'Max', sz(2));
set(handles.slider2, 'SliderStep', [1/sz(2) 1/sz(2)]);
set(handles.slider3, 'Max', sz(3));
set(handles.slider3, 'SliderStep', [1/sz(3) 1/sz(3)]);
set(handles.slider1,'Value', pos(1));
set(handles.slider2,'Value', pos(2));
set(handles.slider3,'Value', pos(3));
% set segmented volumes to 0
handles.segm.scalpmask = [];
handles.segm.brainmask = [];
handles.segm.outerskullmask = [];
handles.segm.innerskullmask = [];
handles.filteredvol = [];
% initialize current operation
handles.CurrentOperation = 1;
setup_operation(handles);
% Update handles structure
guidata(handles.figure1, handles);
update_view_selector(handles);
function update_view_selector(handles)
if not(isempty(handles.volume))
set(handles.MRradiobutton, 'Enable', 'on')
else
set(handles.MRradiobutton, 'Enable', 'off')
end
if not(isempty(handles.filteredvol))
set(handles.Filteredradiobutton, 'Enable', 'on')
set(handles.pushbuttonSaveFiltered, 'Enable', 'on')
else
set(handles.Filteredradiobutton, 'Enable', 'off')
set(handles.pushbuttonSaveFiltered, 'Enable', 'off')
end
if not(isempty(handles.segm.scalpmask))
set(handles.Scalpradiobutton, 'Enable', 'on')
set(handles.pushbuttonSaveSegm, 'Enable', 'on')
else
set(handles.Scalpradiobutton, 'Enable', 'off')
set(handles.pushbuttonSaveSegm, 'Enable', 'off')
end
if not(isempty(handles.segm.brainmask))
set(handles.Brainradiobutton, 'Enable', 'on')
else
set(handles.Brainradiobutton, 'Enable', 'off')
end
if not(isempty(handles.segm.outerskullmask))
set(handles.OSradiobutton, 'Enable', 'on')
else
set(handles.OSradiobutton, 'Enable', 'off')
end
if not(isempty(handles.segm.innerskullmask))
set(handles.ISradiobutton, 'Enable', 'on')
else
set(handles.ISradiobutton, 'Enable', 'off')
end
% --- Outputs from this function are returned to the command line.
function varargout = segmentation_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;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
cla;
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
case 1
plot(rand(5));
case 2
plot(sin(1:0.01:25.99));
case 3
bar(1:.5:10);
case 4
plot(membrane);
case 5
surf(peaks);
end
% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject handle to FileMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to OpenMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file, path] = uigetfile('*.hdr');
if ~isequal(file, 0) && length(file) > 5
handles.data.filename = file(1:length(file)-4);
handles.data.filepath = path;
handles.parameters.MRfile = file;
handles.parameters.MRpath = path;
% remove .hdr extension
file = [path file(1:length(file)-4)];
[x,y,z] = segm_readanalyze(file);
[K,L,M]=size(x);
if K==L && L==M && M==K && y(1)==1 && y(2)==1 && y(3)==1
else
error_msg = ['Please run Freesurfer pre-processing on the MR image: \n',...
'https://sccn.ucsd.edu/wiki/Chapter_02:_Head_Modeling_from_MR_Images'];
error( 'u:stuffed:it' , error_msg)
end
set(handles.pbinhomog,'Enable','on');
if get(handles.checkboxLRflip, 'Value') == 1
% flip image left-right (image is flipped during MR acquisition)
% image is saggital
[K,L,M]=size(x); x1=zeros(K,L,M);
for i=1:M
x1(:,:,i)=x(:,:,M-i+1);
end
x=x1; clear x1;
end
handles.parameters.LRflip = get(handles.checkboxLRflip,'Value');
handles = segmentation_SetVolume(handles, x);
view_slice_Display(handles);
end
% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to PrintMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)
% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to CloseMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
['Close ' get(handles.figure1,'Name') '...'],...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(handles.figure1)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'bar(1:.5:10)', 'plot(membrane)', 'surf(peaks)'});
% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes2
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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
view_slice_Display(handles);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (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
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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
view_slice_Display(handles);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (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
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject handle to slider3 (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
view_slice_Display(handles);
% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider3 (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
% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=ceil(get(handles.axes1, 'CurrentPoint'));
%set(handles.slider1,'Value', pos(1));
set(handles.slider2,'Value', x(1,2));
set(handles.slider3,'Value', x(1,1));
view_slice_Display(handles);
% --- Executes on mouse press over axes background.
function axes2_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=ceil(get(handles.axes2, 'CurrentPoint'));
%set(handles.slider1,'Value', pos(1));
set(handles.slider1,'Value', x(1,2));
set(handles.slider3,'Value', x(1,1));
view_slice_Display(handles);
% --- Executes on mouse press over axes background.
function axes3_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=ceil(get(handles.axes3, 'CurrentPoint'));
%set(handles.slider1,'Value', pos(1));
set(handles.slider1,'Value', x(1,2));
set(handles.slider2,'Value', x(1,1));
view_slice_Display(handles);
% --- Executes during object creation, after setting all properties.
function text1_CreateFcn(hObject, eventdata, handles)
% hObject handle to text1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject handle to checkbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox3
% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject handle to checkbox4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox4
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Runbutton.
function Runbutton_Callback(hObject, eventdata, handles)
% hObject handle to Runbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.CurrentOperation == 1
iter = str2num(get(handles.editNumberofIter, 'String'));
% ts = str2num(get(handles.editTimeStep, 'String'));
ts = 0.0625;
cond = str2num(get(handles.editConductance, 'String'));
handles.parameters.filter.iter = iter;
handles.parameters.filter.cond = cond;
set(handles.textStatus,'String','Filtering...'); pause(1);
handles.filteredvol = segm_aniso_filtering(handles.volume, iter, ts, cond);
set(handles.textStatus,'String','Image is filtered!'); pause(0.1);
elseif handles.CurrentOperation == 2
% scalp
set(handles.textStatus,'String','Segmenting scalp...'); pause(1);
handles.segm.scalpmask = segm_scalp(handles.filteredvol);
set(handles.textStatus,'String','Scalp segmented!')
elseif handles.CurrentOperation == 3
% brain
sli = str2num(get(handles.editSlice, 'String'));
WMp(1) = str2num(get(handles.editWMp1, 'String'));
WMp(2) = str2num(get(handles.editWMp2, 'String'));
WMp(3) = str2num(get(handles.editWMp3, 'String'));
sl = str2num(get(handles.editSetLevel, 'String'));
st = str2num(get(handles.editSetThres, 'String'));
handles.parameters.brain.slice = sli;
handles.parameters.brain.WMp = WMp;
handles.parameters.brain.filllevel = sl;
handles.parameters.brain.threshold = st;
set(handles.textStatus,'String','Segmenting brain...'); pause(1);
handles.segm.brainmask = segm_brain(handles.filteredvol,handles.segm.scalpmask, sli, WMp, sl, st);
set(handles.textStatus,'String','Brain segmented!')
elseif handles.CurrentOperation == 4
% outer skull
sli_eyes = str2num(get(handles.editSliceForEyes, 'String'));
handles.parameters.skull.sli_eyes = sli_eyes;
set(handles.textStatus,'String','Segmenting skull...'); pause(1);
[handles.segm.outerskullmask, handles.X_dark, thr] = segm_outer_skull(handles.filteredvol, handles.segm.scalpmask, handles.segm.brainmask, sli_eyes);
handles.parameters.skull.thr = thr; % 1/29/2013
set(handles.textStatus,'String','Skull segmented!')
elseif handles.CurrentOperation == 5
% inner skull
set(handles.textStatus,'String','Segmenting CSF...'); pause(1);
WMp(1) = str2num(get(handles.editWMp1, 'String'));
WMp(2) = str2num(get(handles.editWMp2, 'String'));
WMp(3) = str2num(get(handles.editWMp3, 'String'));
handles.segm.innerskullmask = segm_inner_skull(handles.filteredvol, handles.segm.outerskullmask, handles.X_dark, handles.segm.brainmask,WMp);
set(handles.textStatus,'String','Correcting skull and scalp...'); pause(1);
[handles.segm.scalpmask, handles.segm.outerskullmask]=segm_final_skull(handles.segm.scalpmask, handles.segm.outerskullmask, handles.segm.innerskullmask, WMp);
set(handles.textStatus,'String','Segmentation complete!'); pause(1);
end
% Enable and select Next button
set(handles.Nextbutton, 'Enable', 'on');
uicontrol(handles.Nextbutton);
% Update handles structure
guidata(handles.figure1, handles);
update_view_selector(handles);
function setup_operation(handles)
% setup current operation
col_on = [1 0 0];
col_off = [0 0 0.322];
set(handles.Prevbutton, 'Enable', 'on');
set(handles.Nextbutton, 'Enable', 'off');
if handles.CurrentOperation == 1
set(handles.Prevbutton, 'Enable', 'off');
set(handles.Filttext2, 'ForegroundColor',col_on);
set(handles.Runbutton, 'ForegroundColor',col_on);
set(handles.Runbutton, 'TooltipString','Run anisotropic filtering');
else
set(handles.Filttext2, 'ForegroundColor',col_off);
end
if handles.CurrentOperation == 2
set(handles.Scalptext2, 'ForegroundColor',col_on);
set(handles.Runbutton, 'TooltipString','Run scalp segmentation');
else
set(handles.Scalptext2, 'ForegroundColor',col_off);
end
if handles.CurrentOperation == 3
set(handles.Braintext2, 'ForegroundColor',col_on);
set(handles.Runbutton, 'TooltipString','Run brain segmentation');
else
set(handles.Braintext2, 'ForegroundColor',col_off);
end
if handles.CurrentOperation == 4
set(handles.OStext2, 'ForegroundColor',col_on);
set(handles.Runbutton, 'TooltipString','Run outer skull segmentation');
else
set(handles.OStext2, 'ForegroundColor',col_off);
end
if handles.CurrentOperation == 5
set(handles.IStext2, 'ForegroundColor',col_on);
set(handles.Runbutton, 'TooltipString','Run inner skull segmentation');
else
set(handles.IStext2, 'ForegroundColor',col_off);
end
uicontrol(handles.Runbutton);
% --------------------------------------------------------------------
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to uipanel3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get(handles.uipanel3);
view_slice_Display(handles);
% --- Executes on button press in Prevbutton.
function Prevbutton_Callback(hObject, eventdata, handles)
% hObject handle to Prevbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.CurrentOperation > 1
handles.CurrentOperation = handles.CurrentOperation - 1;
setup_operation(handles);
% Update handles structure
guidata(handles.figure1, handles);
end
% --- Executes on button press in Nextbutton.
function Nextbutton_Callback(hObject, eventdata, handles)
% hObject handle to Nextbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.CurrentOperation < 5
handles.CurrentOperation = handles.CurrentOperation + 1;
setup_operation(handles);
% Update handles structure
guidata(handles.figure1, handles);
end
function editNumberofIter_Callback(hObject, eventdata, handles)
% hObject handle to editNumberofIter (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 editNumberofIter as text
% str2double(get(hObject,'String')) returns contents of editNumberofIter as a double
% --- Executes during object creation, after setting all properties.
function editNumberofIter_CreateFcn(hObject, eventdata, handles)
% hObject handle to editNumberofIter (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
function editTimeStep_Callback(hObject, eventdata, handles)
% hObject handle to editTimeStep (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 editTimeStep as text
% str2double(get(hObject,'String')) returns contents of editTimeStep as a double
% --- Executes during object creation, after setting all properties.
function editTimeStep_CreateFcn(hObject, eventdata, handles)
% hObject handle to editTimeStep (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
function editConductance_Callback(hObject, eventdata, handles)
% hObject handle to editConductance (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 editConductance as text
% str2double(get(hObject,'String')) returns contents of editConductance as a double
% --- Executes during object creation, after setting all properties.
function editConductance_CreateFcn(hObject, eventdata, handles)
% hObject handle to editConductance (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
function editWMp1_Callback(hObject, eventdata, handles)
% hObject handle to editWMp1 (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 editWMp1 as text
% str2double(get(hObject,'String')) returns contents of editWMp1 as a double
% --- Executes during object creation, after setting all properties.
function editWMp1_CreateFcn(hObject, eventdata, handles)
% hObject handle to editWMp1 (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
function editWMp2_Callback(hObject, eventdata, handles)
% hObject handle to editWMp2 (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 editWMp2 as text
% str2double(get(hObject,'String')) returns contents of editWMp2 as a double
% --- Executes during object creation, after setting all properties.
function editWMp2_CreateFcn(hObject, eventdata, handles)
% hObject handle to editWMp2 (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
function editWMp3_Callback(hObject, eventdata, handles)
% hObject handle to editWMp3 (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 editWMp3 as text
% str2double(get(hObject,'String')) returns contents of editWMp3 as a double
% --- Executes during object creation, after setting all properties.
function editWMp3_CreateFcn(hObject, eventdata, handles)
% hObject handle to editWMp3 (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
function editSlice_Callback(hObject, eventdata, handles)
% hObject handle to editSlice (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 editSlice as text
% str2double(get(hObject,'String')) returns contents of editSlice as a double
% --- Executes during object creation, after setting all properties.
function editSlice_CreateFcn(hObject, eventdata, handles)
% hObject handle to editSlice (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 pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbuttonGetSlice.
function pushbuttonGetSlice_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonGetSlice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos2 = ceil(get(handles.slider2,'Value'));
set(handles.editSlice, 'String', num2str(pos2));
% --- Executes on button press in pushbuttonGetWMp.
function pushbuttonGetWMp_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonGetWMp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos1 = ceil(get(handles.slider1,'Value'));
pos2 = ceil(get(handles.slider2,'Value'));
pos3 = ceil(get(handles.slider3,'Value'));
set(handles.editWMp1, 'String', num2str(pos1));
set(handles.editWMp2, 'String', num2str(pos2));
set(handles.editWMp3, 'String', num2str(pos3));
function editSliceForEyes_Callback(hObject, eventdata, handles)
% hObject handle to editSliceForEyes (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 editSliceForEyes as text
% str2double(get(hObject,'String')) returns contents of editSliceForEyes as a double
% --- Executes during object creation, after setting all properties.
function editSliceForEyes_CreateFcn(hObject, eventdata, handles)
% hObject handle to editSliceForEyes (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 pushbuttonGetEyeSlice.
function pushbuttonGetEyeSlice_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonGetEyeSlice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos2 = ceil(get(handles.slider2,'Value'));
set(handles.editSliceForEyes, 'String', num2str(pos2));
% --- Executes on button press in pushbuttonSaveFiltered.
function pushbuttonSaveFiltered_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonSaveFiltered (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'arg_subject')
if isempty(handles.arg_subject)
f = handles.data.filename; % if subject name is not given save the output
else % with MR image's name.
f = handles.arg_subject;
end
else
f = handles.data.filename;
end
set(handles.textStatus,'String',['Saving filtered image as ' f '_filtered.mat']); pause(0.5);
filt_im = handles.filteredvol;
% convert to axial (from saggital) before saving!
ax = filt_im; clear filt_im
[K,L,M] = size(ax);
for i = 1:M
filt_im(:,i,:) = (reshape(ax(:,:,i), K, L));
end
[K,L,M] = size(filt_im);
for i = 1:M
filt_im(:,:,i) = rot90(filt_im(:,:,i),3);
end
OutputFolder = handles.OutputFolder;
if isempty(OutputFolder)
error('You must enter output folder...')
end
lof = length(OutputFolder);
if OutputFolder(lof) ~= '/'
OutputFolder(lof+1) = '/';
end
p = OutputFolder;
%save([p f '_filtered.mat'],'filt_im');
filt_im = filt_im/max(max(max(filt_im)));
[K,L,M] = size(filt_im);
mri.dim = [K L M];
mri.xgrid = [1:K];
mri.ygrid = [1:L];
mri.zgrid = [1:M];
mri.anatomy = filt_im;
mri.transform = eye(4);
mri.hdr = handles.parameters.MRfile;
save([p f '_mri'],'mri')
clear filt_im ax;
set(handles.textStatus,'String',['Filtered image saved as ' f '_filtered.mat'])
% --- Executes on button press in pushbuttonSaveSegm.
function pushbuttonSaveSegm_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonSaveSegm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'arg_subject')
if isempty(handles.arg_subject)
f = handles.data.filename; % if subject name is not given save the output
else % with MR image's name.