首頁 > 軟體

使用Python VTK 完成影象切割

2022-04-18 16:00:10

1、讀取二維圖片序列完成面繪製

詳情見Python-VTK批次讀取二維切片並顯示三維模型

2、vtk.vtkOutlineFilter()介紹

這個空間就相當於生成渲染模型的輪廓線,比如三維影象大小為(256x256x200),那麼這個控制元件就會生成一個長寬高分別為256x256x200的一個長方體框架 

詳細介紹:VTK官方檔案

3、隱函數平面模組vtk.vtkImplicitPlaneWidget()

使用該模組可以靈活的調整需要選取的平面 vtkImplicitPlaneWidget官方檔案

4、vtk.vtkClipPolyData()

vtkclippolydata的剪下結果,根據切平面法線分為上下兩部分,介面中有相應的輸出介面

vtkClipPolyData官方檔案

切割效果展示

程式碼如下:

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 VTK 完成影象切割的文章就介紹到這了,更多相關Python VTK影象切割內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com