-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreateRoi.m
More file actions
351 lines (260 loc) · 9.29 KB
/
createRoi.m
File metadata and controls
351 lines (260 loc) · 9.29 KB
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
% (C) Copyright 2021 CPP ROI developers
function mask = createRoi(type, specification, volumeDefiningImage, outputDir, saveImg)
%
% Returns a mask to be used as a ROI by ``spm_summarize``.
% Can also save the ROI as binary image.
%
% USAGE::
%
% mask = createROI(type, ...
% specification, ...
% volumeDefiningImage = '', ...
% outputDir = pwd, ...
% saveImg = false);
%
% mask = createROI('mask', roiImage);
% mask = createROI('sphere', sphere);
% mask = createROI('intersection', specification);
% mask = createROI('expand', specification);
%
% See the ``demos/roi`` to see examples on how to use it.
%
% :param type: ``'mask'``, ``'sphere'``, ``'intersection'``, ``'expand'``
% :type type: string
% :param specification: depending on the chosen ``type`` this can be:
%
% :param roiImage: fullpath of the roi image for ``'mask'``
% :type roiImage: string
% :param sphere: defines the charateristic for ``'sphere'``
% :type sphere: structure
% ``sphere.location``: X Y Z coordinates in millimeters
% ``sphere.radius``: radius in millimeters
% :param specification: defines the charateristic for ``'intersection'`` and ``'expand'``
% :type sphere: structure
% ``sphere.location``: X Y Z coordinates in millimeters
% ``sphere.radius``: radius in millimeters
%
% :param volumeDefiningImage: fullpath of the image that will define the space
% (resolution, ...) if the ROI is to be saved.
% :type volumeDefiningImage: string
% :param saveImg: Will save the resulting image as binary mask if set to
% ``true``
% :type saveImg: boolean
%
% :returns:
%
% mask - structure for the volume of interest adapted from ``spm_ROI``
%
% mask.def - VOI definition [sphere, mask]
% mask.rej - cell array of disabled VOI definition options
% mask.xyz - centre of VOI {mm} (for sphere)
% mask.spec - VOI definition parameters (radius for sphere)
% mask.str - description of the VOI
%
% mask.descrip
% mask.label
%
% mask.roi.size - number of voxel in ROI
% mask.roi.XYZ - voxel coordinates
% mask.roi.XYZmm - voxel world coordinates
%
% mask.global.hdr - header of the "search space" where the roi is
% defined
% mask.global.img
% mask.global.XYZ
% mask.global.XYZmm
%
%
if nargin < 5
saveImg = false;
end
if nargin < 4
outputDir = pwd;
end
if nargin < 3
volumeDefiningImage = '';
end
switch type
case 'sphere'
sphere = specification;
mask.def = type;
mask.spec = sphere.radius;
mask.xyz = sphere.location;
if size(mask.xyz, 1) ~= 3
mask.xyz = mask.xyz';
end
mask = spm_ROI(mask);
mask.roi.XYZmm = [];
mask = createRoiLabel(mask);
case 'mask'
roiImage = specification;
mask = struct('XYZmm', []);
mask = defineGlobalSearchSpace(mask, roiImage);
% in real world coordinates
mask.global.XYZmm = returnXYZm(mask.global.hdr.mat, mask.global.XYZ);
assert(size(mask.global.XYZmm, 2) == sum(mask.global.img(:)));
locationsToSample = mask.global.XYZmm;
mask.def = type;
mask.spec = roiImage;
[~, mask.roi.XYZmm, j] = spm_ROI(mask, locationsToSample);
mask.roi.XYZ = mask.global.XYZ(:, j);
mask = setRoiSizeAndType(mask, type);
mask = createRoiLabel(mask);
case 'intersection'
% Ugly hack
% ideally we want to loop over the masks and figure out
% if they are binary images or spheres...
if ischar(specification.mask1)
roiImage = specification.mask1;
sphere = specification.mask2;
else
roiImage = specification.mask1;
sphere = specification.mask2;
end
mask = createRoi('mask', roiImage);
mask2 = createRoi('sphere', sphere);
locationsToSample = mask.global.XYZmm;
[~, mask.roi.XYZmm] = spm_ROI(mask2, locationsToSample);
mask = setRoiSizeAndType(mask, type);
mask = createRoiLabel(mask);
case 'merge'
roiImages = specification;
mask = [];
% loop through the ROIs to merge
for iRoi = 1:length(roiImages)
% load one ROI per time
maskToMerge = spm_read_vols(spm_vol(roiImages{iRoi}));
mask = cat(4, mask, maskToMerge);
end
% merge the logical masks into one
mask = any(mask, 4);
% assign fileprefix (name to be save?)
% [ WIP ]
% mask = createRoiLabel(mask);
case 'expand'
% Ugly hack
% ideally we want to loop over the masks and figure out
% if they are binary images or spheres...
if ischar(specification.mask1)
roiImage = specification.mask1;
sphere = specification.mask2;
else
roiImage = specification.mask1;
sphere = specification.mask2;
end
specification = struct( ...
'mask1', roiImage, ...
'mask2', sphere);
% take as radius step the smallest voxel dimension of the roi image
hdr = spm_vol(roiImage);
dim = diag(hdr.mat);
radiusStep = min(abs(dim(1:3)));
while true
mask = createRoi('intersection', specification);
mask.roi.radius = specification.mask2.radius;
if mask.roi.size > sphere.maxNbVoxels
break
end
specification.mask2.radius = specification.mask2.radius + radiusStep;
end
mask.xyz = sphere.location;
mask = setRoiSizeAndType(mask, type);
mask = createRoiLabel(mask);
end
if saveImg
saveRoi(mask, volumeDefiningImage, outputDir);
end
end
function mask = defineGlobalSearchSpace(mask, image)
%
% gets the X, Y, Z subscripts of the voxels included in the ROI
%
mask.global.hdr = spm_vol(image);
mask.global.img = logical(spm_read_vols(mask.global.hdr));
[X, Y, Z] = ind2sub(size(mask.global.img), find(mask.global.img));
% XYZ format
mask.global.XYZ = [X'; Y'; Z'];
mask.global.size = size(mask.global.XYZ, 2);
end
function XYZmm = returnXYZm(transformationMatrix, XYZ)
%
% apply voxel to world transformation
%
XYZmm = transformationMatrix(1:3, :) * [XYZ; ones(1, size(XYZ, 2))];
end
function mask = setRoiSizeAndType(mask, type)
mask.def = type;
mask.roi.size = size(mask.roi.XYZmm, 2);
end
function mask = createRoiLabel(mask)
switch mask.def
case 'sphere'
mask.label = sprintf('sphere%0.0fx%0.0fy%0.0fz%0.0f', ...
mask.spec, ...
mask.xyz);
% change any minus coordinate (x = -67) to minus (xMinus67)
% SUPER ugly but any minus will mess up the bids parsing otherwise
mask.label = strrep(mask.label, '-', 'Minus');
mask.descrip = sprintf('%s at [%0.1f %0.1f %0.1f]', ...
mask.str, ...
mask.xyz);
case 'expand'
mask.label = sprintf('%sVox%i', ...
mask.def, ...
mask.roi.size);
mask.descrip = sprintf('%s from [%0.0f %0.0f %0.0f] till %i voxels', ...
mask.def, ...
mask.xyz, ...
mask.roi.size);
otherwise
mask.label = mask.def;
mask.descrip = mask.def;
end
end
function saveRoi(mask, volumeDefiningImage, outputDir)
if strcmp(mask.def, 'sphere')
[~, mask.roi.XYZmm] = spm_ROI(mask, volumeDefiningImage);
mask = setRoiSizeAndType(mask, mask.def);
end
roiName = createRoiName(mask, volumeDefiningImage);
% use the marsbar toolbox
roiObject = maroi_pointlist(struct('XYZ', mask.roi.XYZmm, ...
'mat', spm_get_space(volumeDefiningImage), ...
'label', mask.label, ...
'descrip', mask.descrip));
% use Marsbar to save as a .mat and then convert that to an image
% in the correct space
tempFile = spm_file(roiName, ...
'ext', ...
'mat');
saveroi(roiObject, ...
fullfile(outputDir, tempFile));
mars_rois2img(fullfile(outputDir, tempFile), ...
fullfile(outputDir, roiName), ...
spm_vol(volumeDefiningImage));
delete(fullfile(outputDir, tempFile));
% delete label files
delete(fullfile(outputDir, '*_mask_labels.mat'));
end
function roiName = createRoiName(mask, volumeDefiningImage)
if strcmp(mask.def, 'sphere')
p.filename = '';
p.ext = '.nii';
p.type = 'mask';
if ~isempty(volumeDefiningImage)
tmp = bids.internal.parse_filename(volumeDefiningImage);
% if the volume defining image has a space entity we reuse it
if isfield(p, 'space')
p.space = tmp.space;
end
end
else
p = bids.internal.parse_filename(mask.global.hdr.fname);
end
label = '';
if isfield(p, 'label')
label = p.label;
end
p.label = [label ' ' mask.label];
roiName = createFilename(p);
end