|
@@ -0,0 +1,111 @@
|
|
|
+import vtk, qt, ctk, slicer
|
|
|
+import numpy as np
|
|
|
+import SimpleITK as sitk
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class vtkInterface:
|
|
|
+ def __init__(self, parent):
|
|
|
+
|
|
|
+
|
|
|
+ parent.dependencies = []
|
|
|
+ parent.title ="vtkInterface"
|
|
|
+ parent.contributors = ["Andrej Studen (FMF/JSI)"]
|
|
|
+ parent.helpText = """
|
|
|
+ Convert native numpy data structures to vtk
|
|
|
+ """
|
|
|
+ parent.acknowledgementText = """
|
|
|
+ This module was developed within the frame of the ARRS sponsored medical
|
|
|
+ physics research programe to investigate quantitative measurements of cardiac
|
|
|
+ function using sestamibi-like tracers
|
|
|
+ """ # replace with organization, grant and thanks.
|
|
|
+ self.parent = parent
|
|
|
+
|
|
|
+
|
|
|
+def numpyToVTK(numpy_array, shape, data_type=vtk.VTK_FLOAT):
|
|
|
+ v=vtk.vtkImageData()
|
|
|
+ v.GetPointData().SetScalars(
|
|
|
+ vtk.util.numpy_support.numpy_to_vtk(
|
|
|
+ np.ravel(numpy_array,order='F'),deep=True, array_type=data_type))
|
|
|
+ v.SetOrigin(0,0,0)
|
|
|
+ v.SetSpacing(1,1,1)
|
|
|
+ v.SetDimensions(shape)
|
|
|
+ return v
|
|
|
+
|
|
|
+
|
|
|
+def completeOrientation(orientation):
|
|
|
+ o=orientation
|
|
|
+ o.append(o[1]*o[5]-o[2]*o[4])
|
|
|
+ o.append(o[2]*o[3]-o[0]*o[5])
|
|
|
+ o.append(o[0]*o[4]-o[1]*o[3])
|
|
|
+ return o
|
|
|
+
|
|
|
+
|
|
|
+def ITK2VTK(img):
|
|
|
+
|
|
|
+
|
|
|
+ data=sitk.GetArrayFromImage(img)
|
|
|
+
|
|
|
+ shape=list(reversed(data.shape))
|
|
|
+ return numpyToVTK(data.ravel(),shape)
|
|
|
+
|
|
|
+def VTK2ITK(v):
|
|
|
+
|
|
|
+
|
|
|
+ scalars=v.GetPointData().GetScalars()
|
|
|
+ shape=v.GetDimensions()
|
|
|
+ data=vtk.util.numpy_support.vtk_to_numpy(scalars)
|
|
|
+
|
|
|
+ return sitk.GetImageFromArray(np.reshape(data,list(reversed(shape))))
|
|
|
+
|
|
|
+def ITKfromNode(nodeName):
|
|
|
+
|
|
|
+ node=slicer.mrmlScene.GetFirstNodeByName(nodeName)
|
|
|
+ if node==None:
|
|
|
+ print("Node {0} not available".format(nodeName))
|
|
|
+ return
|
|
|
+
|
|
|
+ img=VTK2ITK(node.GetImageData())
|
|
|
+
|
|
|
+ img.SetOrigin(node.GetOrigin())
|
|
|
+ img.SetSpacing(node.GetSpacing())
|
|
|
+ m=vtk.vtkMatrix4x4()
|
|
|
+ node.GetIJKToRASDirectionMatrix(m)
|
|
|
+ orientation=[0]*9
|
|
|
+ for i in range(0,3):
|
|
|
+ for j in range (0,3):
|
|
|
+ orientation[3*j+i]=m.GetElement(i,j)
|
|
|
+ img.SetDirection(orientation)
|
|
|
+ return img
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def ITKtoNode(img,nodeName):
|
|
|
+
|
|
|
+
|
|
|
+ node=slicer.mrmlScene.GetFirstNodeByName(nodeName)
|
|
|
+ if node==None:
|
|
|
+ node=slicer.vtkMRMLScalarVolumeNode()
|
|
|
+ node.SetName(nodeName)
|
|
|
+ slicer.mrmlScene.AddNode(node)
|
|
|
+
|
|
|
+ node.SetAndObserveImageData(ITK2VTK(img))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ spacing=img.GetSpacing()
|
|
|
+ orientation=img.GetDirection()
|
|
|
+ origin=img.GetOrigin()
|
|
|
+
|
|
|
+
|
|
|
+ ijkToRAS = vtk.vtkMatrix4x4()
|
|
|
+
|
|
|
+ for i in range(0,3):
|
|
|
+ for j in range(0,3):
|
|
|
+ ijkToRAS.SetElement(i,j,spacing[i]*orientation[3*j+i])
|
|
|
+
|
|
|
+ ijkToRAS.SetElement(i,3,origin[i])
|
|
|
+
|
|
|
+ node.SetIJKToRASMatrix(ijkToRAS)
|