Hi all,
I am fairly new to VTK. I am using VTK version 8.1.2, Python 3.7.5 on Windows 10. I am trying to clip a VTK image with Python similar to the screenshot below taken from Slicer:
Below is the code that I manage to put together, however I have the feeling that I am missing something conceptual in VTK library because the code is hanging on line 23 (clipper.Update()) without any errors.
import vtk
renderer = vtk.vtkRenderer()
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window_interaction = vtk.vtkRenderWindowInteractor()
render_window_interaction.SetRenderWindow(render_window)
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName('.\\sample.vtk')
reader.Update()
plane = vtk.vtkPlane()
plane.SetOrigin(0.25, 0, 0)
plane.SetNormal(-1, -1, 0)
clipper = vtk.vtkClipVolume()
clipper.SetInputConnection(reader.GetOutputPort())
clipper.SetClipFunction(plane)
clipper.GenerateClipScalarsOn()
clipper.GenerateClippedOutputOn()
clipper.SetValue(0.5)
clipper.Update()
volume_mapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
volume_mapper.SetInputConnection(clipper.GetOutputPort())
volume_mapper.SetBlendModeToComposite()
volume_color = vtk.vtkColorTransferFunction()
volume_color.AddRGBPoint(0, 0.0, 0.0, 0.0)
volume_color.AddRGBPoint(500, 1.0, 0.5, 0.3)
volume_color.AddRGBPoint(1000, 1.0, 0.5, 0.3)
volume_color.AddRGBPoint(1150, 1.0, 1.0, 0.9)
volume_scalar_opacity = vtk.vtkPiecewiseFunction()
volume_scalar_opacity.AddPoint(0, 0.00)
volume_scalar_opacity.AddPoint(500, 0.15)
volume_scalar_opacity.AddPoint(1000, 0.15)
volume_scalar_opacity.AddPoint(1150, 0.85)
volume_gradient_opacity = vtk.vtkPiecewiseFunction()
volume_gradient_opacity.AddPoint(0, 0.0)
volume_gradient_opacity.AddPoint(90, 0.5)
volume_gradient_opacity.AddPoint(100, 1.0)
volume_property = vtk.vtkVolumeProperty()
volume_property.SetColor(volume_color)
volume_property.SetScalarOpacity(volume_scalar_opacity)
volume_property.SetGradientOpacity(volume_gradient_opacity)
volume_property.SetInterpolationTypeToLinear()
volume_property.ShadeOn()
volume_property.SetAmbient(0.4)
volume_property.SetDiffuse(0.6)
volume_property.SetSpecular(0.2)
volume = vtk.vtkVolume()
volume.SetMapper(volume_mapper)
volume.SetProperty(volume_property)
renderer.AddViewProp(volume)
camera = renderer.GetActiveCamera()
center = volume.GetCenter()
camera.SetFocalPoint(center[0], center[1], center[2])
camera.SetPosition(center[0] + 400, center[1], center[2])
camera.SetViewUp(0, 0, -1)
render_window.SetSize(640, 480)
render_window_interaction.Initialize()
render_window.Render()
render_window_interaction.Start()
Looking forward for your help!