Search code examples
pythonvtk

Display a 3D image with vtk python


I want to Display a 3D image with the vtk module python. The image was saved in .vti format. I wrote the following simple code but it does not work correctly. How i can fix that?

import vtk

file_name = 'Vug.vti'

# Read the source file.
reader = vtk.vtkNrrdReader()
reader.SetFileName(file_name)
reader.Update()  

# Map the image through the lookup table
color = vtk.vtkImageMapToColors()
#color.SetLookupTable(table)
color.SetInputConnection(reader.GetOutputPort())

# Display the image
actor = vtk.vtkImageActor()
actor.GetMapper().SetInputConnection(color.GetOutputPort())

renderer = vtk.vtkRenderer()
renderer.AddActor(actor)

window = vtk.vtkRenderWindow()
window.AddRenderer(renderer)

# Set up the interaction
interactor = vtk.vtkRenderWindowInteractor()
window.SetInteractor(interactor)
window.Render()

# Start interaction
interactor.Start()

Solution

  • A vtkImageActor displays an image as a 2d object. If you want to render a 3d volume, you need to use a vtkVolume object.

    This example shows how to volume render in VTK: https://github.com/Kitware/VTK/blob/master/Examples/VolumeRendering/Python/SimpleRayCast.py