使用Python+VTK实现三维模型的显示和切割(面绘制)

本文将介绍使用VTK的Python版本完成面绘制已经模型的切割

会使用的模块介绍
1、读取二维图片序列完成面绘制
详情见读取二维序列显示

2、vtk.vtkOutlineFilter()介绍

这个空间就相当于生成渲染模型的轮廓线,比如三维图像大小为(256x256x200),那么这个控件就会生成一个长宽高分别为256想x256x200的一个长方体框架
使用Python+VTK实现三维模型的显示和切割(面绘制)_第1张图片
详细介绍:VTK官方文档

3、隐函数平面模块vtk.vtkImplicitPlaneWidget()
使用该模块可以灵活的调整需要选取的平面
vtkImplicitPlaneWidget官方文档
使用Python+VTK实现三维模型的显示和切割(面绘制)_第2张图片
4、vtk.vtkClipPolyData()
vtkclippolydata的剪切结果,根据切平面法线分为上下两部分,接口中有相应的输出接口

vtkClipPolyData官方文档

切割效果展示

使用Python+VTK实现三维模型的显示和切割(面绘制)_第3张图片

代码如下:

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()
#读取图片、面绘制
    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()
    
#面绘制代码,详情见使用python-vtk完成面绘制文章
    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)
#定义一个图像边界控件
    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);
#定义切割器
    global cliper
    cliper = vtk.vtkClipPolyData()
    cliper.SetInputData(skinStripper.GetOutput())
#定义平面隐函数
    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)
    #关联CallBack函数
    implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
    implicitPlaneWidget.On()


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

#CallBack函数
def my_call_back(pWidget,ev):
#表示当pWidget控件改变时,触发函数
    if (pWidget):
        print(pWidget.GetClassName(), "Event Id:", ev)
        planeNew = vtk.vtkPlane()
        #获得pWidget中的平面,将平面值赋值planeNew
        pWidget.GetPlane(planeNew)
        #cliper将裁剪器cliper的平面设置为planeNew
        cliper.SetClipFunction(planeNew)
        planeNew.GetNormal()
        cliper.Update();
        #将裁减后的模型传递给另一个窗口
        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()



你可能感兴趣的:(python图像处理,python,计算机视觉,vtk,opengl,人工智能)