• vtk对二维图像构造三维


    1. import vtk
    2. def main():
    3. arender = vtk.vtkRenderer()
    4. arender.SetViewport(0, 0.0, 0.5, 1.0)
    5. renWin = vtk.vtkRenderWindow()
    6. renWin.AddRenderer(arender)
    7. iren = vtk.vtkRenderWindowInteractor()
    8. iren.SetRenderWindow(renWin)
    9. # Reader = vtk.vtkMetaImageReader()
    10. # Reader.SetFileName("bbb.mhd")
    11. # Reader.Update()
    12. # 读取图片、面绘制
    13. Reader = vtk.vtkPNGReader()
    14. Reader.SetNumberOfScalarComponents(1)
    15. Reader.GetOutput().GetOrigin()
    16. Reader.SetDataByteOrderToLittleEndian()
    17. Reader.SetFileDimensionality(3)
    18. Reader.SetDataExtent(0, 480, 0, 480, 0,70)
    19. Reader.SetFilePrefix("./patient3/patient_")
    20. Reader.SetFilePattern("%s%d.jpg")
    21. Reader.SetDataSpacing(1, 1, 15) # Volume Pixel
    22. Reader.Update()
    23. # 面绘制代码,详情见使用python-vtk完成面绘制文章
    24. skinExtractor = vtk.vtkContourFilter()
    25. skinExtractor.SetInputConnection(Reader.GetOutputPort())
    26. skinExtractor.SetValue(0, 1)
    27. skinExtractor.ComputeGradientsOn();
    28. skinExtractor.ComputeScalarsOn();
    29. smooth = vtk.vtkSmoothPolyDataFilter()
    30. smooth.SetInputConnection(skinExtractor.GetOutputPort())
    31. smooth.SetNumberOfIterations(100)
    32. skinNormals = vtk.vtkPolyDataNormals()
    33. skinNormals.SetInputConnection(smooth.GetOutputPort())
    34. skinNormals.SetFeatureAngle(50)
    35. skinStripper = vtk.vtkStripper()
    36. skinStripper.SetInputConnection(skinNormals.GetOutputPort())
    37. skinMapper = vtk.vtkPolyDataMapper()
    38. skinMapper.SetInputConnection(skinStripper.GetOutputPort())
    39. skinMapper.ScalarVisibilityOff()
    40. skin = vtk.vtkActor()
    41. skin.SetMapper(skinMapper)
    42. # 定义一个图像边界控件
    43. outlineData = vtk.vtkOutlineFilter()
    44. outlineData.SetInputConnection(Reader.GetOutputPort())
    45. mapOutline = vtk.vtkPolyDataMapper()
    46. mapOutline.SetInputConnection(outlineData.GetOutputPort())
    47. outline = vtk.vtkActor()
    48. outline.SetMapper(mapOutline)
    49. outline.GetProperty().SetColor(0, 0, 0)
    50. aCamera = vtk.vtkCamera()
    51. aCamera.SetViewUp(0, 0, -1)
    52. aCamera.SetPosition(0, 1, 0)
    53. aCamera.ComputeViewPlaneNormal()
    54. aCamera.Azimuth(30.0)
    55. aCamera.Elevation(30.0)
    56. aCamera.Dolly(1.5)
    57. arender.AddActor(outline)
    58. arender.AddActor(skin)
    59. # splineActor.GetProperty().SetLineWidth(5)
    60. # arender.AddActor(splineActor)
    61. # arender.AddActor(pointActor)
    62. arender.SetActiveCamera(aCamera)
    63. arender.ResetCamera()
    64. arender.SetBackground(.2, .3, .4)
    65. arender.ResetCameraClippingRange()
    66. renWin.SetSize(1000, 1000)
    67. style = vtk.vtkInteractorStyleTrackballCamera()
    68. iren.SetInteractorStyle(style);
    69. # 定义切割器
    70. global cliper
    71. cliper = vtk.vtkClipPolyData()
    72. cliper.SetInputData(skinStripper.GetOutput())
    73. # 定义平面隐函数
    74. implicitPlaneWidget = vtk.vtkImplicitPlaneWidget()
    75. implicitPlaneWidget.SetInteractor(iren)
    76. implicitPlaneWidget.SetPlaceFactor(1.25)
    77. implicitPlaneWidget.SetInputData(skinStripper.GetOutput())
    78. implicitPlaneWidget.PlaceWidget()
    79. global coneSkinActor
    80. coneSkinActor = vtk.vtkActor()
    81. coneSkinActor.SetMapper(skinMapper)
    82. rRenderer = vtk.vtkRenderer()
    83. rRenderer.SetBackground(0.2, 0.3, 0.5)
    84. rRenderer.SetViewport(0.5, 0.0, 1.0, 1.0)
    85. coneSkinActor.RotateZ(90)
    86. rRenderer.AddActor(coneSkinActor)
    87. renWin.AddRenderer(rRenderer)
    88. # 关联CallBack函数
    89. implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
    90. implicitPlaneWidget.On()
    91. renWin.Render()
    92. iren.Initialize()
    93. iren.Start()
    94. # CallBack函数
    95. def my_call_back(pWidget, ev):
    96. # 表示当pWidget控件改变时,触发函数
    97. if (pWidget):
    98. print(pWidget.GetClassName(), "Event Id:", ev)
    99. planeNew = vtk.vtkPlane()
    100. # 获得pWidget中的平面,将平面值赋值planeNew
    101. pWidget.GetPlane(planeNew)
    102. # cliper将裁剪器cliper的平面设置为planeNew
    103. cliper.SetClipFunction(planeNew)
    104. planeNew.GetNormal()
    105. cliper.Update();
    106. # 将裁减后的模型传递给另一个窗口
    107. clipedData = vtk.vtkPolyData()
    108. clipedData.DeepCopy(cliper.GetOutput())
    109. coneMapper = vtk.vtkPolyDataMapper()
    110. coneMapper.SetInputData(clipedData)
    111. coneMapper.ScalarVisibilityOff()
    112. coneSkinActor.SetMapper(coneMapper)
    113. print("Plane Normal = " + str(planeNew.GetNormal()))
    114. print("Plane Origin = " + str(planeNew.GetOrigin()))
    115. main()

  • 相关阅读:
    WSA工具箱安装应用商店提示无法工作怎么解决?
    benchmark
    Rust You Don’t Know
    旋转框目标检测mmrotate v0.3.1入门
    【数据集处理工具】将COCO格式数据集的val.json与tett.json文件合并为一个json
    LLM生态下爬虫程序的现状与未来
    y137.第八章 Servless和Knative从入门到精通 -- Serverless概念和基础(一)
    vue3 el-form中嵌套el-tabale 对输入动态校验
    OPENCV实现DNN图像分类
    Linux(CentOS-7)-全面详解(学习总结---从入门到深化)
  • 原文地址:https://blog.csdn.net/caochunxiao1/article/details/122260749