


Get header information etc for images.
FORMAT V = spm_vol(P)
P - a matrix of filenames.
V - a vector of structures containing image volume information.
The elements of the structures are:
V.fname - the filename of the image.
V.dim - the x, y and z dimensions of the volume
V.dt - A 1x2 array. First element is datatype (see spm_type).
The second is 1 or 0 depending on the endian-ness.
V.mat - a 4x4 affine transformation matrix mapping from
voxel coordinates to real world coordinates.
V.pinfo - plane info for each plane of the volume.
V.pinfo(1,:) - scale for each plane
V.pinfo(2,:) - offset for each plane
The true voxel intensities of the jth image are given
by: val*V.pinfo(1,j) + V.pinfo(2,j)
V.pinfo(3,:) - offset into image (in bytes).
If the size of pinfo is 3x1, then the volume is assumed
to be contiguous and each plane has the same scalefactor
and offset.
____________________________________________________________________________
The fields listed above are essential for the mex routines, but other
fields can also be incorporated into the structure.
The images are not memory mapped at this step, but are mapped when
the mex routines using the volume information are called.
Note that spm_vol can also be applied to the filename(s) of 4-dim
volumes. In that case, the elements of V will point to a series of 3-dim
images.
This is a replacement for the spm_map_vol and spm_unmap_vol stuff of
MatLab4 SPMs (SPM94-97), which is now obsolete.
_______________________________________________________________________
Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e



0001 function V = nic_spm_vol(P) 0002 % Get header information etc for images. 0003 % FORMAT V = spm_vol(P) 0004 % P - a matrix of filenames. 0005 % V - a vector of structures containing image volume information. 0006 % The elements of the structures are: 0007 % V.fname - the filename of the image. 0008 % V.dim - the x, y and z dimensions of the volume 0009 % V.dt - A 1x2 array. First element is datatype (see spm_type). 0010 % The second is 1 or 0 depending on the endian-ness. 0011 % V.mat - a 4x4 affine transformation matrix mapping from 0012 % voxel coordinates to real world coordinates. 0013 % V.pinfo - plane info for each plane of the volume. 0014 % V.pinfo(1,:) - scale for each plane 0015 % V.pinfo(2,:) - offset for each plane 0016 % The true voxel intensities of the jth image are given 0017 % by: val*V.pinfo(1,j) + V.pinfo(2,j) 0018 % V.pinfo(3,:) - offset into image (in bytes). 0019 % If the size of pinfo is 3x1, then the volume is assumed 0020 % to be contiguous and each plane has the same scalefactor 0021 % and offset. 0022 %____________________________________________________________________________ 0023 % 0024 % The fields listed above are essential for the mex routines, but other 0025 % fields can also be incorporated into the structure. 0026 % 0027 % The images are not memory mapped at this step, but are mapped when 0028 % the mex routines using the volume information are called. 0029 % 0030 % Note that spm_vol can also be applied to the filename(s) of 4-dim 0031 % volumes. In that case, the elements of V will point to a series of 3-dim 0032 % images. 0033 % 0034 % This is a replacement for the spm_map_vol and spm_unmap_vol stuff of 0035 % MatLab4 SPMs (SPM94-97), which is now obsolete. 0036 %_______________________________________________________________________ 0037 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e 0038 0039 % John Ashburner 0040 % $Id: spm_vol.m 434 2006-02-15 13:46:45Z john $ 0041 0042 if nargin==0, 0043 V = struct('fname', {},... 0044 'dim', {},... 0045 'dt', {},... 0046 'pinfo', {},... 0047 'mat', {},... 0048 'n', {},... 0049 'descrip', {},... 0050 'private',{}); 0051 return; 0052 end; 0053 0054 % If is already a vol structure then just return; 0055 if isstruct(P), V = P; return; end; 0056 0057 V = subfunic_2(P); 0058 return; 0059 0060 function V = subfunic_2(P) 0061 if iscell(P), 0062 V = cell(size(P)); 0063 for j=1:numel(P), 0064 if iscell(P{j}), 0065 V{j} = subfunic_2(P{j}); 0066 else 0067 V{j} = subfunic_1(P{j}); 0068 end; 0069 end; 0070 else 0071 V = subfunic_1(P); 0072 end; 0073 return; 0074 0075 function V = subfunic_1(P) 0076 if isempty(P), 0077 V = []; 0078 return; 0079 end; 0080 0081 counter = 0; 0082 for i=1:size(P,1), 0083 v = subfunic_(P(i,:)); 0084 [V(counter+1:counter+size(v, 2),1).fname] = deal(''); 0085 [V(counter+1:counter+size(v, 2),1).mat] = deal([0 0 0 0]); 0086 [V(counter+1:counter+size(v, 2),1).mat] = deal(eye(4)); 0087 [V(counter+1:counter+size(v, 2),1).mat] = deal([1 0 0]'); 0088 if isempty(v), 0089 hread_error_message(P(i,:)); 0090 error(['Can''t get volume information for ''' P(i,:) '''']); 0091 end 0092 0093 f = fieldnames(v); 0094 for j=1:size(f,1) 0095 eval(['[V(counter+1:counter+size(v,2),1).' f{j} '] = deal(v.' f{j} ');']); 0096 end 0097 counter = counter + size(v,2); 0098 end 0099 0100 return; 0101 0102 function V = subfunic_(p) 0103 V = []; 0104 p = deblank(p); 0105 [pth,nam,ext] = fileparts(deblank(p)); 0106 t = find(ext==','); 0107 0108 n = []; 0109 if ~isempty(t), 0110 t = t(1); 0111 n1 = ext((t+1):end); 0112 if ~isempty(n1), 0113 n = str2num(n1); 0114 ext = ext(1:(t-1)); 0115 end; 0116 end; 0117 p = fullfile(pth,[nam ext]); 0118 0119 if strcmpi(ext,'.nii') || (strcmpi(ext,'.img') && ... 0120 (exist(fullfile(pth,[nam '.hdr']),'file') || exist(fullfile(pth,[nam '.HDR']),'file'))), 0121 if isempty(n), V = nic_spm_vol_nifti(p); 0122 else V = nic_spm_vol_nifti(p,n); end; 0123 0124 if isempty(n) && length(V.private.dat.dim) > 3 0125 V0(1) = V; 0126 for i = 2:V.private.dat.dim(4) 0127 V0(i) = nic_spm_vol_nifti(p, i); 0128 end 0129 V = V0; 0130 end 0131 0132 if ~isempty(V), return; end; 0133 0134 else % Try other formats 0135 error('Unknown file format.'); 0136 end; 0137 return; 0138 0139 0140 %_______________________________________________________________________ 0141 function hread_error_message(q) 0142 0143 str = {... 0144 'Error reading information on:',... 0145 [' ',nic_spm_str_manip(q,'k40d')],... 0146 ' ',... 0147 'Please check that it is in the correct format.'}; 0148 0149 nic_spm('alert*',str,mfilename,sqrt(-1)); 0150 0151 return; 0152 %_______________________________________________________________________