-
Notifications
You must be signed in to change notification settings - Fork 98
/
ea_anatomyslices.m
203 lines (177 loc) · 6.73 KB
/
ea_anatomyslices.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
function ea_anatomyslices(resultfig,togglestates,options,controlhandles)
% input xyz in mm coordinates.
% this function plots an anatomical slice to the 3D viewer of lead-dbs.
% __________________________________________________________________________________
% Copyright (C) 2015 Charite University Medicine Berlin, Movement Disorders Unit
% Andreas Horn
if ~exist('controlhandles', 'var')
controlhandles = [];
end
set(0, 'currentfigure', resultfig); % for figures
atlases=getappdata(resultfig,'atlases');
togglestates.xyztransparencies=double(togglestates.xyztransparencies/100);
xsliceplot=getappdata(resultfig,'xsliceplot');
ysliceplot=getappdata(resultfig,'ysliceplot');
zsliceplot=getappdata(resultfig,'zsliceplot');
%% Parse togglestates
if ~isempty(xsliceplot) && ~togglestates.refreshcuts
switch togglestates.cutview
case 'xcut'
set(xsliceplot,'Visible','on')
set(ysliceplot,'Visible','off')
set(zsliceplot,'Visible','off')
ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
case 'ycut'
set(xsliceplot,'Visible','off')
set(ysliceplot,'Visible','on')
set(zsliceplot,'Visible','off')
ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
case 'zcut'
set(xsliceplot,'Visible','off')
set(ysliceplot,'Visible','off')
set(zsliceplot,'Visible','on')
ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
case '3d'
set(xsliceplot,'Visible','on')
set(ysliceplot,'Visible','on')
set(zsliceplot,'Visible','on')
ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
end
end
if ~isempty(xsliceplot) && ~isequal(togglestates.xyztoggles,[1 1 1]) && strcmp(togglestates.cutview,'3d')
if togglestates.xyztoggles(1)
set(xsliceplot,'Visible','on');
else
set(xsliceplot,'Visible','off');
end
if togglestates.xyztoggles(2)
set(ysliceplot,'Visible','on');
else
set(ysliceplot,'Visible','off');
end
if togglestates.xyztoggles(3)
set(zsliceplot,'Visible','on');
else
set(zsliceplot,'Visible','off');
end
ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
end
if (~togglestates.refreshcuts) && (~togglestates.refreshview)
return
end
%% Render slices
V=getappdata(resultfig,'V');
inverted=getappdata(resultfig,'inverted');
if isempty(inverted)
inverted=0;
end
options.d2.writeatlases=1;
if ~isfield(options,'native')
options.native=0;
end
if togglestates.refreshcuts % reload image(s)
clear V
if strcmp(togglestates.template, 'Choose...')
togglestates.template = togglestates.customfile;
end
[V1, V2, V3] = ea_assignbackdrop(togglestates.template,options,'Patient',options.native);
if ~isfield(V1,'img') % image supplied
V{1} = nifti(V1.fname);
V{2} = nifti(V2.fname);
V{3} = nifti(V3.fname);
else
V{1}=V1; V{2}=V2; V{3}=V3;
end
setappdata(resultfig,'templateused',togglestates.template); % refresh used template.
end
if ~inverted==togglestates.tinvert
inverted=togglestates.tinvert;
end
if ~isempty(controlhandles)
if get(controlhandles.slicepopup,'Value')==1
togglestates.xyzmm=[togglestates.xyzmm,1]';
try
xyzv= V{1}.mat \ togglestates.xyzmm;
catch
keyboard
end
xyzv=round(xyzv(1:3)); % now in voxel coordinates.
%keyboard
elseif get(controlhandles.slicepopup,'Value')==2
xyzv = togglestates.xyzmm;
% xyzv= V{1}.mat * togglestates.xyzmm;
end
else % direct call from script.
xyzv = round(V{1}.mat \ [togglestates.xyzmm,1]');
end
% balance the contrast
% if togglestates.refreshcuts
% [balanced,colormap] = ea_autocontrast(double(V{1}.dat),2.5);
% end
usesag=(length(V)>2)*2; % check if explicit saggital volume is available
if isa(V{1+usesag},'nifti') % memory mapped, nifti function
xsliceplot=slice3i(resultfig,V{1+usesag}.dat,V{1+usesag}.mat,1,xyzv(1),controlhandles);
else
xsliceplot=slice3i(resultfig,V{1+usesag}.img,V{1+usesag}.mat,1,xyzv(1),controlhandles);
end
usecor=length(V)>1; % check if explicit coronal volume is available
if isa(V{1+usecor},'nifti') % memory mapped, nifti function
ysliceplot=slice3i(resultfig,V{1+usecor}.dat,V{1+usecor}.mat,2,xyzv(2),controlhandles);
else
ysliceplot=slice3i(resultfig,V{1+usecor}.img,V{1+usecor}.mat,2,xyzv(2),controlhandles);
end
if isa(V{1},'nifti') % memory mapped, nifti function
zsliceplot=slice3i(resultfig,V{1}.dat,V{1}.mat,3,xyzv(3),controlhandles);
else
zsliceplot=slice3i(resultfig,V{1}.img,V{1}.mat,3,xyzv(3),controlhandles);
end
%colormap(cmap);
% Set template popup and slice toggle properly, useful when scripting
if isempty(controlhandles)
awin = getappdata(resultfig, 'awin');
if isvalid(awin) % check if not deleted (closed)
handle = guidata(awin);
[~, idx] = ismember(togglestates.template, handle.templatepopup.String);
if idx
handle.templatepopup.Value = idx;
else
ea_cprintf('CmdWinWarnings', 'Template not found in the list!\n');
end
handle.xtoggle.Value = togglestates.xyztoggles(1);
handle.ytoggle.Value = togglestates.xyztoggles(2);
handle.ztoggle.Value = togglestates.xyztoggles(3);
end
end
% store data in figure
setappdata(resultfig,'xsliceplot',xsliceplot);
setappdata(resultfig,'ysliceplot',ysliceplot);
setappdata(resultfig,'zsliceplot',zsliceplot);
%ea_settransparency(resultfig,togglestates)
setappdata(resultfig,'V',V);
setappdata(resultfig,'inverted',inverted);
function slice=ea_invert(slice,flag)
if flag
slice=slice*-1+max(slice(:));
end
function imin=proxy_slice(slice,togglestates,dim)
maxv=max(slice(:));
minv=min(slice(:));
slice=slice-minv; % 0 smallest number.
slice=(slice/(maxv-minv))*255; % 255 highest number.
imin=repmat(uint8((((slice)))),[1,1,4]);
imin(:,:,4)=uint8(togglestates.xyztransparencies(dim));