vtkInterface.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import vtk, qt, ctk, slicer
  2. import numpy as np
  3. import SimpleITK as sitk
  4. #set of routines to transform images from one form to another, most notably
  5. #numpy to vtk to itk and all possible combinations inbetween. Keep track of
  6. #orientation, origin and spacing between transforms
  7. class vtkInterface:
  8. def __init__(self, parent):
  9. parent.title = "vtk Interface"
  10. parent.categories = ["Examples"]
  11. parent.dependencies = []
  12. parent.contributors = ["Andrej Studen (FMF/JSI)"] # replace with "Firstname Lastname (Org)"
  13. parent.helpText = """
  14. Convert native numpy data structures to vtk
  15. """
  16. parent.acknowledgementText = """
  17. This module was developed within the frame of the ARRS sponsored medical
  18. physics research programe to investigate quantitative measurements of cardiac
  19. function using sestamibi-like tracers
  20. """ # replace with organization, grant and thanks.
  21. self.parent = parent
  22. def numpyToVTK(numpy_array, shape, data_type=vtk.VTK_FLOAT):
  23. v=vtk.vtkImageData()
  24. v.GetPointData().SetScalars(
  25. vtk.util.numpy_support.numpy_to_vtk(
  26. np.ravel(numpy_array,order='F'),deep=True, array_type=data_type))
  27. v.SetOrigin(0,0,0)
  28. v.SetSpacing(1,1,1)
  29. v.SetDimensions(shape)
  30. return v
  31. def completeOrientation(orientation):
  32. o=orientation
  33. o.append(o[1]*o[5]-o[2]*o[4])#0,3
  34. o.append(o[2]*o[3]-o[0]*o[5])#1,4
  35. o.append(o[0]*o[4]-o[1]*o[3])#2,5
  36. return o
  37. def ITK2VTK(img):
  38. #convert itk to vtk format.
  39. #get the array
  40. data=sitk.GetArrayFromImage(img)
  41. #reverse the shape (don't ask, look at vtk manual if really curios)
  42. shape=list(reversed(data.shape))
  43. return numpyToVTK(data.ravel(),shape)
  44. def VTK2ITK(v):
  45. #convert vtk image to sitk image
  46. #convert to numpy first and then go to sitk
  47. scalars=v.GetPointData().GetScalars()
  48. shape=v.GetDimensions()
  49. data=vtk.util.numpy_support.vtk_to_numpy(scalars)
  50. #now convert to sitk (notice the little reversal of the shape)
  51. return sitk.GetImageFromArray(np.reshape(data,list(reversed(shape))))
  52. def ITKfromNode(nodeName):
  53. #use node as data source and generate an itk image
  54. node=slicer.mrmlScene.GetFirstNodeByName(nodeName)
  55. if node==None:
  56. print "Node {0} not available".format(nodeName)
  57. return
  58. img=VTK2ITK(node.GetImageData())
  59. img.SetOrigin(node.GetOrigin())
  60. img.SetSpacing(node.GetSpacing())
  61. m=vtk.vtkMatrix4x4()
  62. node.GetIJKToRASDirectionMatrix(m)
  63. orientation=[0]*9
  64. for i in range(0,3):
  65. for j in range (0,3):
  66. orientation[3*j+i]=m.GetElement(i,j)
  67. img.SetDirection(orientation)
  68. return img
  69. def ITKtoNode(img,nodeName):
  70. #display itk image and assign it a volume node
  71. #useful for displaying outcomes of itk calculations
  72. node=slicer.mrmlScene.GetFirstNodeByName(nodeName)
  73. if node==None:
  74. node=slicer.vtkMRMLScalarVolumeNode()
  75. node.SetName(nodeName)
  76. slicer.mrmlScene.AddNode(node)
  77. node.SetAndObserveImageData(ITK2VTK(img))
  78. #hairy - keep orientation, spacing and origin from node and pass it to itk
  79. #for future reference
  80. spacing=img.GetSpacing()
  81. orientation=img.GetDirection()
  82. origin=img.GetOrigin()
  83. #we should get orientation, spacing and origin from somewhere
  84. ijkToRAS = vtk.vtkMatrix4x4()
  85. #think how to do this with image orientation
  86. for i in range(0,3):
  87. for j in range(0,3):
  88. ijkToRAS.SetElement(i,j,spacing[i]*orientation[3*j+i])
  89. ijkToRAS.SetElement(i,3,origin[i])
  90. node.SetIJKToRASMatrix(ijkToRAS)