Home > bioelectromagnetism > avw_img_read_4d.m

avw_img_read_4d

PURPOSE ^

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

SYNOPSIS ^

function [ avw, machine ] = avw_img_read_4d(fileprefix,volIndex,IMGorient,machine)

DESCRIPTION ^

 avw_img_read - read Analyze format data image (*.img)
 
 [ avw, machine ] = avw_img_read(fileprefix, [volIndex], [orient], [machine])
 
 fileprefix - a string, the filename without the .img extension
 
 volIndex - the volume to read from a 4D file, where the first volume
            has index 1 (the default)
 
 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.

 Returned values:

 avw.hdr - a struct with image data parameters.
 avw.img - a 3D matrix of image data (double precision)
           from a volume of a 4D Analyze file

 The 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)

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

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