Load nifti image with vtk ()
I am trying to load NIFTI image into vtk using python. The data is already loaded using nibabel and I can render it using pyplot (1 slice per parcel, my data is ndarray). But I want to load it from ImagePlaneWidget ... something like this.
I cannot load data like imagesplanewidget wants (vtkDataSet) ... How can I do this?
import nibabel as nib
import matplotlib.pyplot as plt
import vtk
img = nib.load('image.nii.gz')
img_data = img.get_data()
print img_data.shape
def show_slices(slices):
fig,axes = plt.subplots(1, len(slices))
for i, slice in enumerate(slices):
axes[i].imshow(slice.T, cmap="gray", origin="lower")
slice_0=img_data[100, :, :]
slice_1=img_data[:, 100, :]
slice_2=img_data[:, :, 100]
show_slices([slice_0, slice_1, slice_2])
plt.suptitle("Image")
plt.show()
plane = vtk.vtkImagePlaneWidget()
plane.SetInputData(img_data)
Thanks a lot, I am new to python and vtk
+3
source to share
2 answers
And .. I can do it ...
import vtk
import nibabel as nib
img = nib.load('image.nii.gz')
img_data = img.get_data()
img_data_shape = img_data.shape
dataImporter = vtk.vtkImageImport()
dataImporter.SetDataScalarTypeToShort()
data_string = img_data.tostring()
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
dataImporter.SetDataExtent(0, img_data_shape[0] - 1, 0, img_data_shape[1] - 1, 0, img_data_shape[2] - 1)
dataImporter.SetWholeExtent(0, img_data_shape[0] - 1, 0, img_data_shape[1] - 1, 0, img_data_shape[2] - 1)
dataImporter.Update()
temp_data = dataImporter.GetOutput()
new_data = vtk.vtkImageData()
new_data.DeepCopy(temp_data)
#outline
outline=vtk.vtkOutlineFilter()
outline.SetInputData(new_data)
outlineMapper=vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
#Picker
picker = vtk.vtkCellPicker()
picker.SetTolerance(0.005)
#PlaneWidget
planeWidgetX = vtk.vtkImagePlaneWidget()
planeWidgetX.DisplayTextOn()
planeWidgetX.SetInputData(new_data)
planeWidgetX.SetPlaneOrientationToXAxes()
planeWidgetX.SetSliceIndex(100)
planeWidgetX.SetPicker(picker)
planeWidgetX.SetKeyPressActivationValue("x")
prop1 = planeWidgetX.GetPlaneProperty()
prop1.SetColor(1, 0, 0)
planeWidgetY = vtk.vtkImagePlaneWidget()
planeWidgetY.DisplayTextOn()
planeWidgetY.SetInputData(new_data)
planeWidgetY.SetPlaneOrientationToYAxes()
planeWidgetY.SetSliceIndex(100)
planeWidgetY.SetPicker(picker)
planeWidgetY.SetKeyPressActivationValue("y")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(1, 1, 0)
planeWidgetY.SetLookupTable(planeWidgetX.GetLookupTable())
planeWidgetZ = vtk.vtkImagePlaneWidget()
planeWidgetZ.DisplayTextOn()
planeWidgetZ.SetInputData(new_data)
planeWidgetZ.SetPlaneOrientationToZAxes()
planeWidgetZ.SetSliceIndex(100)
planeWidgetZ.SetPicker(picker)
planeWidgetZ.SetKeyPressActivationValue("z")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(0, 0, 1)
planeWidgetZ.SetLookupTable(planeWidgetX.GetLookupTable())
#Renderer
renderer = vtk.vtkRenderer()
renderer.SetBackground(0, 0, 1)
#RenderWindow
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)
#Add outlineactor
renderer.AddActor(outlineActor)
renwin.SetSize(800,800)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renwin)
#Load widget interactors and enable
planeWidgetX.SetInteractor(interactor)
planeWidgetX.On()
planeWidgetY.SetInteractor(interactor)
planeWidgetY.On()
planeWidgetZ.SetInteractor(interactor)
planeWidgetZ.On()
interactor.Initialize()
renwin.Render()
interactor.Start()
+4
source to share
You have two possibilities:
- Re-read the image directly from the vtk reader. I'm not sure if vtkNiftiReader is available in the current (and your) python distribution and it doesn't seem like nibabel is exporting files in a format known as vtk
- Use imageimporter to import an image from an array (I think img_data is an array), see an example at http://wiki.scipy.org/Cookbook/vtkVolumeRendering or https://github.com/cni/psych204a/blob/master /vtk_render.py
+1
source to share