-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc_threedmap.m
More file actions
84 lines (70 loc) · 1.96 KB
/
mc_threedmap.m
File metadata and controls
84 lines (70 loc) · 1.96 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
function mtx = mc_threedmap(template,VnewPath,data,roiMNI,varargin)
% MC_THREEDMAP
% Reconstruct 3d nii image with node-wise data.
%
% INPUT
% mask/template - Mask for building your map. This also serves as the template to define voxels sizes/etc.
% VnewPath - The new nii file path
% data - nVoxel x 1 (or 1 x nVoxel) vector, the data that being written in
% the new nii file.
% roiMNI - mni coordinates
%
% OPTIONAL INPUT
% connectivity - {1} = single voxel, {7} = faces, {19} = faces + edges, {27} =
% faces + edges + corners (cube), any value not in a cell = radius in
% voxels
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin~=5
expand = 1;
elseif (iscell(varargin{1}))
expand = varargin{1}{1};
else
expand = 0;
end
% Select template file
Vmask = spm_vol(template);
Vnew = Vmask;
Vnew=rmfield(Vnew,'descrip');
Vnew=rmfield(Vnew,'pinfo');
% read mask file
mask = spm_read_vols(Vmask);
% convert coordinates
roiVoxels = inv(Vnew.mat)*[roiMNI ones(size(roiMNI,1),1)]';
roiVoxels = round(roiVoxels(1:3,:))';
% Initiate output data
Vnew.fname = VnewPath;
mtx = zeros(Vnew.dim);
switch expand
case 0
radius = varargin{1};
case 1
radius = .9;
case 7
radius = 1;
case 19
radius = 1.5;
case 27
radius = 2;
end
%[x,y,z] = ndgrid(-1:1);
sz = max(1,ceil(radius));
[x,y,z] = ndgrid(-sz:sz);
kernel = sqrt(x.^2 + y.^2 + z.^2) <=radius; %determine this based on connectivity
modifiers = [x(kernel) y(kernel) z(kernel)];
xlim = Vnew.dim(1);
ylim = Vnew.dim(2);
zlim = Vnew.dim(3);
for i = 1:length(data)
lidx = [];
idxx = roiVoxels(i,1);
idxy = roiVoxels(i,2);
idxz = roiVoxels(i,3);
xyz = repmat([idxx idxy idxz],size(modifiers,1),1);
xyz = xyz + modifiers;
lidx = sub2ind(Vnew.dim,xyz(:,1),xyz(:,2),xyz(:,3));
mtx(lidx) = data(i);
end
% write out results
mtx = mtx.*mask;
spm_write_vol(Vnew,mtx);
end