current position:Home>Complete image cutting using Python version VTK

Complete image cutting using Python version VTK

2022-01-31 04:56:02 Pie star's keyboard

This is my participation 11 The fourth of the yuegengwen challenge 5 God , Check out the activity details :2021 One last more challenge

This article introduces the use of VTK Of Python Version finish the cutting of the surface drawing model

Introduction to the modules that will be used

1、 Read two-dimensional picture sequence and finish surface drawing

For details, see Read 2D sequence display

2、vtk.vtkOutlineFilter() Introduce

This space is equivalent to generating the outline of the rendered model , For example, the three-dimensional image size is (256x256x200), Then this control will generate a control with length, width and height respectively 256x256x200 A cuboid frame 1636183600(1).jpg

Detailed introduction :VTK Official documents

3、 Implicit function plane module vtk.vtkImplicitPlaneWidget()

Using this module, the plane to be selected can be flexibly adjusted vtkImplicitPlaneWidget Official documents

1636183830(1).jpg

4、vtk.vtkClipPolyData()

vtkclippolydata The shear results of , It is divided into upper and lower parts according to the normal of the tangent plane , There are corresponding output interfaces in the interface

vtkClipPolyData Official documents

Cutting effect display

1636183904(1).jpg

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 the picture 、 Face painting 
    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()
    
# Face drawing code , For details, see use python-vtk Finish drawing the article 
    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 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)
    # relation CallBack function 
    implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
    implicitPlaneWidget.On()


    renWin.Render()
    iren.Initialize()
    iren.Start()

#CallBack function 
def my_call_back(pWidget,ev):
# Said when pWidget When the control changes , Trigger function 
    if (pWidget):
        print(pWidget.GetClassName(), "Event Id:", ev)
        planeNew = vtk.vtkPlane()
        # get pWidget The plane in , Assign the plane value planeNew
        pWidget.GetPlane(planeNew)
        #cliper Clip the clipper cliper The plane of is set 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 code 

copyright notice
author[Pie star's keyboard],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310456006189.html

Random recommended