0001 function vb_atlas_label(brainfile, atlas_id, Rmax)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 if nargin < 2, atlas_id = 'aal'; end;
0032
0033
0034 if nargin < 3, Rmax = 7; end;
0035
0036
0037
0038
0039 atlas_dir = 'atlas2vb_dir/MNI_atlas_templates/';
0040 atlas_text = which([atlas_dir atlas_id '.txt']);
0041 atlas_file = which([atlas_dir atlas_id '.img']);
0042
0043
0044
0045
0046
0047 MaxCortex = 90;
0048
0049 EXT_brain = '.brain.mat';
0050 brain_id = brainfile(1:findstr(brainfile,EXT_brain)-1);
0051
0052 save_areafile = [brain_id '_' atlas_id '.area.mat'];
0053 save_atlasfile = [brain_id '_' atlas_id '.act.mat'];
0054
0055
0056 Vmni = vb_load_cortex(brainfile, 'MNI')*1000;
0057 Vmni = floor(Vmni);
0058 Npoint = size(Vmni,1);
0059 [Ndipole, NdipoleL] = vb_load_cortex_info(brainfile);
0060
0061 if Npoint ~= Ndipole,
0062 error('Brain file info error');
0063 end
0064
0065 Vlabel = zeros(Npoint,1);
0066 Vcount = zeros(Npoint,1);
0067
0068
0069
0070
0071 fprintf('Load Cortex area index\n')
0072
0073 load(brainfile, 'Vinfo');
0074
0075 Vindx = [Vinfo.cortexL; Vinfo.cortexR];
0076 Ncortex = length(Vindx);
0077
0078
0079
0080
0081
0082
0083
0084 [dim, Trans] = vb_get_image_info(atlas_file);
0085 invTrans = inv(Trans');
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 avw = avw_img_read(atlas_file);
0101 Zlabel = avw.img;
0102
0103 Zlabel = Zlabel(:);
0104 Nlabel = max(Zlabel);
0105
0106
0107 x_indx = -Rmax:Rmax;
0108 [X,Y,Z] = ndgrid(x_indx,x_indx,x_indx);
0109 Box_ix = [X(:),Y(:),Z(:)];
0110
0111
0112 ix = find( sum(Box_ix.^2,2) <= Rmax^2);
0113 Box_ix = Box_ix(ix,:);
0114
0115 Nbox = size(Box_ix,1);
0116 ilabel = (1:Nbox)';
0117
0118 for n = 1:Ncortex
0119
0120 np = Vindx(n);
0121 V = Vmni(np,:);
0122
0123
0124 XYZ = vb_repadd(Box_ix, V);
0125
0126
0127 Vox = fix( [XYZ ones(Nbox,1)] * invTrans );
0128
0129 ix = find( (Vox(:,1) > 0) ...
0130 & (Vox(:,2) > 0) ...
0131 & (Vox(:,3) > 0) ...
0132 & (Vox(:,1) <= dim(1) ) ...
0133 & (Vox(:,2) <= dim(2) ) ...
0134 & (Vox(:,3) <= dim(3) ) ...
0135 );
0136
0137
0138 Vindex = sub2ind(dim,Vox(ix,1),Vox(ix,2),Vox(ix,3));
0139
0140
0141 vlabel = Zlabel(Vindex) ;
0142
0143
0144 jj = find( vlabel > MaxCortex );
0145 vlabel(jj) = 0;
0146
0147 if strcmp( atlas_id, 'aal')
0148 if np > NdipoleL,
0149
0150 jj = find( mod(vlabel,2) == 1);
0151 vlabel(jj) = 0;
0152 else
0153
0154 jj = find( mod(vlabel,2) == 0);
0155 vlabel(jj) = 0;
0156 end
0157 end
0158
0159
0160 vlabel = vlabel + 1;
0161
0162
0163
0164 S = sparse(ix , vlabel, 1, Nbox, Nlabel+1);
0165
0166
0167 count_label = full(sum(S,1));
0168
0169
0170 [count,imax] = max(count_label(2:Nlabel+1));
0171
0172 if count > 0
0173 Vlabel(np) = imax(1);
0174 Vcount(np) = count;
0175
0176 if strcmp( atlas_id, 'brodmann')
0177 if np > NdipoleL,
0178
0179 Vlabel(np) = Vlabel(np) + 1000;
0180 end
0181 end
0182 end
0183 end
0184
0185
0186
0187
0188
0189
0190 [label, label_name] = vb_read_atlas_label(atlas_text);
0191
0192
0193 if strcmp( atlas_id, 'brodmann')
0194
0195 label = [label(:) ; label(:) + 1000];
0196
0197
0198 N = length(label_name);
0199 for n = 1:N
0200
0201 name = label_name{n};
0202 label_name{n} = [name '_L'];
0203 label_name{n+N} = [name '_R'];
0204 end
0205
0206 end
0207
0208
0209
0210
0211
0212
0213
0214 Atlas.key = [atlas_id];
0215 Atlas.xxP = Vlabel;
0216 Atlas.label = label;
0217 Atlas.label_name = label_name;
0218
0219 if exist(save_atlasfile,'file')
0220 delete(save_atlasfile);
0221 end
0222 if exist(save_areafile,'file')
0223 delete(save_areafile);
0224 end
0225
0226
0227 fprintf('Save the atlas file as "%s" \n', save_atlasfile);
0228 vb_add_act(save_atlasfile,Atlas,[],false);
0229
0230
0231 fprintf('Save the area file as "%s" \n', save_areafile);
0232
0233 vb_save_atlas_label(save_areafile,Vlabel,label,label_name);
0234
0235 NLABEL = sum( Vlabel > 0 );
0236 fprintf('# of all vertex = %d\n',Npoint)
0237 fprintf('# of cortex points = %d\n',Ncortex)
0238 fprintf('# of labeled points = %d\n',NLABEL)
0239