vtkImageExportToArray.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. vtkImageExportToArray - a NumPy front-end to vtkImageExport
  3. This class converts a VTK image to a numpy array. The output
  4. array will always have 3 dimensions (or 4, if the image had
  5. multiple scalar components).
  6. To use this class, you must have numpy installed (http://numpy.scipy.org)
  7. Methods
  8. SetInputConnection(vtkAlgorithmOutput) -- connect to VTK image pipeline
  9. SetInputData(vtkImageData) -- set an vtkImageData to export
  10. GetArray() -- execute pipeline and return a numpy array
  11. Methods from vtkImageExport
  12. GetDataExtent()
  13. GetDataSpacing()
  14. GetDataOrigin()
  15. """
  16. import numpy
  17. import numpy.core.umath as umath
  18. from vtkmodules.vtkIOImage import vtkImageExport
  19. from vtkmodules.vtkCommonExecutionModel import vtkStreamingDemandDrivenPipeline
  20. from vtkmodules.vtkCommonCore import VTK_SIGNED_CHAR
  21. from vtkmodules.vtkCommonCore import VTK_UNSIGNED_CHAR
  22. from vtkmodules.vtkCommonCore import VTK_SHORT
  23. from vtkmodules.vtkCommonCore import VTK_UNSIGNED_SHORT
  24. from vtkmodules.vtkCommonCore import VTK_INT
  25. from vtkmodules.vtkCommonCore import VTK_UNSIGNED_INT
  26. from vtkmodules.vtkCommonCore import VTK_LONG
  27. from vtkmodules.vtkCommonCore import VTK_UNSIGNED_LONG
  28. from vtkmodules.vtkCommonCore import VTK_FLOAT
  29. from vtkmodules.vtkCommonCore import VTK_DOUBLE
  30. class vtkImageExportToArray:
  31. def __init__(self):
  32. self.__export = vtkImageExport()
  33. self.__ConvertUnsignedShortToInt = False
  34. # type dictionary
  35. __typeDict = { VTK_SIGNED_CHAR:'b',
  36. VTK_UNSIGNED_CHAR:'B',
  37. VTK_SHORT:'h',
  38. VTK_UNSIGNED_SHORT:'H',
  39. VTK_INT:'i',
  40. VTK_UNSIGNED_INT:'I',
  41. VTK_FLOAT:'f',
  42. VTK_DOUBLE:'d'}
  43. __sizeDict = { VTK_SIGNED_CHAR:1,
  44. VTK_UNSIGNED_CHAR:1,
  45. VTK_SHORT:2,
  46. VTK_UNSIGNED_SHORT:2,
  47. VTK_INT:4,
  48. VTK_UNSIGNED_INT:4,
  49. VTK_FLOAT:4,
  50. VTK_DOUBLE:8 }
  51. # convert unsigned shorts to ints, to avoid sign problems
  52. def SetConvertUnsignedShortToInt(self,yesno):
  53. self.__ConvertUnsignedShortToInt = yesno
  54. def GetConvertUnsignedShortToInt(self):
  55. return self.__ConvertUnsignedShortToInt
  56. def ConvertUnsignedShortToIntOn(self):
  57. self.__ConvertUnsignedShortToInt = True
  58. def ConvertUnsignedShortToIntOff(self):
  59. self.__ConvertUnsignedShortToInt = False
  60. # set the input
  61. def SetInputConnection(self,input):
  62. return self.__export.SetInputConnection(input)
  63. def SetInputData(self,input):
  64. return self.__export.SetInputData(input)
  65. def GetInput(self):
  66. return self.__export.GetInput()
  67. def GetArray(self):
  68. self.__export.Update()
  69. input = self.__export.GetInput()
  70. extent = input.GetExtent()
  71. type = input.GetScalarType()
  72. numComponents = input.GetNumberOfScalarComponents()
  73. dim = (extent[5]-extent[4]+1,
  74. extent[3]-extent[2]+1,
  75. extent[1]-extent[0]+1)
  76. if (numComponents > 1):
  77. dim = dim + (numComponents,)
  78. imArray = numpy.zeros(dim, self.__typeDict[type])
  79. self.__export.Export(imArray)
  80. # convert unsigned short to int to avoid sign issues
  81. if (type == VTK_UNSIGNED_SHORT and self.__ConvertUnsignedShortToInt):
  82. imArray = umath.bitwise_and(imArray.astype('i'),0xffff)
  83. return imArray
  84. def GetDataExtent(self):
  85. return self.__export.GetDataExtent()
  86. def GetDataSpacing(self):
  87. return self.__export.GetDataSpacing()
  88. def GetDataOrigin(self):
  89. return self.__export.GetDataOrigin()