unxform_nii.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. % Undo the flipping and rotations performed by xform_nii; spit back only
  2. % the raw img data block. Initial cut will only deal with 3D volumes
  3. % strongly assume we have called xform_nii to write down the steps used
  4. % in xform_nii.
  5. %
  6. % Usage: a = load_nii('original_name');
  7. % manipulate a.img to make array b;
  8. %
  9. % if you use unxform_nii to un-tranform the image (img) data
  10. % block, then nii.original.hdr is the corresponding header.
  11. %
  12. % nii.original.img = unxform_nii(a, b);
  13. % save_nii(nii.original,'newname');
  14. %
  15. % Where, 'newname' is created with data in the same space as the
  16. % original_name data
  17. %
  18. % - Jeff Gunter, 26-JUN-06
  19. %
  20. function outblock = unxform_nii(nii, inblock)
  21. if isempty(nii.hdr.hist.rot_orient)
  22. outblock=inblock;
  23. else
  24. [dummy unrotate_orient] = sort(nii.hdr.hist.rot_orient);
  25. outblock = permute(inblock, unrotate_orient);
  26. end
  27. if ~isempty(nii.hdr.hist.flip_orient)
  28. flip_orient = nii.hdr.hist.flip_orient(unrotate_orient);
  29. for i = 1:3
  30. if flip_orient(i)
  31. outblock = flipdim(outblock, i);
  32. end
  33. end
  34. end;
  35. return;