flip_lr.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. % When you load any ANALYZE or NIfTI file with 'load_nii.m', and view
  2. % it with 'view_nii.m', you may find that the image is L-R flipped.
  3. % This is because of the confusion of radiological and neurological
  4. % convention in the medical image before NIfTI format is adopted. You
  5. % can find more details from:
  6. %
  7. % http://www.rotman-baycrest.on.ca/~jimmy/UseANALYZE.htm
  8. %
  9. % Sometime, people even want to convert RAS (standard orientation) back
  10. % to LAS orientation to satisfy the legend programs or processes. This
  11. % program is only written for those purpose. So PLEASE BE VERY CAUTIOUS
  12. % WHEN USING THIS 'FLIP_LR.M' PROGRAM.
  13. %
  14. % With 'flip_lr.m', you can convert any ANALYZE or NIfTI (no matter
  15. % 3D or 4D) file to a flipped NIfTI file. This is implemented simply
  16. % by flipping the affine matrix in the NIfTI header. Since the L-R
  17. % orientation is determined there, so the image will be flipped.
  18. %
  19. % Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance],[preferredForm])
  20. %
  21. % original_fn - filename of the original ANALYZE or NIfTI (3D or 4D) file
  22. %
  23. % flipped_fn - filename of the L-R flipped NIfTI file
  24. %
  25. % old_RGB (optional) - a scale number to tell difference of new RGB24
  26. % from old RGB24. New RGB24 uses RGB triple sequentially for each
  27. % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect
  28. % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for
  29. % each slices. If the image that you view is garbled, try to set
  30. % old_RGB variable to 1 and try again, because it could be in
  31. % old RGB24. It will be set to 0, if it is default or empty.
  32. %
  33. % tolerance (optional) - distortion allowed for non-orthogonal rotation
  34. % or shearing in NIfTI affine matrix. It will be set to 0.1 (10%),
  35. % if it is default or empty.
  36. %
  37. % preferredForm (optional) - selects which transformation from voxels
  38. % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate
  39. % "prefer sform or qform, but use others if preferred not present".
  40. % Upper case indicate the program is forced to use the specificied
  41. % tranform or fail loading. 'preferredForm' will be 's', if it is
  42. % default or empty. - Jeff Gunter
  43. %
  44. % Example: flip_lr('avg152T1_LR_nifti.nii', 'flipped_lr.nii');
  45. % flip_lr('avg152T1_RL_nifti.nii', 'flipped_rl.nii');
  46. %
  47. % You will find that 'avg152T1_LR_nifti.nii' and 'avg152T1_RL_nifti.nii'
  48. % are the same, and 'flipped_lr.nii' and 'flipped_rl.nii' are also the
  49. % the same, but they are L-R flipped from 'avg152T1_*'.
  50. %
  51. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  52. %
  53. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  54. %
  55. function flip_lr(original_fn, flipped_fn, old_RGB, tolerance, preferredForm)
  56. if ~exist('original_fn','var') | ~exist('flipped_fn','var')
  57. error('Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance])');
  58. end
  59. if ~exist('old_RGB','var') | isempty(old_RGB)
  60. old_RGB = 0;
  61. end
  62. if ~exist('tolerance','var') | isempty(tolerance)
  63. tolerance = 0.1;
  64. end
  65. if ~exist('preferredForm','var') | isempty(preferredForm)
  66. preferredForm= 's'; % Jeff
  67. end
  68. nii = load_nii(original_fn, [], [], [], [], old_RGB, tolerance, preferredForm);
  69. M = diag(nii.hdr.dime.pixdim(2:5));
  70. M(1:3,4) = -M(1:3,1:3)*(nii.hdr.hist.originator(1:3)-1)';
  71. M(1,:) = -1*M(1,:);
  72. nii.hdr.hist.sform_code = 1;
  73. nii.hdr.hist.srow_x = M(1,:);
  74. nii.hdr.hist.srow_y = M(2,:);
  75. nii.hdr.hist.srow_z = M(3,:);
  76. save_nii(nii, flipped_fn);
  77. return; % flip_lr