-
Notifications
You must be signed in to change notification settings - Fork 0
/
SNiP_tbxvol_extract_fast.m
101 lines (94 loc) · 3.07 KB
/
SNiP_tbxvol_extract_fast.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
function [outr, res]=SNiP_tbxvol_extract_fast(srcimgs, roispec, avg)
% functionm [outr, res]=SNiP_tbxvol_extract_fast(srcimages, roispec, avg)
%
% Reads raw data from a series of images (nifti only) and extracts from
% a given ROI (mask, sphere, single coordinate).
%
% Example code:
% F = spm_select(Inf,'image');
%
% % roi (mask)
% roispec{1}.srcimg = spm_select(1, 'image');
%
% % sphere around center
% roispec{2}.roisphere.roicent = [0 0 0]';
% roispec{2}.roisphere.roirad = 6;
%
% % coordinate
% roispec{3}.roilist = [12 12 12]';
%
% [outr, res] = SNiP_tbxvol_extract_fast(F, roispec, 'none',0);
%
% scrcimages char array of input files
% roispec roi structure (see example above)
% avg none or vox
%
% outr the output format depends on option avg
% none outr is a #rois cell array of matrices (tpt x voxels in
% ROI)
% vox outr is a tpt x ROI array with meaned time series
%
% This code adapted from the Volumes toolbox for SPM.
%
% Axel Schäfer, axel.schaefer@zi-mannheim.de, March 16 2013
% Systems Neuroscience in Psychiatry (SNiP), CIMH, Mannheim
interp=0;
res = struct('raw',[], 'adj',[], 'posmm',[], 'Vspace',[]);
V = spm_vol(srcimgs);
for l = 1:numel(roispec)
fprintf(1,'roi # %d\n',l);
res(l).Vspace = rmfield(V(1),{'fname','private'});
[res(l).posmm res(l).posvx] = get_pos(roispec{l},V(1));
for k=1:numel(V)
raw = spm_sample_vol(V(k), res(l).posvx(1,:), ...
res(l).posvx(2,:), res(l).posvx(3,:), ...
interp);
switch avg
case 'none'
outr{l}(k,:) = raw;
case 'vox'
outr(k,l) = mean(raw);
end;
end;
end;
function [posmm, posvx] = get_pos(roispec, V)
if isfield(roispec, 'srcimg')
% resample mask VM in space of current image V
VM = spm_vol(roispec.srcimg);
x = []; y = []; z = [];
[x1 y1] = ndgrid(1:V.dim(1),1:V.dim(2));
for p = 1:V.dim(3)
B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
M = VM.mat\(V.mat/B);
msk = find(spm_slice_vol(VM,M,V.dim(1:2),0));
if ~isempty(msk)
z1 = p*ones(size(msk(:)));
x = [x; x1(msk(:))];
y = [y; y1(msk(:))];
z = [z; z1];
end;
end;
posvx = [x'; y'; z'];
xyzmm = V.mat*[posvx;ones(1,size(posvx,2))];
posmm = xyzmm(1:3,:);
elseif isfield(roispec, 'roisphere')
cent = round(V.mat\[roispec.roisphere.roicent; 1]);
tmp = spm_imatrix(V.mat);
vdim = tmp(7:9);
vxrad = ceil((roispec.roisphere.roirad*ones(1,3))./ ...
vdim)';
[x y z] = ndgrid(-vxrad(1):sign(vdim(1)):vxrad(1), ...
-vxrad(2):sign(vdim(2)):vxrad(2), ...
-vxrad(3):sign(vdim(3)):vxrad(3));
sel = (x./vxrad(1)).^2 + (y./vxrad(2)).^2 + (z./vxrad(3)).^2 <= 1;
x = cent(1)+x(sel(:));
y = cent(2)+y(sel(:));
z = cent(3)+z(sel(:));
posvx = [x y z]';
xyzmm = V.mat*[posvx;ones(1,size(posvx,2))];
posmm = xyzmm(1:3,:);
elseif isfield(roispec, 'roilist')
posmm = roispec.roilist;
posvx = V.mat\[posmm; ones(1,size(posmm,2))];
posvx = posvx(1:3,:);
end;