/
in_tess_mrimask.m
332 lines (311 loc) · 12 KB
/
in_tess_mrimask.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
function TessMat = in_tess_mrimask(MriFile, isMni)
% IN_TESS_MRIMASK: Import an MRI as a mask or atlas, and tesselate the volumes in it
%
% USAGE: TessMat = in_tess_mrimask(MriFile, isMni=0)
% TessMat = in_tess_mrimask(sMri, isMni=0)
%
%
% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c)2000-2020 University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2012-2020
% Parse inputs
if (nargin < 2) || isempty(isMni)
isMni = 0;
end
% Read MRI volume
if ischar(MriFile)
% FreeSurfer ASEG or BrainSuite SVReg label file? For convenience, both
% are referred to as isAseg
isAseg = (~isempty(strfind(MriFile, 'svreg.label.nii.gz')) || ~isempty(strfind(MriFile, 'aseg.mgz')) || ~isempty(strfind(MriFile, 'aseg.auto.mgz')) || ~isempty(strfind(MriFile, 'aseg.auto_noCCseg.mgz')));
isBrainSuite = ~isempty(strfind(MriFile, 'svreg.label.nii.gz'));
% Read volume
isInteractive = ~isAseg;
if isMni
sMri = in_mri(MriFile, 'ALL-MNI', isInteractive, 0);
else
sMri = in_mri(MriFile, 'ALL', isInteractive, 0);
if isBrainSuite
sMri.Cube = mod(sMri.Cube,1000);
end
end
if isempty(sMri)
TessMat = [];
return;
end
% Try to get volume labels for this atlas
if isBrainSuite
FsVersion = 0; % Set Fs version 0 for BrainSuite
elseif isAseg
FsVersion = 5; % FreeSurfer 5 and 6
else
FsVersion = []; % Other file formats
end
VolumeLabels = panel_scout('GetVolumeLabels', MriFile, FsVersion);
else
sMri = MriFile;
MriFile = [];
VolumeLabels = [];
end
% Convert to double and keep only the first volume (if multiple)
sMri.Cube = double(sMri.Cube(:,:,:,1));
% Get al the values in the MRI
allValues = unique(sMri.Cube);
% If values are not integers, it is not a mask or an atlas: it has to be binarized first
if any(allValues ~= round(allValues))
% Warning: not a binary mask
isConfirm = java_dialog('confirm', ['Warning: This is not a binary mask.' 10 'Try to import this MRI as a surface anyway?'], 'Import binary mask');
if ~isConfirm
TessMat = [];
return;
end
% Analyze MRI histogram
Histogram = mri_histogram(sMri.Cube);
% Binarize based on background level
sMri.Cube = (sMri.Cube > Histogram.bgLevel);
allValues = [0,1];
end
% Display warning when no MNI transformation available
if isMni && (~isfield(sMri, 'NCS') || ~isfield(sMri.NCS, 'R') || isempty(sMri.NCS.R))
isMni = 0;
disp('Error: No MNI transformation available in this file.');
end
% Default labels for FreeSurfer ASEG.MGZ
if (length(allValues) > 10) && ~isempty(MriFile) && isAseg
switch FsVersion
case 5
Labels = {...
16, 'Brainstem'; ...
8, 'Cerebellum L'; ...
47, 'Cerebellum R'; ...
26, 'Accumbens L'; ...
58, 'Accumbens R'; ...
18, 'Amygdala L'; ...
54, 'Amygdala R'; ...
11, 'Caudate L'; ...
50, 'Caudate R'; ...
17, 'Hippocampus L'; ...
53, 'Hippocampus R'; ...
13, 'Pallidum L'; ...
52, 'Pallidum R'; ...
12, 'Putamen L'; ...
51, 'Putamen R'; ...
9, 'Thalamus L'; ...
10, 'Thalamus L'; ...
48, 'Thalamus R'; ...
49, 'Thalamus R'; ...
};
% Group the cerebellum white+cortex voxels
sMri.Cube(sMri.Cube == 7) = 8;
sMri.Cube(sMri.Cube == 46) = 47;
% Group all the brainstem elements
sMri.Cube(sMri.Cube == 170) = 16;
sMri.Cube(sMri.Cube == 171) = 16;
sMri.Cube(sMri.Cube == 172) = 16;
sMri.Cube(sMri.Cube == 173) = 16;
sMri.Cube(sMri.Cube == 174) = 16;
sMri.Cube(sMri.Cube == 175) = 16;
sMri.Cube(sMri.Cube == 177) = 16;
sMri.Cube(sMri.Cube == 178) = 16;
sMri.Cube(sMri.Cube == 179) = 16;
% Update unique values
allValues = unique(sMri.Cube);
case 3
Labels = {...
48, 'Brainstem'; ...
24, 'Cerebellum L'; ...
141, 'Cerebellum R'; ...
51, 'Hippocampus L'; ...
159, 'Hippocampus R'; ...
39, 'Pallidum L'; ...
156, 'Pallidum R'; ...
36, 'Putamen L'; ...
153, 'Putamen R'; ...
30, 'Thalamus L'; ...
147, 'Thalamus R'; ...
};
% Grouping the cerebellum white+cortex voxels
sMri.Cube(sMri.Cube == 21) = 24;
sMri.Cube(sMri.Cube == 138) = 141;
case 0
% This means BrainSuite labels
% Remove 4th decimal place which indicates GM or WM
if sum(sMri.Cube(:)==370) == 0
% Old BrainSuite labels on/before 2018
Labels = {...
800, 'Brainstem'; ...
900, 'Cerebellum'; ...
345, 'Hippocampus L'; ...
344, 'Hippocampus R'; ...
613, 'Caudate L';...
612, 'Caudate R';...
615, 'Putamen L'; ...
614, 'Putamen R'; ...
617, 'Pallidum L'; ...
616, 'Pallidum R'; ...
621, 'Accumbens L';...
620, 'Accumbens R';...
641, 'Thalamus L'; ...
640, 'Thalamus R';...
};
else
% new BrainSuite Labels
Labels = {...
800, 'Brainstem'; ...
900, 'Cerebellum'; ...
371, 'Hippocampus L'; ...
370, 'Hippocampus R'; ...
641, 'Pallidum L'; ...
640, 'Pallidum R'; ...
631, 'Putamen L'; ...
630, 'Putamen R'; ...
661, 'Thalamus L'; ...
660, 'Thalamus R'; ...
613, 'Caudate L';...
612, 'Caudate R';...
621, 'Accumbens L';...
620, 'Accumbens R';...
};
end
end
% Keep only the labelled areas
[allValues, I, J] = intersect([Labels{:,1}], allValues);
Labels = Labels(I,:);
% Get labelled values in alphabetical order
allValues = [Labels{:,1}];
% Labels available in an external file
elseif ~isempty(VolumeLabels)
% Keep only the labelled areas
[allValues, I, J] = intersect([VolumeLabels{:,1}], allValues);
Labels = VolumeLabels(I,:);
% Get labelled values in alphabetical order
allValues = [Labels{:,1}];
% No labels available
else
% Skip the first value (background)
allValues(1) = [];
Labels = {};
end
TessMat = repmat(struct('Comment', [], 'Vertices', [], 'Faces', []), [1, 0]);
% Generate a tesselation for all the others
for i = 1:length(allValues)
% Display progress bar
if (length(allValues) > 1)
bst_progress('text', sprintf('Importing atlas surface #%d/%d...', i, length(allValues)));
end
% Get the binary mask of the current region
mask = (sMri.Cube == allValues(i));
% Fill small holes
mask = mri_dilate(mask, 1);
mask = mask & ~mri_dilate(~mask, 1);
% Close the volumes by setting to zero all the edges
mask([1,end],:,:) = 0;
mask(:,[1,end],:) = 0;
mask(:,:,[1,end]) = 0;
% Empty mask: skip
if ~any(mask(:))
continue;
end
% Comment field
if ~isempty(Labels)
Comment = Labels{i,2};
elseif (length(allValues) > 1)
Comment = sprintf('%d', allValues(i));
elseif ~isempty(MriFile)
[fPath, fBase, fExt] = bst_fileparts(MriFile);
Comment = fBase;
else
Comment = 'mask';
end
% Add new tesselation
iTess = [];
% If importing an atlas in MNI coordinates
if isMni
% Get the coordinates of all the points in the mask
Pvox = [];
iMask = find(mask);
[Pvox(:,1),Pvox(:,2),Pvox(:,3)] = ind2sub(size(mask), iMask);
% Convert to MNI coordinates
Pmni = cs_convert(sMri, 'voxel', 'mni', Pvox);
% Find left and right areas
iL = find(Pmni(:,1) < 0);
iR = find(Pmni(:,1) >= 0);
% If this is a bilateral region: split in two
if ~isempty(iL) && ~isempty(iR) && (length(iL) / length(iR) < 1.6) && (length(iL) / length(iR) > 0.4)
% Create two separate masks
maskL = mask;
maskR = mask;
maskL(iMask(iR)) = 0;
maskR(iMask(iL)) = 0;
% Tesselate left
[Vertices, Faces] = TesselateMask(sMri, maskL, isMni);
if ~isempty(Vertices)
iTess = length(TessMat) + 1;
TessMat(iTess).Comment = [Comment, ' L'];
TessMat(iTess).Vertices = Vertices;
TessMat(iTess).Faces = Faces;
end
% Tesselate right
[Vertices, Faces] = TesselateMask(sMri, maskR, isMni);
if ~isempty(Vertices)
iTess = length(TessMat) + 1;
TessMat(iTess).Comment = [Comment, ' R'];
TessMat(iTess).Vertices = Vertices;
TessMat(iTess).Faces = Faces;
end
end
end
% If tesselation was not already added
if isempty(iTess)
% Tesselate surface
[Vertices,Faces] = TesselateMask(sMri, mask, isMni);
% Create new entry
if ~isempty(Vertices)
iTess = length(TessMat) + 1;
TessMat(iTess).Comment = Comment;
TessMat(iTess).Vertices = Vertices;
TessMat(iTess).Faces = Faces;
end
end
end
end
%% ===== FINALIZE SURFACE =====
function [Vertices, Faces] = TesselateMask(sMri, mask, isMni)
% Create an isosurface
[Faces, Vertices] = mri_isosurface(mask, 0.5);
% Convert to Brainstorm format
Vertices = Vertices(:, [2 1 3]);
Faces = Faces(:, [2 1 3]);
% Convert coordinates
if isMni
% Convert from voxels to MNI space
Vertices = cs_convert(sMri, 'voxel', 'mni', Vertices);
else
% Convert from voxels to MRI (in meters)
Vertices = cs_convert(sMri, 'voxel', 'mri', Vertices);
end
% Remove small objects
[Vertices, Faces] = tess_remove_small(Vertices, Faces);
% Compute vertex-vertex connectivity
VertConn = tess_vertconn(Vertices, Faces);
% Smooth surface
Vertices = tess_smooth(Vertices, 1, 2, VertConn, 0);
% Enlarge a bit
VertNormals = tess_normals(Vertices, Faces, VertConn);
Vertices = Vertices + 0.0002 * VertNormals;
end