VTK clipping with Python

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:

2020-05-20 16_41_11-Изпратени елементи - nikolay.kolev@icb.bg - Outlook

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!

Remove vtkClipVolume from your pipeline. For volume rendering, the clipping is done by the mapper:

volume_mapper.AddClippingPlane(plane)
1 Like

Also note that you can run your code in Slicer’s Python environment and then you don’t need to reimplement any existing features just add what is new or specific to your project. You can use the full Slicer GUI, a plain Python console, or Slicer Jupyter kernel (which also provides interactive 3D and slice viewers inside the notebook).

1 Like

Thank you for your replay! The screenshot from my question is actually from Slicer GUI. I was not aware that Slicer had a Python console because as I said I am fairly new. Now I found it and I will play with it.

1 Like

nikolay im working on cropping a volume set of dicom data volume with python and i need help plz i don’t
know where to start something like new vtk.js release crroping widget thanks

@dgobbi
Hi, I’m new in VTK .
I’m tryna implement clipping volume in vtk8.2 with QT(c++) on windows10 like this issue.

My code is below

VTK_NEW(vtkSmartVolumeMapper, mapper);
VTK_NEW(vtkPlane, plane);
plane->SetOrigin(m_ImageData->GetOrigin());
plane->SetNormal(-1, -1, 0);

mapper->SetInputData(m_ImageData);
mapper->AddClippingPlane(plane);

VTK_NEW(vtkVolumeProperty, volumeProperty);
volumeProperty->SetScalarOpacity(m_OpacityFunc);
volumeProperty->SetColor(m_ColorFunc);
volumeProperty->ShadeOn();
volumeProperty->SetInterpolationTypeToLinear();

m_volume->SetMapper(mapper);
m_volume->SetProperty(volumeProperty);

but it doesn’t work , I can’t find clipping plane on volume.

would you plz know how to sovle this??

Thank you.

This line is the mistake, because it places the plane at the corner of the volume:

If you want to see the plane, then place it at the center of the volume.

plane->SetOrigin(m_ImageData->GetCenter());

Thank you for reply and it is very useful!!
I checked clipped volume.
If you know implement circle which is ROI of volume , Can you know that??
I wanna control regieon which is plane position(or origin) by mouse interaction like the picture in this issue.
Thank you.