This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
This article describes cutting the surface rendering model using the Python version of VTK
This section describes the modules that will be used
1. Read 2d image sequence to complete surface rendering
See read 2d sequence display for details
Vtk.vtkoutlinefilter (
This space is equivalent to generating the contour line of the rendering model. For example, if the size of the 3D image is (256x256x200), then this control will generate a rectangular frame with length, width and height 256x256x200 respectively
Details: VTK official documentation
3, the implicit function graphic module VTK. VtkImplicitPlaneWidget ()
Use this module to flexibly adjust the selected plane vtkImplicitPlaneWidget official document
4, VTK. VtkClipPolyData ()
The shear result of VTKclipPolydata is divided into upper and lower parts according to the tangent plane normals, with corresponding output interface in the interface
VtkClipPolyData official document
Cutting effect display
The code is as follows:
import vtk
def main() :
arender = vtk.vtkRenderer()
arender.SetViewport(0.0.0.0.5.1.0)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(arender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Reader = vtk.vtkMetaImageReader()
# Reader.SetFileName("bbb.mhd")
# Reader.Update()
# Read pictures, surface drawing
Reader = vtk.vtkPNGReader()
Reader.SetNumberOfScalarComponents(1)
Reader.GetOutput().GetOrigin()
Reader.SetDataByteOrderToLittleEndian()
Reader.SetFileDimensionality(3)
Reader.SetDataExtent(0.512.0.512.0.226)
Reader.SetFilePrefix("E:/qct_data/in_out_data/in_data/inner/label/22/")
#Reader.SetFilePrefix("C:/Users/deng5/Desktop/2/48/")
Reader.SetFilePattern("%s%d.png")
Reader.SetDataSpacing(1.1.1) # Volume Pixel
Reader.Update()
See the python-vtk Complete surface rendering article for more details
skinExtractor = vtk.vtkContourFilter()
skinExtractor.SetInputConnection(Reader.GetOutputPort())
skinExtractor.SetValue(0.1)
skinExtractor.ComputeGradientsOn();
skinExtractor.ComputeScalarsOn();
smooth = vtk.vtkSmoothPolyDataFilter()
smooth.SetInputConnection(skinExtractor.GetOutputPort())
smooth.SetNumberOfIterations(100)
skinNormals = vtk.vtkPolyDataNormals()
skinNormals.SetInputConnection(smooth.GetOutputPort())
skinNormals.SetFeatureAngle(50)
skinStripper = vtk.vtkStripper()
skinStripper.SetInputConnection(skinNormals.GetOutputPort())
skinMapper = vtk.vtkPolyDataMapper()
skinMapper.SetInputConnection(skinStripper.GetOutputPort())
skinMapper.ScalarVisibilityOff()
skin = vtk.vtkActor()
skin.SetMapper(skinMapper)
Define an image boundary control
outlineData = vtk.vtkOutlineFilter()
outlineData.SetInputConnection(Reader.GetOutputPort())
mapOutline = vtk.vtkPolyDataMapper()
mapOutline.SetInputConnection(outlineData.GetOutputPort())
outline = vtk.vtkActor()
outline.SetMapper(mapOutline)
outline.GetProperty().SetColor(0.0.0)
aCamera = vtk.vtkCamera()
aCamera.SetViewUp(0.0, -1)
aCamera.SetPosition(0.1.0)
aCamera.ComputeViewPlaneNormal()
aCamera.Azimuth(30.0)
aCamera.Elevation(30.0)
aCamera.Dolly(1.5)
arender.AddActor(outline)
arender.AddActor(skin)
#splineActor.GetProperty().SetLineWidth(5)
#arender.AddActor(splineActor)
#arender.AddActor(pointActor)
arender.SetActiveCamera(aCamera)
arender.ResetCamera()
arender.SetBackground(2..3..4.)
arender.ResetCameraClippingRange()
renWin.SetSize(1000.1000)
style = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style);
Define the cutter
global cliper
cliper = vtk.vtkClipPolyData()
cliper.SetInputData(skinStripper.GetOutput())
# Define the plane implicit function
implicitPlaneWidget = vtk.vtkImplicitPlaneWidget()
implicitPlaneWidget.SetInteractor(iren)
implicitPlaneWidget.SetPlaceFactor(1.25)
implicitPlaneWidget.SetInputData(skinStripper.GetOutput())
implicitPlaneWidget.PlaceWidget()
global coneSkinActor
coneSkinActor = vtk.vtkActor()
coneSkinActor.SetMapper(skinMapper)
rRenderer = vtk.vtkRenderer()
rRenderer.SetBackground(0.2.0.3.0.5)
rRenderer.SetViewport(0.5.0.0.1.0.1.0)
coneSkinActor.RotateZ(90)
rRenderer.AddActor(coneSkinActor)
renWin.AddRenderer(rRenderer)
# correlation CallBack function
implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
implicitPlaneWidget.On()
renWin.Render()
iren.Initialize()
iren.Start()
# CallBack function
def my_call_back(pWidget,ev) :
# indicates that the function is fired when the pWidget control changes
if (pWidget):
print(pWidget.GetClassName(), "Event Id:", ev)
planeNew = vtk.vtkPlane()
Get the plane in the pWidget and assign the plane value planeNew
pWidget.GetPlane(planeNew)
#cliper Sets the plane of clipper cliper to planeNew
cliper.SetClipFunction(planeNew)
planeNew.GetNormal()
cliper.Update();
Pass the trimmed model to another window
clipedData = vtk.vtkPolyData()
clipedData.DeepCopy(cliper.GetOutput())
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputData(clipedData)
coneMapper.ScalarVisibilityOff()
coneSkinActor.SetMapper(coneMapper)
print("Plane Normal = "+str(planeNew.GetNormal()))
print("Plane Origin = "+str(planeNew.GetOrigin()))
main()
Copy the code