Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,317 @@
1
+%  Imbed Zoom, Interp, and Info menu to view_nii window.
2
+%
3
+%  Usage: view_nii_menu(fig);
4
+%
5
+
6
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
7
+%
8
+%--------------------------------------------------------------------
9
+function menu_hdl = view_nii_menu(fig, varargin)
10
+
11
+   if isnumeric(fig)
12
+      menu_hdl = init(fig);
13
+      return;
14
+   end
15
+
16
+   menu_hdl = [];
17
+
18
+   switch fig
19
+   case 'interp'
20
+      if nargin > 1
21
+         fig = varargin{1};
22
+      else
23
+         fig = gcbf;
24
+      end
25
+
26
+      nii_menu = getappdata(fig, 'nii_menu');
27
+      interp_on_state = get(nii_menu.Minterp,'Userdata');
28
+
29
+      if (interp_on_state == 1)
30
+         opt.useinterp = 1;
31
+         view_nii(fig,opt);
32
+         set(nii_menu.Minterp,'Userdata',0,'Label','Interp off');
33
+         reset_zoom(fig);
34
+      else
35
+         opt.useinterp = 0;
36
+         view_nii(fig,opt);
37
+         set(nii_menu.Minterp,'Userdata',1,'Label','Interp on');
38
+         reset_zoom(fig);
39
+      end
40
+   case 'reset_zoom'
41
+      if nargin > 1
42
+         fig = varargin{1};
43
+      else
44
+         fig = gcbf;
45
+      end
46
+
47
+      reset_zoom(fig);
48
+   case 'orient'
49
+      orient;
50
+   case 'img_info'
51
+      img_info;
52
+   case 'save_disp'
53
+      save_disp;
54
+   end
55
+
56
+   return					% view_nii_menu
57
+
58
+
59
+%--------------------------------------------------------------------
60
+function menu_hdl = init(fig)
61
+
62
+   %  search for edit, view menu
63
+   %
64
+   nii_menu.Mfile = [];
65
+   nii_menu.Medit = [];
66
+   nii_menu.Mview = [];
67
+   menuitems = findobj(fig, 'type', 'uimenu');
68
+
69
+   for i=1:length(menuitems)
70
+      filelabel = get(menuitems(i),'label');
71
+
72
+      if strcmpi(strrep(filelabel, '&', ''), 'file')
73
+         nii_menu.Mfile = menuitems(i);
74
+      end
75
+
76
+      editlabel = get(menuitems(i),'label');
77
+
78
+      if strcmpi(strrep(editlabel, '&', ''), 'edit')
79
+         nii_menu.Medit = menuitems(i);
80
+      end
81
+
82
+      viewlabel = get(menuitems(i),'label');
83
+
84
+      if strcmpi(strrep(viewlabel, '&', ''), 'view')
85
+         nii_menu.Mview = menuitems(i);
86
+      end
87
+   end
88
+
89
+   set(fig, 'menubar', 'none');
90
+
91
+   if isempty(nii_menu.Mfile)
92
+      nii_menu.Mfile = uimenu('Parent',fig, ...
93
+   	   'Label','File');
94
+
95
+      nii_menu.Mfile_save = uimenu('Parent',nii_menu.Mfile, ...
96
+   	   'Label','Save displayed image as ...', ...
97
+           'Callback','view_nii_menu(''save_disp'');');
98
+   else
99
+      nii_menu.Mfile_save = uimenu('Parent',nii_menu.Mfile, ...
100
+   	   'Label','Save displayed image as ...', ...
101
+           'separator','on', ...
102
+           'Callback','view_nii_menu(''save_disp'');');
103
+   end
104
+
105
+   if isempty(nii_menu.Medit)
106
+      nii_menu.Medit = uimenu('Parent',fig, ...
107
+   	   'Label','Edit');
108
+
109
+      nii_menu.Medit_orient = uimenu('Parent',nii_menu.Medit, ...
110
+   	   'Label','Convert to RAS orientation', ...
111
+           'Callback','view_nii_menu(''orient'');');
112
+   else
113
+      nii_menu.Medit_orient = uimenu('Parent',nii_menu.Medit, ...
114
+   	   'Label','Convert to RAS orientation', ...
115
+           'separator','on', ...
116
+           'Callback','view_nii_menu(''orient'');');
117
+   end
118
+
119
+   if isempty(nii_menu.Mview)
120
+      nii_menu.Mview = uimenu('Parent',fig, ...
121
+   	   'Label','View');
122
+
123
+      nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ...
124
+   	   'Label','Image Information', ...
125
+           'Callback','view_nii_menu(''img_info'');');
126
+   else
127
+      nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ...
128
+   	   'Label','Image Information', ...
129
+           'separator','on', ...
130
+           'Callback','view_nii_menu(''img_info'');');
131
+   end
132
+
133
+   nii_menu.Mzoom = rri_zoom_menu(fig);
134
+
135
+   nii_menu.Minterp = uimenu('Parent',fig, ...
136
+   	   'Label','Interp on', ...
137
+	   'Userdata', 1, ...
138
+           'Callback','view_nii_menu(''interp'');');
139
+
140
+   setappdata(fig,'nii_menu',nii_menu);
141
+   menu_hdl = nii_menu.Minterp;
142
+
143
+   return					% init
144
+
145
+
146
+%----------------------------------------------------------------
147
+function reset_zoom(fig)
148
+
149
+   old_handle_vis = get(fig, 'HandleVisibility');
150
+   set(fig, 'HandleVisibility', 'on');
151
+
152
+   nii_view = getappdata(fig, 'nii_view');
153
+   nii_menu = getappdata(fig, 'nii_menu');
154
+
155
+   set(nii_menu.Mzoom,'Userdata',1,'Label','Zoom on');
156
+   set(fig,'pointer','arrow');
157
+   zoom off;
158
+
159
+   axes(nii_view.handles.axial_axes);
160
+   setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ...
161
+			[get(gca, 'xlim') get(gca, 'ylim')])
162
+%   zoom reset;
163
+ %  zoom getlimits;
164
+   zoom out;
165
+
166
+   axes(nii_view.handles.coronal_axes);
167
+   setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ...
168
+			[get(gca, 'xlim') get(gca, 'ylim')])
169
+%   zoom reset;
170
+ %  zoom getlimits;
171
+   zoom out;
172
+
173
+   axes(nii_view.handles.sagittal_axes);
174
+   setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ...
175
+			[get(gca, 'xlim') get(gca, 'ylim')])
176
+%   zoom reset;
177
+ %  zoom getlimits;
178
+   zoom out;
179
+
180
+   set(fig, 'HandleVisibility', old_handle_vis);
181
+
182
+   return;					% reset_zoom
183
+
184
+
185
+%----------------------------------------------------------------
186
+function img_info
187
+
188
+   nii_view = getappdata(gcbf, 'nii_view');
189
+   hdr = nii_view.nii.hdr;
190
+
191
+   max_value = num2str(double(max(nii_view.nii.img(:))));
192
+   min_value = num2str(double(min(nii_view.nii.img(:))));
193
+
194
+   dim = sprintf('%d  %d  %d', double(hdr.dime.dim(2:4)));
195
+   vox = sprintf('%.3f  %.3f  %.3f', double(hdr.dime.pixdim(2:4)));
196
+
197
+   if double(hdr.dime.datatype) == 1
198
+      type = '1-bit binary';
199
+   elseif double(hdr.dime.datatype) == 2
200
+      type = '8-bit unsigned integer';
201
+   elseif double(hdr.dime.datatype) == 4
202
+      type = '16-bit signed integer';
203
+   elseif double(hdr.dime.datatype) == 8
204
+      type = '32-bit signed integer';
205
+   elseif double(hdr.dime.datatype) == 16
206
+      type = '32-bit single float';
207
+   elseif double(hdr.dime.datatype) == 64
208
+      type = '64-bit double precision';
209
+   elseif double(hdr.dime.datatype) == 128
210
+      type = '24-bit RGB true color';
211
+   elseif double(hdr.dime.datatype) == 256
212
+      type = '8-bit signed integer';
213
+   elseif double(hdr.dime.datatype) == 511
214
+      type = '96-bit RGB true color';
215
+   elseif double(hdr.dime.datatype) == 512
216
+      type = '16-bit unsigned integer';
217
+   elseif double(hdr.dime.datatype) == 768
218
+      type = '32-bit unsigned integer';
219
+   elseif double(hdr.dime.datatype) == 1024
220
+      type = '64-bit signed integer';
221
+   elseif double(hdr.dime.datatype) == 1280
222
+      type = '64-bit unsigned integer';
223
+   end
224
+
225
+   msg = {};
226
+   msg = [msg {''}];
227
+   msg = [msg {['Dimension:  [', dim, ']']}];
228
+   msg = [msg {''}];
229
+   msg = [msg {['Voxel Size:  [', vox, ']']}];
230
+   msg = [msg {''}];
231
+   msg = [msg {['Data Type:  [', type, ']']}];
232
+   msg = [msg {''}];
233
+   msg = [msg {['Max Value:  [', max_value, ']']}];
234
+   msg = [msg {''}];
235
+   msg = [msg {['Min Value:  [', min_value, ']']}];
236
+   msg = [msg {''}];
237
+
238
+   if isfield(nii_view.nii, 'fileprefix')
239
+      if isfield(nii_view.nii, 'filetype') & nii_view.nii.filetype == 2
240
+         msg = [msg {['File Name:  [', nii_view.nii.fileprefix, '.nii]']}];
241
+         msg = [msg {''}];
242
+      elseif isfield(nii_view.nii, 'filetype')
243
+         msg = [msg {['File Name:  [', nii_view.nii.fileprefix, '.img]']}];
244
+         msg = [msg {''}];
245
+      else
246
+         msg = [msg {['File Prefix:  [', nii_view.nii.fileprefix, ']']}];
247
+         msg = [msg {''}];
248
+      end
249
+   end
250
+
251
+   h = msgbox(msg, 'Image Information', 'modal');
252
+   set(h,'color',[1 1 1]);
253
+
254
+   return;					% img_info
255
+
256
+
257
+%----------------------------------------------------------------
258
+function orient
259
+
260
+   fig = gcbf;
261
+   nii_view = getappdata(fig, 'nii_view');
262
+   nii = nii_view.nii;
263
+
264
+   old_pointer = get(fig,'Pointer');
265
+   set(fig,'Pointer','watch');
266
+
267
+   [nii orient] = rri_orient(nii);
268
+
269
+   if isequal(orient, [1 2 3])		% do nothing
270
+      set(fig,'Pointer',old_pointer);
271
+      return;
272
+   end
273
+
274
+   oldopt = view_nii(fig);
275
+   opt.setarea = oldopt.area;
276
+   opt.usecolorbar = oldopt.usecolorbar;
277
+   opt.usecrosshair = oldopt.usecrosshair;
278
+   opt.usestretch = oldopt.usestretch;
279
+   opt.useimagesc = oldopt.useimagesc;
280
+   opt.useinterp = oldopt.useinterp;
281
+   opt.command = 'updatenii';
282
+
283
+   view_nii(fig, nii, opt);
284
+   set(fig,'Pointer',old_pointer);
285
+   reset_zoom(fig);
286
+
287
+   return;					% orient
288
+
289
+
290
+%----------------------------------------------------------------
291
+function save_disp
292
+
293
+   [filename pathname] = uiputfile('*.*', 'Save displayed image as (*.nii or *.img)');
294
+
295
+   if isequal(filename,0) | isequal(pathname,0)
296
+      return;
297
+   else
298
+      out_imgfile = fullfile(pathname, filename);	% original image file
299
+   end
300
+
301
+   old_pointer = get(gcbf,'Pointer');
302
+   set(gcbf,'Pointer','watch');
303
+
304
+   nii_view = getappdata(gcbf, 'nii_view');
305
+   nii = nii_view.nii;
306
+
307
+   try
308
+      save_nii(nii, out_imgfile);
309
+   catch
310
+      msg = 'File can not be saved.';
311
+      msgbox(msg, 'File write error', 'modal');
312
+   end
313
+
314
+   set(gcbf,'Pointer',old_pointer);
315
+
316
+   return;					% save_disp
317
+