PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/external/fieldtrip/trunk/fileio/private/avw_img_read.m

http://open-realtime-fmri.googlecode.com/
MATLAB | 697 lines | 178 code | 114 blank | 405 comment | 50 complexity | c3036270ea1a3880db1c5afe78adf957 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. function [ avw, machine ] = avw_img_read(fileprefix,IMGorient,machine,verbose)
  2. % avw_img_read - read Analyze format data image (*.img)
  3. %
  4. % [ avw, machine ] = avw_img_read(fileprefix,[orient],[machine],[verbose])
  5. %
  6. % fileprefix - a string, the filename without the .img extension
  7. %
  8. % orient - read a specified orientation, integer values:
  9. %
  10. % '', use header history orient field
  11. % 0, transverse unflipped (LAS*)
  12. % 1, coronal unflipped (LA*S)
  13. % 2, sagittal unflipped (L*AS)
  14. % 3, transverse flipped (LPS*)
  15. % 4, coronal flipped (LA*I)
  16. % 5, sagittal flipped (L*AI)
  17. %
  18. % where * follows the slice dimension and letters indicate +XYZ
  19. % orientations (L left, R right, A anterior, P posterior,
  20. % I inferior, & S superior).
  21. %
  22. % Some files may contain data in the 3-5 orientations, but this
  23. % is unlikely. For more information about orientation, see the
  24. % documentation at the end of this .m file. See also the
  25. % AVW_FLIP function for orthogonal reorientation.
  26. %
  27. % machine - a string, see machineformat in fread for details.
  28. % The default here is 'ieee-le' but the routine
  29. % will automatically switch between little and big
  30. % endian to read any such Analyze header. It
  31. % reports the appropriate machine format and can
  32. % return the machine value.
  33. %
  34. % verbose - the default is to output processing information to the command
  35. % window. If verbose = 0, this will not happen.
  36. %
  37. % Returned values:
  38. %
  39. % avw.hdr - a struct with image data parameters.
  40. % avw.img - a 3D matrix of image data (double precision).
  41. %
  42. % A returned 3D matrix will correspond with the
  43. % default ANALYZE coordinate system, which
  44. % is Left-handed:
  45. %
  46. % X-Y plane is Transverse
  47. % X-Z plane is Coronal
  48. % Y-Z plane is Sagittal
  49. %
  50. % X axis runs from patient right (low X) to patient Left (high X)
  51. % Y axis runs from posterior (low Y) to Anterior (high Y)
  52. % Z axis runs from inferior (low Z) to Superior (high Z)
  53. %
  54. % The function can read a 4D Analyze volume, but only if it is in the
  55. % axial unflipped orientation.
  56. %
  57. % See also: avw_hdr_read (called by this function),
  58. % avw_view, avw_write, avw_img_write, avw_flip
  59. %
  60. % $Revision: 2885 $ $Date: 2009/01/14 09:24:45 $
  61. % Licence: GNU GPL, no express or implied warranties
  62. % History: 05/2002, Darren.Weber@flinders.edu.au
  63. % The Analyze format is copyright
  64. % (c) Copyright, 1986-1995
  65. % Biomedical Imaging Resource, Mayo Foundation
  66. % 01/2003, Darren.Weber@flinders.edu.au
  67. % - adapted for matlab v5
  68. % - revised all orientation information and handling
  69. % after seeking further advice from AnalyzeDirect.com
  70. % 03/2003, Darren.Weber@flinders.edu.au
  71. % - adapted for -ve pixdim values (non standard Analyze)
  72. % 07/2004, chodkowski@kennedykrieger.org, added ability to
  73. % read volumes with dimensionality greather than 3.
  74. % a >3D volume cannot be flipped. and error is thrown if a volume of
  75. % greater than 3D (ie, avw.hdr.dime.dim(1) > 3) requests a data flip
  76. % (ie, avw.hdr.hist.orient ~= 0 ). i pulled the transfer of read-in
  77. % data (tmp) to avw.img out of any looping mechanism. looping is not
  78. % necessary as the data is already in its correct orientation. using
  79. % 'reshape' rather than looping should be faster but, more importantly,
  80. % it allows the reading in of N-D volumes. See lines 270-280.
  81. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  82. % check if SPM2 is in path and if not add
  83. hastoolbox('SPM2',1);
  84. if ~exist('IMGorient','var'), IMGorient = ''; end
  85. if ~exist('machine','var'), machine = 'ieee-le'; end
  86. if ~exist('verbose','var'), verbose = 1; end
  87. if isempty(IMGorient), IMGorient = ''; end
  88. if isempty(machine), machine = 'ieee-le'; end
  89. if isempty(verbose), verbose = 1; end
  90. if ~exist('fileprefix','var'),
  91. msg = sprintf('...no input fileprefix - see help avw_img_read\n\n');
  92. error(msg);
  93. end
  94. if findstr('.hdr',fileprefix),
  95. fileprefix = strrep(fileprefix,'.hdr','');
  96. end
  97. if findstr('.img',fileprefix),
  98. fileprefix = strrep(fileprefix,'.img','');
  99. end
  100. % MAIN
  101. % Read the file header
  102. [ avw, machine ] = avw_hdr_read(fileprefix,machine,verbose);
  103. avw = read_image(avw,IMGorient,machine,verbose);
  104. return
  105. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  107. function [ avw ] = read_image(avw,IMGorient,machine,verbose)
  108. fid = fopen(sprintf('%s.img',avw.fileprefix),'r',machine);
  109. if fid < 0,
  110. msg = sprintf('...cannot open file %s.img\n\n',avw.fileprefix);
  111. error(msg);
  112. end
  113. if verbose,
  114. ver = '[$Revision: 2885 $]';
  115. fprintf('\nAVW_IMG_READ [v%s]\n',ver(12:16)); tic;
  116. end
  117. % short int bitpix; /* Number of bits per pixel; 1, 8, 16, 32, or 64. */
  118. % short int datatype /* Datatype for this image set */
  119. % /*Acceptable values for datatype are*/
  120. % #define DT_NONE 0
  121. % #define DT_UNKNOWN 0 /*Unknown data type*/
  122. % #define DT_BINARY 1 /*Binary ( 1 bit per voxel)*/
  123. % #define DT_UNSIGNED_CHAR 2 /*Unsigned character ( 8 bits per voxel)*/
  124. % #define DT_SIGNED_SHORT 4 /*Signed short (16 bits per voxel)*/
  125. % #define DT_SIGNED_INT 8 /*Signed integer (32 bits per voxel)*/
  126. % #define DT_FLOAT 16 /*Floating point (32 bits per voxel)*/
  127. % #define DT_COMPLEX 32 /*Complex (64 bits per voxel; 2 floating point numbers)/*
  128. % #define DT_DOUBLE 64 /*Double precision (64 bits per voxel)*/
  129. % #define DT_RGB 128 /*A Red-Green-Blue datatype*/
  130. % #define DT_ALL 255 /*Undocumented*/
  131. switch double(avw.hdr.dime.bitpix),
  132. case 1, precision = 'bit1';
  133. case 8, precision = 'uchar';
  134. case 16, precision = 'int16';
  135. case 32,
  136. if isequal(avw.hdr.dime.datatype, 8), precision = 'int32';
  137. else precision = 'single';
  138. end
  139. case 64, precision = 'double';
  140. otherwise,
  141. precision = 'uchar';
  142. if verbose, fprintf('...precision undefined in header, using ''uchar''\n'); end
  143. end
  144. % read the whole .img file into matlab (faster)
  145. if verbose,
  146. fprintf('...reading %s Analyze %s image format.\n',machine,precision);
  147. end
  148. fseek(fid,0,'bof');
  149. % adjust for matlab version
  150. ver = version;
  151. ver = str2num(ver(1));
  152. if ver < 6,
  153. tmp = fread(fid,inf,sprintf('%s',precision));
  154. else,
  155. tmp = fread(fid,inf,sprintf('%s=>double',precision));
  156. end
  157. fclose(fid);
  158. % Update the global min and max values
  159. avw.hdr.dime.glmax = max(double(tmp));
  160. avw.hdr.dime.glmin = min(double(tmp));
  161. %---------------------------------------------------------------
  162. % Now partition the img data into xyz
  163. % --- first figure out the size of the image
  164. % short int dim[ ]; /* Array of the image dimensions */
  165. %
  166. % dim[0] Number of dimensions in database; usually 4.
  167. % dim[1] Image X dimension; number of pixels in an image row.
  168. % dim[2] Image Y dimension; number of pixel rows in slice.
  169. % dim[3] Volume Z dimension; number of slices in a volume.
  170. % dim[4] Time points; number of volumes in database.
  171. PixelDim = double(avw.hdr.dime.dim(2));
  172. RowDim = double(avw.hdr.dime.dim(3));
  173. SliceDim = double(avw.hdr.dime.dim(4));
  174. TimeDim = double(avw.hdr.dime.dim(5));
  175. PixelSz = double(avw.hdr.dime.pixdim(2));
  176. RowSz = double(avw.hdr.dime.pixdim(3));
  177. SliceSz = double(avw.hdr.dime.pixdim(4));
  178. TimeSz = double(avw.hdr.dime.pixdim(5));
  179. % ---- NON STANDARD ANALYZE...
  180. % Some Analyze files have been found to set -ve pixdim values, eg
  181. % the MNI template avg152T1_brain in the FSL etc/standard folder,
  182. % perhaps to indicate flipped orientation? If so, this code below
  183. % will NOT handle the flip correctly!
  184. if PixelSz < 0,
  185. warning('X pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(2))');
  186. PixelSz = abs(PixelSz);
  187. avw.hdr.dime.pixdim(2) = single(PixelSz);
  188. end
  189. if RowSz < 0,
  190. warning('Y pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(3))');
  191. RowSz = abs(RowSz);
  192. avw.hdr.dime.pixdim(3) = single(RowSz);
  193. end
  194. if SliceSz < 0,
  195. warning('Z pixdim < 0 !!! resetting to abs(avw.hdr.dime.pixdim(4))');
  196. SliceSz = abs(SliceSz);
  197. avw.hdr.dime.pixdim(4) = single(SliceSz);
  198. end
  199. % ---- END OF NON STANDARD ANALYZE
  200. % --- check the orientation specification and arrange img accordingly
  201. if ~isempty(IMGorient),
  202. if ischar(IMGorient),
  203. avw.hdr.hist.orient = uint8(str2num(IMGorient));
  204. else
  205. avw.hdr.hist.orient = uint8(IMGorient);
  206. end
  207. end,
  208. if isempty(avw.hdr.hist.orient),
  209. msg = [ '...unspecified avw.hdr.hist.orient, using default 0\n',...
  210. ' (check image and try explicit IMGorient option).\n'];
  211. fprintf(msg);
  212. avw.hdr.hist.orient = uint8(0);
  213. end
  214. % --- check if the orientation is to be flipped for a volume with more
  215. % --- than 3 dimensions. this logic is currently unsupported so throw
  216. % --- an error. volumes of any dimensionality may be read in *only* as
  217. % --- unflipped, ie, avw.hdr.hist.orient == 0
  218. if ( TimeDim > 1 ) && (avw.hdr.hist.orient ~= 0 ),
  219. msg = [ 'ERROR: This volume has more than 3 dimensions *and* ', ...
  220. 'requires flipping the data. Flipping is not supported ', ...
  221. 'for volumes with dimensionality greater than 3. Set ', ...
  222. 'avw.hdr.hist.orient = 0 and flip your volume after ', ...
  223. 'calling this function' ];
  224. msg = sprintf( '%s (%s).', msg, mfilename );
  225. error( msg );
  226. end
  227. switch double(avw.hdr.hist.orient),
  228. case 0, % transverse unflipped
  229. % orient = 0: The primary orientation of the data on disk is in the
  230. % transverse plane relative to the object scanned. Most commonly, the fastest
  231. % moving index through the voxels that are part of this transverse image would
  232. % span the right-left extent of the structure imaged, with the next fastest
  233. % moving index spanning the posterior-anterior extent of the structure. This
  234. % 'orient' flag would indicate to Analyze that this data should be placed in
  235. % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
  236. % being the slice direction.
  237. % For the 'transverse unflipped' type, the voxels are stored with
  238. % Pixels in 'x' axis (varies fastest) - from patient right to left
  239. % Rows in 'y' axis - from patient posterior to anterior
  240. % Slices in 'z' axis - from patient inferior to superior
  241. if verbose, fprintf('...reading axial unflipped orientation\n'); end
  242. % -- This code will handle nD files
  243. dims = double( avw.hdr.dime.dim(2:end) );
  244. % replace dimensions of 0 with 1 to be used in reshape
  245. idx = find( dims == 0 );
  246. dims( idx ) = 1;
  247. avw.img = reshape( tmp, dims );
  248. % -- The code above replaces this
  249. % avw.img = zeros(PixelDim,RowDim,SliceDim);
  250. %
  251. % n = 1;
  252. % x = 1:PixelDim;
  253. % for z = 1:SliceDim,
  254. % for y = 1:RowDim,
  255. % % load Y row of X values into Z slice avw.img
  256. % avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  257. % n = n + PixelDim;
  258. % end
  259. % end
  260. % no need to rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  261. case 1, % coronal unflipped
  262. % orient = 1: The primary orientation of the data on disk is in the coronal
  263. % plane relative to the object scanned. Most commonly, the fastest moving
  264. % index through the voxels that are part of this coronal image would span the
  265. % right-left extent of the structure imaged, with the next fastest moving
  266. % index spanning the inferior-superior extent of the structure. This 'orient'
  267. % flag would indicate to Analyze that this data should be placed in the X-Z
  268. % plane of the 3D Analyze Coordinate System, with the Y dimension being the
  269. % slice direction.
  270. % For the 'coronal unflipped' type, the voxels are stored with
  271. % Pixels in 'x' axis (varies fastest) - from patient right to left
  272. % Rows in 'z' axis - from patient inferior to superior
  273. % Slices in 'y' axis - from patient posterior to anterior
  274. if verbose, fprintf('...reading coronal unflipped orientation\n'); end
  275. avw.img = zeros(PixelDim,SliceDim,RowDim);
  276. n = 1;
  277. x = 1:PixelDim;
  278. for y = 1:SliceDim,
  279. for z = 1:RowDim,
  280. % load Z row of X values into Y slice avw.img
  281. avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  282. n = n + PixelDim;
  283. end
  284. end
  285. % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  286. avw.hdr.dime.dim(2:4) = int16([PixelDim,SliceDim,RowDim]);
  287. avw.hdr.dime.pixdim(2:4) = single([PixelSz,SliceSz,RowSz]);
  288. case 2, % sagittal unflipped
  289. % orient = 2: The primary orientation of the data on disk is in the sagittal
  290. % plane relative to the object scanned. Most commonly, the fastest moving
  291. % index through the voxels that are part of this sagittal image would span the
  292. % posterior-anterior extent of the structure imaged, with the next fastest
  293. % moving index spanning the inferior-superior extent of the structure. This
  294. % 'orient' flag would indicate to Analyze that this data should be placed in
  295. % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
  296. % being the slice direction.
  297. % For the 'sagittal unflipped' type, the voxels are stored with
  298. % Pixels in 'y' axis (varies fastest) - from patient posterior to anterior
  299. % Rows in 'z' axis - from patient inferior to superior
  300. % Slices in 'x' axis - from patient right to left
  301. if verbose, fprintf('...reading sagittal unflipped orientation\n'); end
  302. avw.img = zeros(SliceDim,PixelDim,RowDim);
  303. n = 1;
  304. y = 1:PixelDim; % posterior to anterior (fastest)
  305. for x = 1:SliceDim, % right to left (slowest)
  306. for z = 1:RowDim, % inferior to superior
  307. % load Z row of Y values into X slice avw.img
  308. avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  309. n = n + PixelDim;
  310. end
  311. end
  312. % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  313. avw.hdr.dime.dim(2:4) = int16([SliceDim,PixelDim,RowDim]);
  314. avw.hdr.dime.pixdim(2:4) = single([SliceSz,PixelSz,RowSz]);
  315. %--------------------------------------------------------------------------------
  316. % Orient values 3-5 have the second index reversed in order, essentially
  317. % 'flipping' the images relative to what would most likely become the vertical
  318. % axis of the displayed image.
  319. %--------------------------------------------------------------------------------
  320. case 3, % transverse/axial flipped
  321. % orient = 3: The primary orientation of the data on disk is in the
  322. % transverse plane relative to the object scanned. Most commonly, the fastest
  323. % moving index through the voxels that are part of this transverse image would
  324. % span the right-left extent of the structure imaged, with the next fastest
  325. % moving index spanning the *anterior-posterior* extent of the structure. This
  326. % 'orient' flag would indicate to Analyze that this data should be placed in
  327. % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
  328. % being the slice direction.
  329. % For the 'transverse flipped' type, the voxels are stored with
  330. % Pixels in 'x' axis (varies fastest) - from patient right to Left
  331. % Rows in 'y' axis - from patient anterior to Posterior *
  332. % Slices in 'z' axis - from patient inferior to Superior
  333. if verbose, fprintf('...reading axial flipped (+Y from Anterior to Posterior)\n'); end
  334. avw.img = zeros(PixelDim,RowDim,SliceDim);
  335. n = 1;
  336. x = 1:PixelDim;
  337. for z = 1:SliceDim,
  338. for y = RowDim:-1:1, % flip in Y, read A2P file into P2A 3D matrix
  339. % load a flipped Y row of X values into Z slice avw.img
  340. avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  341. n = n + PixelDim;
  342. end
  343. end
  344. % no need to rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  345. case 4, % coronal flipped
  346. % orient = 4: The primary orientation of the data on disk is in the coronal
  347. % plane relative to the object scanned. Most commonly, the fastest moving
  348. % index through the voxels that are part of this coronal image would span the
  349. % right-left extent of the structure imaged, with the next fastest moving
  350. % index spanning the *superior-inferior* extent of the structure. This 'orient'
  351. % flag would indicate to Analyze that this data should be placed in the X-Z
  352. % plane of the 3D Analyze Coordinate System, with the Y dimension being the
  353. % slice direction.
  354. % For the 'coronal flipped' type, the voxels are stored with
  355. % Pixels in 'x' axis (varies fastest) - from patient right to Left
  356. % Rows in 'z' axis - from patient superior to Inferior*
  357. % Slices in 'y' axis - from patient posterior to Anterior
  358. if verbose, fprintf('...reading coronal flipped (+Z from Superior to Inferior)\n'); end
  359. avw.img = zeros(PixelDim,SliceDim,RowDim);
  360. n = 1;
  361. x = 1:PixelDim;
  362. for y = 1:SliceDim,
  363. for z = RowDim:-1:1, % flip in Z, read S2I file into I2S 3D matrix
  364. % load a flipped Z row of X values into Y slice avw.img
  365. avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  366. n = n + PixelDim;
  367. end
  368. end
  369. % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  370. avw.hdr.dime.dim(2:4) = int16([PixelDim,SliceDim,RowDim]);
  371. avw.hdr.dime.pixdim(2:4) = single([PixelSz,SliceSz,RowSz]);
  372. case 5, % sagittal flipped
  373. % orient = 5: The primary orientation of the data on disk is in the sagittal
  374. % plane relative to the object scanned. Most commonly, the fastest moving
  375. % index through the voxels that are part of this sagittal image would span the
  376. % posterior-anterior extent of the structure imaged, with the next fastest
  377. % moving index spanning the *superior-inferior* extent of the structure. This
  378. % 'orient' flag would indicate to Analyze that this data should be placed in
  379. % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
  380. % being the slice direction.
  381. % For the 'sagittal flipped' type, the voxels are stored with
  382. % Pixels in 'y' axis (varies fastest) - from patient posterior to Anterior
  383. % Rows in 'z' axis - from patient superior to Inferior*
  384. % Slices in 'x' axis - from patient right to Left
  385. if verbose, fprintf('...reading sagittal flipped (+Z from Superior to Inferior)\n'); end
  386. avw.img = zeros(SliceDim,PixelDim,RowDim);
  387. n = 1;
  388. y = 1:PixelDim;
  389. for x = 1:SliceDim,
  390. for z = RowDim:-1:1, % flip in Z, read S2I file into I2S 3D matrix
  391. % load a flipped Z row of Y values into X slice avw.img
  392. avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
  393. n = n + PixelDim;
  394. end
  395. end
  396. % rearrange avw.hdr.dime.dim or avw.hdr.dime.pixdim
  397. avw.hdr.dime.dim(2:4) = int16([SliceDim,PixelDim,RowDim]);
  398. avw.hdr.dime.pixdim(2:4) = single([SliceSz,PixelSz,RowSz]);
  399. otherwise
  400. error('unknown value in avw.hdr.hist.orient, try explicit IMGorient option.');
  401. end
  402. if verbose, t=toc; fprintf('...done (%5.2f sec).\n\n',t); end
  403. return
  404. % This function attempts to read the orientation of the
  405. % Analyze file according to the hdr.hist.orient field of the
  406. % header. Unfortunately, this field is optional and not
  407. % all programs will set it correctly, so there is no guarantee,
  408. % that the data loaded will be correctly oriented. If necessary,
  409. % experiment with the 'orient' option to read the .img
  410. % data into the 3D matrix of avw.img as preferred.
  411. %
  412. % (Conventions gathered from e-mail with support@AnalyzeDirect.com)
  413. %
  414. % 0 transverse unflipped
  415. % X direction first, progressing from patient right to left,
  416. % Y direction second, progressing from patient posterior to anterior,
  417. % Z direction third, progressing from patient inferior to superior.
  418. % 1 coronal unflipped
  419. % X direction first, progressing from patient right to left,
  420. % Z direction second, progressing from patient inferior to superior,
  421. % Y direction third, progressing from patient posterior to anterior.
  422. % 2 sagittal unflipped
  423. % Y direction first, progressing from patient posterior to anterior,
  424. % Z direction second, progressing from patient inferior to superior,
  425. % X direction third, progressing from patient right to left.
  426. % 3 transverse flipped
  427. % X direction first, progressing from patient right to left,
  428. % Y direction second, progressing from patient anterior to posterior,
  429. % Z direction third, progressing from patient inferior to superior.
  430. % 4 coronal flipped
  431. % X direction first, progressing from patient right to left,
  432. % Z direction second, progressing from patient superior to inferior,
  433. % Y direction third, progressing from patient posterior to anterior.
  434. % 5 sagittal flipped
  435. % Y direction first, progressing from patient posterior to anterior,
  436. % Z direction second, progressing from patient superior to inferior,
  437. % X direction third, progressing from patient right to left.
  438. %----------------------------------------------------------------------------
  439. % From ANALYZE documentation...
  440. %
  441. % The ANALYZE coordinate system has an origin in the lower left
  442. % corner. That is, with the subject lying supine, the coordinate
  443. % origin is on the right side of the body (x), at the back (y),
  444. % and at the feet (z). This means that:
  445. %
  446. % +X increases from right (R) to left (L)
  447. % +Y increases from the back (posterior,P) to the front (anterior, A)
  448. % +Z increases from the feet (inferior,I) to the head (superior, S)
  449. %
  450. % The LAS orientation is the radiological convention, where patient
  451. % left is on the image right. The alternative neurological
  452. % convention is RAS (also Talairach convention).
  453. %
  454. % A major advantage of the Analzye origin convention is that the
  455. % coordinate origin of each orthogonal orientation (transverse,
  456. % coronal, and sagittal) lies in the lower left corner of the
  457. % slice as it is displayed.
  458. %
  459. % Orthogonal slices are numbered from one to the number of slices
  460. % in that orientation. For example, a volume (x, y, z) dimensioned
  461. % 128, 256, 48 has:
  462. %
  463. % 128 sagittal slices numbered 1 through 128 (X)
  464. % 256 coronal slices numbered 1 through 256 (Y)
  465. % 48 transverse slices numbered 1 through 48 (Z)
  466. %
  467. % Pixel coordinates are made with reference to the slice numbers from
  468. % which the pixels come. Thus, the first pixel in the volume is
  469. % referenced p(1,1,1) and not at p(0,0,0).
  470. %
  471. % Transverse slices are in the XY plane (also known as axial slices).
  472. % Sagittal slices are in the ZY plane.
  473. % Coronal slices are in the ZX plane.
  474. %
  475. %----------------------------------------------------------------------------
  476. %----------------------------------------------------------------------------
  477. % E-mail from support@AnalyzeDirect.com
  478. %
  479. % The 'orient' field in the data_history structure specifies the primary
  480. % orientation of the data as it is stored in the file on disk. This usually
  481. % corresponds to the orientation in the plane of acquisition, given that this
  482. % would correspond to the order in which the data is written to disk by the
  483. % scanner or other software application. As you know, this field will contain
  484. % the values:
  485. %
  486. % orient = 0 transverse unflipped
  487. % 1 coronal unflipped
  488. % 2 sagittal unflipped
  489. % 3 transverse flipped
  490. % 4 coronal flipped
  491. % 5 sagittal flipped
  492. %
  493. % It would be vary rare that you would ever encounter any old Analyze 7.5
  494. % files that contain values of 'orient' which indicate that the data has been
  495. % 'flipped'. The 'flipped flag' values were really only used internal to
  496. % Analyze to precondition data for fast display in the Movie module, where the
  497. % images were actually flipped vertically in order to accommodate the raster
  498. % paint order on older graphics devices. The only cases you will encounter
  499. % will have values of 0, 1, or 2.
  500. %
  501. % As mentioned, the 'orient' flag only specifies the primary orientation of
  502. % data as stored in the disk file itself. It has nothing to do with the
  503. % representation of the data in the 3D Analyze coordinate system, which always
  504. % has a fixed representation to the data. The meaning of the 'orient' values
  505. % should be interpreted as follows:
  506. %
  507. % orient = 0: The primary orientation of the data on disk is in the
  508. % transverse plane relative to the object scanned. Most commonly, the fastest
  509. % moving index through the voxels that are part of this transverse image would
  510. % span the right-left extent of the structure imaged, with the next fastest
  511. % moving index spanning the posterior-anterior extent of the structure. This
  512. % 'orient' flag would indicate to Analyze that this data should be placed in
  513. % the X-Y plane of the 3D Analyze Coordinate System, with the Z dimension
  514. % being the slice direction.
  515. %
  516. % orient = 1: The primary orientation of the data on disk is in the coronal
  517. % plane relative to the object scanned. Most commonly, the fastest moving
  518. % index through the voxels that are part of this coronal image would span the
  519. % right-left extent of the structure imaged, with the next fastest moving
  520. % index spanning the inferior-superior extent of the structure. This 'orient'
  521. % flag would indicate to Analyze that this data should be placed in the X-Z
  522. % plane of the 3D Analyze Coordinate System, with the Y dimension being the
  523. % slice direction.
  524. %
  525. % orient = 2: The primary orientation of the data on disk is in the sagittal
  526. % plane relative to the object scanned. Most commonly, the fastest moving
  527. % index through the voxels that are part of this sagittal image would span the
  528. % posterior-anterior extent of the structure imaged, with the next fastest
  529. % moving index spanning the inferior-superior extent of the structure. This
  530. % 'orient' flag would indicate to Analyze that this data should be placed in
  531. % the Y-Z plane of the 3D Analyze Coordinate System, with the X dimension
  532. % being the slice direction.
  533. %
  534. % Orient values 3-5 have the second index reversed in order, essentially
  535. % 'flipping' the images relative to what would most likely become the vertical
  536. % axis of the displayed image.
  537. %
  538. % Hopefully you understand the difference between the indication this 'orient'
  539. % flag has relative to data stored on disk and the full 3D Analyze Coordinate
  540. % System for data that is managed as a volume image. As mentioned previously,
  541. % the orientation of patient anatomy in the 3D Analyze Coordinate System has a
  542. % fixed orientation relative to each of the orthogonal axes. This orientation
  543. % is completely described in the information that is attached, but the basics
  544. % are:
  545. %
  546. % Left-handed coordinate system
  547. %
  548. % X-Y plane is Transverse
  549. % X-Z plane is Coronal
  550. % Y-Z plane is Sagittal
  551. %
  552. % X axis runs from patient right (low X) to patient left (high X)
  553. % Y axis runs from posterior (low Y) to anterior (high Y)
  554. % Z axis runs from inferior (low Z) to superior (high Z)
  555. %
  556. %----------------------------------------------------------------------------
  557. %----------------------------------------------------------------------------
  558. % SPM2 NOTES from spm2 webpage: One thing to watch out for is the image
  559. % orientation. The proper Analyze format uses a left-handed co-ordinate
  560. % system, whereas Talairach uses a right-handed one. In SPM99, images were
  561. % flipped at the spatial normalisation stage (from one co-ordinate system
  562. % to the other). In SPM2b, a different approach is used, so that either a
  563. % left- or right-handed co-ordinate system is used throughout. The SPM2b
  564. % program is told about the handedness that the images are stored with by
  565. % the spm_flip_analyze_images.m function and the defaults.analyze.flip
  566. % parameter that is specified in the spm_defaults.m file. These files are
  567. % intended to be customised for each site. If you previously used SPM99
  568. % and your images were flipped during spatial normalisation, then set
  569. % defaults.analyze.flip=1. If no flipping took place, then set
  570. % defaults.analyze.flip=0. Check that when using the Display facility
  571. % (possibly after specifying some rigid-body rotations) that:
  572. %
  573. % The top-left image is coronal with the top (superior) of the head displayed
  574. % at the top and the left shown on the left. This is as if the subject is viewed
  575. % from behind.
  576. %
  577. % The bottom-left image is axial with the front (anterior) of the head at the
  578. % top and the left shown on the left. This is as if the subject is viewed from above.
  579. %
  580. % The top-right image is sagittal with the front (anterior) of the head at the
  581. % left and the top of the head shown at the top. This is as if the subject is
  582. % viewed from the left.
  583. %----------------------------------------------------------------------------