Home > bioelectromagnetism > avw_img_read.m

avw_img_read

PURPOSE ^

avw_img_read - read Analyze format data image (*.img)

SYNOPSIS ^

function [ avw, machine ] = avw_img_read(fileprefix,IMGorient,machine,verbose)

DESCRIPTION ^

 avw_img_read - read Analyze format data image (*.img)
 
 [ avw, machine ] = avw_img_read(fileprefix,[orient],[machine],[verbose])
 
 fileprefix - a string, the filename without the .img extension
 
 orient - read a specified orientation, integer values:
 
          '', use header history orient field
          0,  transverse unflipped (LAS*)
          1,  coronal unflipped (LA*S)
          2,  sagittal unflipped (L*AS)
          3,  transverse flipped (LPS*)
          4,  coronal flipped (LA*I)
          5,  sagittal flipped (L*AI)
 
 where * follows the slice dimension and letters indicate +XYZ
 orientations (L left, R right, A anterior, P posterior,
 I inferior, & S superior).
 
 Some files may contain data in the 3-5 orientations, but this
 is unlikely. For more information about orientation, see the
 documentation at the end of this .m file.  See also the
 AVW_FLIP function for orthogonal reorientation.

 machine - a string, see machineformat in fread for details.
           The default here is 'ieee-le' but the routine
           will automatically switch between little and big
           endian to read any such Analyze header.  It
           reports the appropriate machine format and can
           return the machine value.

 verbose - the default is to output processing information to the command
           window.  If verbose = 0, this will not happen.

 Returned values:

 avw.hdr - a struct with image data parameters.
 avw.img - a 3D matrix of image data (double precision).

 A returned 3D matrix will correspond with the
 default ANALYZE coordinate system, which
 is Left-handed:

 X-Y plane is Transverse
 X-Z plane is Coronal
 Y-Z plane is Sagittal

 X axis runs from patient right (low X) to patient Left (high X)
 Y axis runs from posterior (low Y) to Anterior (high Y)
 Z axis runs from inferior (low Z) to Superior (high Z)

 The function can read a 4D Analyze volume, but only if it is in the
 axial unflipped orientation.

 See also: avw_hdr_read (called by this function),
           avw_view, avw_write, avw_img_write, avw_flip

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ avw, machine ] = avw_img_read(fileprefix,IMGorient,machine,verbose)
0002 
0003 % avw_img_read - read Analyze format data image (*.img)
0004 %
0005 % [ avw, machine ] = avw_img_read(fileprefix,[orient],[machine],[verbose])
0006 %
0007 % fileprefix - a string, the filename without the .img extension
0008 %
0009 % orient - read a specified orientation, integer values:
0010 %
0011 %          '', use header history orient field
0012 %          0,  transverse unflipped (LAS*)
0013 %          1,  coronal unflipped (LA*S)
0014 %          2,  sagittal unflipped (L*AS)
0015 %          3,  transverse flipped (LPS*)
0016 %          4,  coronal flipped (LA*I)
0017 %          5,  sagittal flipped (L*AI)
0018 %
0019 % where * follows the slice dimension and letters indicate +XYZ
0020 % orientations (L left, R right, A anterior, P posterior,
0021 % I inferior, & S superior).
0022 %
0023 % Some files may contain data in the 3-5 orientations, but this
0024 % is unlikely. For more information about orientation, see the
0025 % documentation at the end of this .m file.  See also the
0026 % AVW_FLIP function for orthogonal reorientation.
0027 %
0028 % machine - a string, see machineformat in fread for details.
0029 %           The default here is 'ieee-le' but the routine
0030 %           will automatically switch between little and big
0031 %           endian to read any such Analyze header.  It
0032 %           reports the appropriate machine format and can
0033 %           return the machine value.
0034 %
0035 % verbose - the default is to output processing information to the command
0036 %           window.  If verbose = 0, this will not happen.
0037 %
0038 % Returned values:
0039 %
0040 % avw.hdr - a struct with image data parameters.
0041 % avw.img - a 3D matrix of image data (double precision).
0042 %
0043 % A returned 3D matrix will correspond with the
0044 % default ANALYZE coordinate system, which
0045 % is Left-handed:
0046 %
0047 % X-Y plane is Transverse
0048 % X-Z plane is Coronal
0049 % Y-Z plane is Sagittal
0050 %
0051 % X axis runs from patient right (low X) to patient Left (high X)
0052 % Y axis runs from posterior (low Y) to Anterior (high Y)
0053 % Z axis runs from inferior (low Z) to Superior (high Z)
0054 %
0055 % The function can read a 4D Analyze volume, but only if it is in the
0056 % axial unflipped orientation.
0057 %
0058 % See also: avw_hdr_read (called by this function),
0059 %           avw_view, avw_write, avw_img_write, avw_flip
0060 %
0061 
0062 
0063 % $Revision: 1.1 $ $Date: 2004/11/12 01:30:25 $
0064 
0065 % Licence:  GNU GPL, no express or implied warranties
0066 % History:  05/2002, Darren.Weber@flinders.edu.au
0067 %                    The Analyze format is copyright
0068 %                    (c) Copyright, 1986-1995
0069 %                    Biomedical Imaging Resource, Mayo Foundation
0070 %           01/2003, Darren.Weber@flinders.edu.au
0071 %                    - adapted for matlab v5
0072 %                    - revised all orientation information and handling
0073 %                      after seeking further advice from AnalyzeDirect.com
0074 %           03/2003, Darren.Weber@flinders.edu.au
0075 %                    - adapted for -ve pixdim values (non standard Analyze)
0076 %           07/2004, chodkowski@kennedykrieger.org, added ability to
0077 %                    read volumes with dimensionality greather than 3.
0078 %  a >3D volume cannot be flipped.  and error is thrown if a volume of
0079 %  greater than 3D (ie, avw.hdr.dime.dim(1) > 3) requests a data flip
0080 %  (ie, avw.hdr.hist.orient ~= 0 ).  i pulled the transfer of read-in
0081 %  data (tmp) to avw.img out of any looping mechanism.  looping is not
0082 %  necessary as the data is already in its correct orientation.  using
0083 %  'reshape' rather than looping should be faster but, more importantly,
0084 %  it allows the reading in of N-D volumes. See lines 270-280.
0085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0086 
0087 if ~exist('IMGorient','var'), IMGorient = ''; end
0088 if ~exist('machine','var'), machine = 'ieee-le'; end
0089 if ~exist('verbose','var'), verbose = 1; end
0090 
0091 if isempty(IMGorient), IMGorient = ''; end
0092 if isempty(machine), machine = 'ieee-le'; end
0093 if isempty(verbose), verbose = 1; end
0094 
0095 if ~exist('fileprefix','var'),
0096   msg = sprintf('...no input fileprefix - see help avw_img_read\n\n');
0097   error(msg);
0098 end
0099 if findstr('.hdr',fileprefix),
0100   fileprefix = strrep(fileprefix,'.hdr','');
0101 end
0102 if findstr('.img',fileprefix),
0103   fileprefix = strrep(fileprefix,'.img','');
0104 end
0105 
0106 % MAIN
0107 
0108 % Read the file header
0109 [ avw, machine ] = avw_hdr_read(fileprefix,machine,verbose);
0110 
0111 avw = read_image(avw,IMGorient,machine,verbose);
0112 
0113 return
0114 
0115 
0116 
0117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0119 function [ avw ] = read_image(avw,IMGorient,machine,verbose)
0120 
0121 fid = fopen(sprintf('%s.img',avw.fileprefix),'r',machine);
0122 if fid < 0,
0123   msg = sprintf('...cannot open file %s.img\n\n',avw.fileprefix);
0124   error(msg);
0125 end
0126 
0127 if verbose,
0128     ver = '[$Revision: 1.1 $]';
0129     fprintf('\nAVW_IMG_READ [v%s]\n',ver(12:16));  tic;
0130 end
0131 
0132 % short int bitpix;    /* Number of bits per pixel; 1, 8, 16, 32, or 64. */
0133 % short int datatype      /* Datatype for this image set */
0134 % /*Acceptable values for datatype are*/
0135 % #define DT_NONE             0
0136 % #define DT_UNKNOWN          0    /*Unknown data type*/
0137 % #define DT_BINARY           1    /*Binary             ( 1 bit per voxel)*/
0138 % #define DT_UNSIGNED_CHAR    2    /*Unsigned character ( 8 bits per voxel)*/
0139 % #define DT_SIGNED_SHORT     4    /*Signed short       (16 bits per voxel)*/
0140 % #define DT_SIGNED_INT       8    /*Signed integer     (32 bits per voxel)*/
0141 % #define DT_FLOAT           16    /*Floating point     (32 bits per voxel)*/
0142 % #define DT_COMPLEX         32    /*Complex (64 bits per voxel; 2 floating point numbers)/*
0143 % #define DT_DOUBLE          64    /*Double precision   (64 bits per voxel)*/
0144 % #define DT_RGB            128    /*A Red-Green-Blue datatype*/
0145 % #define DT_ALL            255    /*Undocumented*/
0146 
0147 switch double(avw.hdr.dime.bitpix),
0148   case  1,   precision = 'bit1';
0149   case  8,   precision = 'uchar';
0150   case 16,   precision = 'int16';
0151   case 32,
0152     if     isequal(avw.hdr.dime.datatype, 8), precision = 'int32';
0153     else                                      precision = 'single';
0154     end
0155   case 64,   precision = 'double';
0156   otherwise,
0157     precision = 'uchar';
0158     if verbose, fprintf('...precision undefined in header, using ''uchar''\n'); end
0159 end
0160 
0161 % read the whole .img file into matlab (faster)
0162 if verbose,
0163     fprintf('...reading %s Analyze %s image format.\n',machine,precision);
0164 end
0165 fseek(fid,0,'bof');
0166 % adjust for matlab version
0167 ver = version;
0168 ver = str2num(ver(1));
0169 if ver < 6,
0170   tmp = fread(fid,inf,sprintf('%s',precision));
0171 else,
0172   tmp = fread(fid,inf,sprintf('%s=>double',precision));
0173 end
0174 fclose(fid);
0175 
0176 % Update the global min and max values
0177 avw.hdr.dime.glmax = max(double(tmp));
0178 avw.hdr.dime.glmin = min(double(tmp));
0179 
0180 
0181 %---------------------------------------------------------------
0182 % Now partition the img data into xyz
0183 
0184 % --- first figure out the size of the image
0185 
0186 % short int dim[ ];      /* Array of the image dimensions */
0187 %
0188 % dim[0]      Number of dimensions in database; usually 4.
0189 % dim[1]      Image X dimension;  number of pixels in an image row.
0190 % dim[2]      Image Y dimension;  number of pixel rows in slice.
0191 % dim[3]      Volume Z dimension; number of slices in a volume.
0192 % dim[4]      Time points; number of volumes in database.
0193 
0194 PixelDim = double(avw.hdr.dime.dim(2));
0195 RowDim   = double(avw.hdr.dime.dim(3));
0196 SliceDim = double(avw.hdr.dime.dim(4));
0197 TimeDim  = double(avw.hdr.dime.dim(5));
0198 
0199 PixelSz  = double(avw.hdr.dime.pixdim(2));
0200 RowSz    = double(avw.hdr.dime.pixdim(3));
0201 SliceSz  = double(avw.hdr.dime.pixdim(4));
0202 TimeSz   = double(avw.hdr.dime.pixdim(5));
0203 
0204 
0205 
0206 
0207 % ---- NON STANDARD ANALYZE...
0208 
0209 % Some Analyze files have been found to set -ve pixdim values, eg
0210 % the MNI template avg152T1_brain in the FSL etc/standard folder,
0211 % perhaps to indicate flipped orientation?  If so, this code below
0212 % will NOT handle the flip correctly!
0213 if PixelSz < 0,
0214   warning('X pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(2))');
0215   PixelSz = abs(PixelSz);
0216   avw.hdr.dime.pixdim(2) = single(PixelSz);
0217 end
0218 if RowSz < 0,
0219   warning('Y pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(3))');
0220   RowSz = abs(RowSz);
0221   avw.hdr.dime.pixdim(3) = single(RowSz);
0222 end
0223 if SliceSz < 0,
0224   warning('Z pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(4))');
0225   SliceSz = abs(SliceSz);
0226   avw.hdr.dime.pixdim(4) = single(SliceSz);
0227 end
0228 
0229 % ---- END OF NON STANDARD ANALYZE
0230 
0231 
0232 
0233 
0234 
0235 % --- check the orientation specification and arrange img accordingly
0236 if ~isempty(IMGorient),
0237   if ischar(IMGorient),
0238     avw.hdr.hist.orient = uint8(str2num(IMGorient));
0239   else
0240     avw.hdr.hist.orient = uint8(IMGorient);
0241   end
0242 end,
0243 
0244 if isempty(avw.hdr.hist.orient),
0245   msg = [ '...unspecified avw.hdr.hist.orient, using default 0\n',...
0246       '   (check image and try explicit IMGorient option).\n'];
0247   fprintf(msg);
0248   avw.hdr.hist.orient = uint8(0);
0249 end
0250 
0251 % --- check if the orientation is to be flipped for a volume with more
0252 % --- than 3 dimensions.  this logic is currently unsupported so throw
0253 % --- an error.  volumes of any dimensionality may be read in *only* as
0254 % --- unflipped, ie, avw.hdr.hist.orient == 0
0255 if ( TimeDim > 1 ) && (avw.hdr.hist.orient ~= 0 ),
0256    msg = [ 'ERROR: This volume has more than 3 dimensions *and* ', ...
0257            'requires flipping the data.  Flipping is not supported ', ...
0258            'for volumes with dimensionality greater than 3.  Set ', ...
0259            'avw.hdr.hist.orient = 0 and flip your volume after ', ...
0260            'calling this function' ];
0261    msg = sprintf( '%s (%s).', msg, mfilename );
0262    error( msg );
0263 end
0264 
0265 switch double(avw.hdr.hist.orient),
0266   
0267   case 0, % transverse unflipped
0268     
0269     % orient = 0:  The primary orientation of the data on disk is in the
0270     % transverse plane relative to the object scanned.  Most commonly, the fastest
0271     % moving index through the voxels that are part of this transverse image would
0272     % span the right-left extent of the structure imaged, with the next fastest
0273     % moving index spanning the posterior-anterior extent of the structure.  This
0274     % 'orient' flag would indicate to Analyze that this data should be placed in
0275     % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
0276     % being the slice direction.
0277     
0278     % For the 'transverse unflipped' type, the voxels are stored with
0279     % Pixels in 'x' axis (varies fastest) - from patient right to left
0280     % Rows in   'y' axis                  - from patient posterior to anterior
0281     % Slices in 'z' axis                  - from patient inferior to superior
0282     
0283     if verbose, fprintf('...reading axial unflipped orientation\n'); end
0284     
0285     % -- This code will handle nD files
0286     dims = double( avw.hdr.dime.dim(2:end) );
0287     % replace dimensions of 0 with 1 to be used in reshape
0288     idx = find( dims == 0 );
0289     dims( idx ) = 1;
0290     avw.img = reshape( tmp, dims );
0291     
0292     % -- The code above replaces this
0293     %         avw.img = zeros(PixelDim,RowDim,SliceDim);
0294     %
0295     %         n = 1;
0296     %         x = 1:PixelDim;
0297     %         for z = 1:SliceDim,
0298     %             for y = 1:RowDim,
0299     %                 % load Y row of X values into Z slice avw.img
0300     %                 avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0301     %                 n = n + PixelDim;
0302     %             end
0303     %         end
0304     
0305     
0306     % no need to rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0307     
0308     
0309 case 1, % coronal unflipped
0310     
0311     % orient = 1:  The primary orientation of the data on disk is in the coronal
0312     % plane relative to the object scanned.  Most commonly, the fastest moving
0313     % index through the voxels that are part of this coronal image would span the
0314     % right-left extent of the structure imaged, with the next fastest moving
0315     % index spanning the inferior-superior extent of the structure.  This 'orient'
0316     % flag would indicate to Analyze that this data should be placed in the X-Z
0317     % plane of the 3D Analyze Coordinate System, with the Y dimension being the
0318     % slice direction.
0319     
0320     % For the 'coronal unflipped' type, the voxels are stored with
0321     % Pixels in 'x' axis (varies fastest) - from patient right to left
0322     % Rows in   'z' axis                  - from patient inferior to superior
0323     % Slices in 'y' axis                  - from patient posterior to anterior
0324     
0325     if verbose, fprintf('...reading coronal unflipped orientation\n'); end
0326     
0327     avw.img = zeros(PixelDim,SliceDim,RowDim);
0328     
0329     n = 1;
0330     x = 1:PixelDim;
0331     for y = 1:SliceDim,
0332       for z = 1:RowDim,
0333         % load Z row of X values into Y slice avw.img
0334         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0335         n = n + PixelDim;
0336       end
0337     end
0338     
0339     % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0340     avw.hdr.dime.dim(2:4) = int16([PixelDim,SliceDim,RowDim]);
0341     avw.hdr.dime.pixdim(2:4) = single([PixelSz,SliceSz,RowSz]);
0342     
0343     
0344   case 2, % sagittal unflipped
0345     
0346     % orient = 2:  The primary orientation of the data on disk is in the sagittal
0347     % plane relative to the object scanned.  Most commonly, the fastest moving
0348     % index through the voxels that are part of this sagittal image would span the
0349     % posterior-anterior extent of the structure imaged, with the next fastest
0350     % moving index spanning the inferior-superior extent of the structure.  This
0351     % 'orient' flag would indicate to Analyze that this data should be placed in
0352     % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
0353     % being the slice direction.
0354     
0355     % For the 'sagittal unflipped' type, the voxels are stored with
0356     % Pixels in 'y' axis (varies fastest) - from patient posterior to anterior
0357     % Rows in   'z' axis                  - from patient inferior to superior
0358     % Slices in 'x' axis                  - from patient right to left
0359     
0360     if verbose, fprintf('...reading sagittal unflipped orientation\n'); end
0361     
0362     avw.img = zeros(SliceDim,PixelDim,RowDim);
0363     
0364     n = 1;
0365     y = 1:PixelDim;         % posterior to anterior (fastest)
0366     
0367     for x = 1:SliceDim,     % right to left (slowest)
0368       for z = 1:RowDim,   % inferior to superior
0369         
0370         % load Z row of Y values into X slice avw.img
0371         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0372         n = n + PixelDim;
0373       end
0374     end
0375     
0376     % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0377     avw.hdr.dime.dim(2:4) = int16([SliceDim,PixelDim,RowDim]);
0378     avw.hdr.dime.pixdim(2:4) = single([SliceSz,PixelSz,RowSz]);
0379     
0380     
0381     %--------------------------------------------------------------------------------
0382     % Orient values 3-5 have the second index reversed in order, essentially
0383     % 'flipping' the images relative to what would most likely become the vertical
0384     % axis of the displayed image.
0385     %--------------------------------------------------------------------------------
0386     
0387   case 3, % transverse/axial flipped
0388     
0389     % orient = 3:  The primary orientation of the data on disk is in the
0390     % transverse plane relative to the object scanned.  Most commonly, the fastest
0391     % moving index through the voxels that are part of this transverse image would
0392     % span the right-left extent of the structure imaged, with the next fastest
0393     % moving index spanning the *anterior-posterior* extent of the structure.  This
0394     % 'orient' flag would indicate to Analyze that this data should be placed in
0395     % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
0396     % being the slice direction.
0397     
0398     % For the 'transverse flipped' type, the voxels are stored with
0399     % Pixels in 'x' axis (varies fastest) - from patient right to Left
0400     % Rows in   'y' axis                  - from patient anterior to Posterior *
0401     % Slices in 'z' axis                  - from patient inferior to Superior
0402     
0403     if verbose, fprintf('...reading axial flipped (+Y from Anterior to Posterior)\n'); end
0404     
0405     avw.img = zeros(PixelDim,RowDim,SliceDim);
0406     
0407     n = 1;
0408     x = 1:PixelDim;
0409     for z = 1:SliceDim,
0410       for y = RowDim:-1:1, % flip in Y, read A2P file into P2A 3D matrix
0411         
0412         % load a flipped Y row of X values into Z slice avw.img
0413         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0414         n = n + PixelDim;
0415       end
0416     end
0417     
0418     % no need to rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0419     
0420     
0421   case 4, % coronal flipped
0422     
0423     % orient = 4:  The primary orientation of the data on disk is in the coronal
0424     % plane relative to the object scanned.  Most commonly, the fastest moving
0425     % index through the voxels that are part of this coronal image would span the
0426     % right-left extent of the structure imaged, with the next fastest moving
0427     % index spanning the *superior-inferior* extent of the structure.  This 'orient'
0428     % flag would indicate to Analyze that this data should be placed in the X-Z
0429     % plane of the 3D Analyze Coordinate System, with the Y dimension being the
0430     % slice direction.
0431     
0432     % For the 'coronal flipped' type, the voxels are stored with
0433     % Pixels in 'x' axis (varies fastest) - from patient right to Left
0434     % Rows in   'z' axis                  - from patient superior to Inferior*
0435     % Slices in 'y' axis                  - from patient posterior to Anterior
0436     
0437     if verbose, fprintf('...reading coronal flipped (+Z from Superior to Inferior)\n'); end
0438     
0439     avw.img = zeros(PixelDim,SliceDim,RowDim);
0440     
0441     n = 1;
0442     x = 1:PixelDim;
0443     for y = 1:SliceDim,
0444       for z = RowDim:-1:1, % flip in Z, read S2I file into I2S 3D matrix
0445         
0446         % load a flipped Z row of X values into Y slice avw.img
0447         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0448         n = n + PixelDim;
0449       end
0450     end
0451     
0452     % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0453     avw.hdr.dime.dim(2:4) = int16([PixelDim,SliceDim,RowDim]);
0454     avw.hdr.dime.pixdim(2:4) = single([PixelSz,SliceSz,RowSz]);
0455     
0456     
0457   case 5, % sagittal flipped
0458     
0459     % orient = 5:  The primary orientation of the data on disk is in the sagittal
0460     % plane relative to the object scanned.  Most commonly, the fastest moving
0461     % index through the voxels that are part of this sagittal image would span the
0462     % posterior-anterior extent of the structure imaged, with the next fastest
0463     % moving index spanning the *superior-inferior* extent of the structure.  This
0464     % 'orient' flag would indicate to Analyze that this data should be placed in
0465     % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
0466     % being the slice direction.
0467     
0468     % For the 'sagittal flipped' type, the voxels are stored with
0469     % Pixels in 'y' axis (varies fastest) - from patient posterior to Anterior
0470     % Rows in   'z' axis                  - from patient superior to Inferior*
0471     % Slices in 'x' axis                  - from patient right to Left
0472     
0473     if verbose, fprintf('...reading sagittal flipped (+Z from Superior to Inferior)\n'); end
0474     
0475     avw.img = zeros(SliceDim,PixelDim,RowDim);
0476     
0477     n = 1;
0478     y = 1:PixelDim;
0479     
0480     for x = 1:SliceDim,
0481       for z = RowDim:-1:1, % flip in Z, read S2I file into I2S 3D matrix
0482         
0483         % load a flipped Z row of Y values into X slice avw.img
0484         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0485         n = n + PixelDim;
0486       end
0487     end
0488     
0489     % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
0490     avw.hdr.dime.dim(2:4) = int16([SliceDim,PixelDim,RowDim]);
0491     avw.hdr.dime.pixdim(2:4) = single([SliceSz,PixelSz,RowSz]);
0492     
0493   otherwise
0494     
0495     error('unknown value in avw.hdr.hist.orient, try explicit IMGorient option.');
0496     
0497 end
0498 
0499 if verbose, t=toc; fprintf('...done (%5.2f sec).\n\n',t); end
0500 
0501 return
0502 
0503 
0504 
0505 
0506 % This function attempts to read the orientation of the
0507 % Analyze file according to the hdr.hist.orient field of the
0508 % header.  Unfortunately, this field is optional and not
0509 % all programs will set it correctly, so there is no guarantee,
0510 % that the data loaded will be correctly oriented.  If necessary,
0511 % experiment with the 'orient' option to read the .img
0512 % data into the 3D matrix of avw.img as preferred.
0513 %
0514 
0515 % (Conventions gathered from e-mail with support@AnalyzeDirect.com)
0516 %
0517 % 0  transverse unflipped
0518 %       X direction first,  progressing from patient right to left,
0519 %       Y direction second, progressing from patient posterior to anterior,
0520 %       Z direction third,  progressing from patient inferior to superior.
0521 % 1  coronal unflipped
0522 %       X direction first,  progressing from patient right to left,
0523 %       Z direction second, progressing from patient inferior to superior,
0524 %       Y direction third,  progressing from patient posterior to anterior.
0525 % 2  sagittal unflipped
0526 %       Y direction first,  progressing from patient posterior to anterior,
0527 %       Z direction second, progressing from patient inferior to superior,
0528 %       X direction third,  progressing from patient right to left.
0529 % 3  transverse flipped
0530 %       X direction first,  progressing from patient right to left,
0531 %       Y direction second, progressing from patient anterior to posterior,
0532 %       Z direction third,  progressing from patient inferior to superior.
0533 % 4  coronal flipped
0534 %       X direction first,  progressing from patient right to left,
0535 %       Z direction second, progressing from patient superior to inferior,
0536 %       Y direction third,  progressing from patient posterior to anterior.
0537 % 5  sagittal flipped
0538 %       Y direction first,  progressing from patient posterior to anterior,
0539 %       Z direction second, progressing from patient superior to inferior,
0540 %       X direction third,  progressing from patient right to left.
0541 
0542 
0543 %----------------------------------------------------------------------------
0544 % From ANALYZE documentation...
0545 %
0546 % The ANALYZE coordinate system has an origin in the lower left
0547 % corner. That is, with the subject lying supine, the coordinate
0548 % origin is on the right side of the body (x), at the back (y),
0549 % and at the feet (z). This means that:
0550 %
0551 % +X increases from right (R) to left (L)
0552 % +Y increases from the back (posterior,P) to the front (anterior, A)
0553 % +Z increases from the feet (inferior,I) to the head (superior, S)
0554 %
0555 % The LAS orientation is the radiological convention, where patient
0556 % left is on the image right.  The alternative neurological
0557 % convention is RAS (also Talairach convention).
0558 %
0559 % A major advantage of the Analzye origin convention is that the
0560 % coordinate origin of each orthogonal orientation (transverse,
0561 % coronal, and sagittal) lies in the lower left corner of the
0562 % slice as it is displayed.
0563 %
0564 % Orthogonal slices are numbered from one to the number of slices
0565 % in that orientation. For example, a volume (x, y, z) dimensioned
0566 % 128, 256, 48 has:
0567 %
0568 %   128 sagittal   slices numbered 1 through 128 (X)
0569 %   256 coronal    slices numbered 1 through 256 (Y)
0570 %    48 transverse slices numbered 1 through  48 (Z)
0571 %
0572 % Pixel coordinates are made with reference to the slice numbers from
0573 % which the pixels come. Thus, the first pixel in the volume is
0574 % referenced p(1,1,1) and not at p(0,0,0).
0575 %
0576 % Transverse slices are in the XY plane (also known as axial slices).
0577 % Sagittal slices are in the ZY plane.
0578 % Coronal slices are in the ZX plane.
0579 %
0580 %----------------------------------------------------------------------------
0581 
0582 
0583 %----------------------------------------------------------------------------
0584 % E-mail from support@AnalyzeDirect.com
0585 %
0586 % The 'orient' field in the data_history structure specifies the primary
0587 % orientation of the data as it is stored in the file on disk.  This usually
0588 % corresponds to the orientation in the plane of acquisition, given that this
0589 % would correspond to the order in which the data is written to disk by the
0590 % scanner or other software application.  As you know, this field will contain
0591 % the values:
0592 %
0593 % orient = 0 transverse unflipped
0594 % 1 coronal unflipped
0595 % 2 sagittal unflipped
0596 % 3 transverse flipped
0597 % 4 coronal flipped
0598 % 5 sagittal flipped
0599 %
0600 % It would be vary rare that you would ever encounter any old Analyze 7.5
0601 % files that contain values of 'orient' which indicate that the data has been
0602 % 'flipped'.  The 'flipped flag' values were really only used internal to
0603 % Analyze to precondition data for fast display in the Movie module, where the
0604 % images were actually flipped vertically in order to accommodate the raster
0605 % paint order on older graphics devices.  The only cases you will encounter
0606 % will have values of 0, 1, or 2.
0607 %
0608 % As mentioned, the 'orient' flag only specifies the primary orientation of
0609 % data as stored in the disk file itself.  It has nothing to do with the
0610 % representation of the data in the 3D Analyze coordinate system, which always
0611 % has a fixed representation to the data.  The meaning of the 'orient' values
0612 % should be interpreted as follows:
0613 %
0614 % orient = 0:  The primary orientation of the data on disk is in the
0615 % transverse plane relative to the object scanned.  Most commonly, the fastest
0616 % moving index through the voxels that are part of this transverse image would
0617 % span the right-left extent of the structure imaged, with the next fastest
0618 % moving index spanning the posterior-anterior extent of the structure.  This
0619 % 'orient' flag would indicate to Analyze that this data should be placed in
0620 % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
0621 % being the slice direction.
0622 %
0623 % orient = 1:  The primary orientation of the data on disk is in the coronal
0624 % plane relative to the object scanned.  Most commonly, the fastest moving
0625 % index through the voxels that are part of this coronal image would span the
0626 % right-left extent of the structure imaged, with the next fastest moving
0627 % index spanning the inferior-superior extent of the structure.  This 'orient'
0628 % flag would indicate to Analyze that this data should be placed in the X-Z
0629 % plane of the 3D Analyze Coordinate System, with the Y dimension being the
0630 % slice direction.
0631 %
0632 % orient = 2:  The primary orientation of the data on disk is in the sagittal
0633 % plane relative to the object scanned.  Most commonly, the fastest moving
0634 % index through the voxels that are part of this sagittal image would span the
0635 % posterior-anterior extent of the structure imaged, with the next fastest
0636 % moving index spanning the inferior-superior extent of the structure.  This
0637 % 'orient' flag would indicate to Analyze that this data should be placed in
0638 % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
0639 % being the slice direction.
0640 %
0641 % Orient values 3-5 have the second index reversed in order, essentially
0642 % 'flipping' the images relative to what would most likely become the vertical
0643 % axis of the displayed image.
0644 %
0645 % Hopefully you understand the difference between the indication this 'orient'
0646 % flag has relative to data stored on disk and the full 3D Analyze Coordinate
0647 % System for data that is managed as a volume image.  As mentioned previously,
0648 % the orientation of patient anatomy in the 3D Analyze Coordinate System has a
0649 % fixed orientation relative to each of the orthogonal axes.  This orientation
0650 % is completely described in the information that is attached, but the basics
0651 % are:
0652 %
0653 % Left-handed coordinate system
0654 %
0655 % X-Y plane is Transverse
0656 % X-Z plane is Coronal
0657 % Y-Z plane is Sagittal
0658 %
0659 % X axis runs from patient right (low X) to patient left (high X)
0660 % Y axis runs from posterior (low Y) to anterior (high Y)
0661 % Z axis runs from inferior (low Z) to superior (high Z)
0662 %
0663 %----------------------------------------------------------------------------
0664 
0665 
0666 
0667 %----------------------------------------------------------------------------
0668 % SPM2 NOTES from spm2 webpage: One thing to watch out for is the image
0669 % orientation. The proper Analyze format uses a left-handed co-ordinate
0670 % system, whereas Talairach uses a right-handed one. In SPM99, images were
0671 % flipped at the spatial normalisation stage (from one co-ordinate system
0672 % to the other). In SPM2b, a different approach is used, so that either a
0673 % left- or right-handed co-ordinate system is used throughout. The SPM2b
0674 % program is told about the handedness that the images are stored with by
0675 % the spm_flip_analyze_images.m function and the defaults.analyze.flip
0676 % parameter that is specified in the spm_defaults.m file. These files are
0677 % intended to be customised for each site. If you previously used SPM99
0678 % and your images were flipped during spatial normalisation, then set
0679 % defaults.analyze.flip=1. If no flipping took place, then set
0680 % defaults.analyze.flip=0. Check that when using the Display facility
0681 % (possibly after specifying some rigid-body rotations) that:
0682 %
0683 % The top-left image is coronal with the top (superior) of the head displayed
0684 % at the top and the left shown on the left. This is as if the subject is viewed
0685 % from behind.
0686 %
0687 % The bottom-left image is axial with the front (anterior) of the head at the
0688 % top and the left shown on the left. This is as if the subject is viewed from above.
0689 %
0690 % The top-right image is sagittal with the front (anterior) of the head at the
0691 % left and the top of the head shown at the top. This is as if the subject is
0692 % viewed from the left.
0693 %----------------------------------------------------------------------------

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003