Guest User

Untitled

a guest
Dec 11th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. from __future__ import print_function
  2. import sys
  3. from sys import argv
  4. from numpy import *
  5.  
  6. def usage():
  7. print("Usage: " + argv[0] + " <input2standard mat> <output mat>")
  8. print(" ")
  9. print(" First argument is the FLIRT transform (12 DOF) from the input image to standard")
  10. print(" Second argument is the output matrix which will go from the input image to standard space (6 DOF)")
  11. print(" aligning the AC, the AC-PC line and the mid-sagittal plane (in order of decreasing accuracy)")
  12. sys.exit(1)
  13.  
  14. if len(argv) < 2:
  15. usage()
  16.  
  17. # Load in the necessary info
  18. a=loadtxt(argv[1])
  19. # set specific AC and PC coordinates in FLIRT convention (x1=AC, x2=PC, x3=point above x1 in the mid-sag plane)
  20. x1=matrix([[91],[129],[67],[1]])
  21. x2=matrix([[91],[100],[70],[1]])
  22. x3=matrix([[91],[129],[117],[1]])
  23.  
  24. ainv=linalg.inv(a)
  25.  
  26. # vectors v are in MNI space, vectors w are in native space
  27. v21=(x2-x1)
  28. v31=(x3-x1)
  29. # normalise and force orthogonality
  30. v21=v21/linalg.norm(v21)
  31. v31=v31-multiply(v31.T * v21,v21)
  32. v31=v31/linalg.norm(v31)
  33. tmp=cross(v21[0:3,0].T,v31[0:3,0].T).T
  34. v41=mat(zeros((4,1)))
  35. v41[0:3,0]=tmp
  36. # Map vectors to native space
  37. w21=ainv*(v21)
  38. w31=ainv*(v31)
  39. # normalise and force orthogonality
  40. w21=w21/linalg.norm(w21)
  41. w31=w31-multiply(w31.T * w21,w21)
  42. w31=w31/linalg.norm(w31)
  43. tmp=cross(w21[0:3,0].T,w31[0:3,0].T).T
  44. w41=mat(zeros((4,1)))
  45. w41[0:3,0]=tmp
  46.  
  47. # setup matrix: native to MNI space
  48. r1=matrix(eye(4))
  49. r1[0:4,0]=w21
  50. r1[0:4,1]=w31
  51. r1[0:4,2]=w41
  52. r2=matrix(eye(4))
  53. r2[0,0:4]=v21.T
  54. r2[1,0:4]=v31.T
  55. r2[2,0:4]=v41.T
  56. r=r2.T*r1.T
  57.  
  58. # Fix the translation (keep AC=x1 in the same place)
  59. ACmni=x1
  60. ACnat=ainv*x1
  61. trans=ACmni-r*ACnat
  62. r[0:3,3]=trans[0:3]
  63.  
  64. # Save out the result
  65. savetxt(argv[2],r,fmt='%14.10f')
Add Comment
Please, Sign In to add comment