python vtk画线的三种方式

1 篇文章 0 订阅

准备数据:

p0 = [0.0, 0.0, 0.0]
p1 = [0.0, 1.0, 0.0]
p2 = [1.0, 0.0, 0.0]
p3 = [1.0, 1.0, 0.0]

一,LineSource:画两个点的线

def createLine1():
    lineSource = vtk.vtkLineSource()
    lineSource.SetPoint1(p1)
    lineSource.SetPoint2(p2)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(lineSource.GetOutputPort())
    return mapper

二,LineSource 多点连续直线

def createLine2():
    lineSource = vtk.vtkLineSource()
    points = vtk.vtkPoints()
    points.InsertNextPoint(p0)
    points.InsertNextPoint(p1)
    points.InsertNextPoint(p2)
    points.InsertNextPoint(p3)
    lineSource.SetPoints(points)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(lineSource.GetOutputPort())
    return mapper

三,LineSource 多点设置几何结构+拓扑结构

通过拓扑结构来设置数据中那些点连在一起

def createLine3():
    # Create a vtkPoints object and store the points in it
    points = vtk.vtkPoints()
    points.InsertNextPoint(p0)
    points.InsertNextPoint(p1)
    points.InsertNextPoint(p2)
    points.InsertNextPoint(p3)

    # Create a cell array to store the lines in and add the lines to it
    lines = vtk.vtkCellArray()

    for i in range(0, 3, 2):
        line = vtk.vtkLine()
        line.GetPointIds().SetId(0, i)
        line.GetPointIds().SetId(1, i + 1)
        lines.InsertNextCell(line)

    # Create a polydata to store everything in
    linesPolyData = vtk.vtkPolyData()

    # Add the points to the dataset         几何结构
    linesPolyData.SetPoints(points)

    # Add the lines to the dataset          拓扑结构
    linesPolyData.SetLines(lines)

    # Setup actor and mapper
    colors = vtk.vtkNamedColors()

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(linesPolyData)

    return mapper

完整代码

import vtk

# Visualize
colors = vtk.vtkNamedColors()
# Create points
p0 = [0.0, 0.0, 0.0]
p1 = [0.0, 1.0, 0.0]
p2 = [1.0, 0.0, 0.0]
p3 = [1.0, 1.0, 0.0]


# LineSource:画两个点的线
def createLine1():
    lineSource = vtk.vtkLineSource()
    lineSource.SetPoint1(p1)
    lineSource.SetPoint2(p2)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(lineSource.GetOutputPort())
    return mapper


# LineSource 多点连续直线
def createLine2():
    lineSource = vtk.vtkLineSource()
    points = vtk.vtkPoints()
    points.InsertNextPoint(p0)
    points.InsertNextPoint(p1)
    points.InsertNextPoint(p2)
    points.InsertNextPoint(p3)
    lineSource.SetPoints(points)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(lineSource.GetOutputPort())
    return mapper


# LineSource 多点设置几何结构+拓扑结构
def createLine3():
    # Create a vtkPoints object and store the points in it
    points = vtk.vtkPoints()
    points.InsertNextPoint(p0)
    points.InsertNextPoint(p1)
    points.InsertNextPoint(p2)
    points.InsertNextPoint(p3)

    # Create a cell array to store the lines in and add the lines to it
    lines = vtk.vtkCellArray()

    for i in range(0, 3, 2):
        line = vtk.vtkLine()
        line.GetPointIds().SetId(0, i)
        line.GetPointIds().SetId(1, i + 1)
        lines.InsertNextCell(line)

    # Create a polydata to store everything in
    linesPolyData = vtk.vtkPolyData()

    # Add the points to the dataset         几何结构
    linesPolyData.SetPoints(points)

    # Add the lines to the dataset          拓扑结构
    linesPolyData.SetLines(lines)

    # Setup actor and mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(linesPolyData)
    return mapper

def main():
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Line")
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
    # Visualize
    colors = vtk.vtkNamedColors()
    renderer.SetBackground(colors.GetColor3d("Silver"))

    actor = vtk.vtkActor()
    # 第一种方式
    # actor.SetMapper(createLine1())
    # 第二种方式
    # actor.SetMapper(createLine2())
    # 第三种方式
    actor.SetMapper(createLine3())

    actor.GetProperty().SetLineWidth(4)
    actor.GetProperty().SetColor(colors.GetColor3d("Peacock"))
    renderer.AddActor(actor)


    renderWindow.Render()
    renderWindowInteractor.Start()


if __name__ == '__main__':
    main()


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VTK(Visualization Toolkit)是一个用于处理和可视化科学数据的开源软件系统,它可用于图像三维重建。下面是基本的流程: 1. 导入所需模块 ```python import vtk ``` 2. 读取DICOM文件 ```python reader = vtk.vtkDICOMImageReader() reader.SetDirectoryName("Your DICOM directory path") reader.Update() ``` 3. 设置渲染器和窗口 ```python ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) ``` 4. 创建体绘制算法 ```python volumeMapper = vtk.vtkSmartVolumeMapper() volumeMapper.SetInputConnection(reader.GetOutputPort()) ``` 5. 创建体绘制属性 ```python volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(vtk.vtkColorTransferFunction()) volumeProperty.SetScalarOpacity(vtk.vtkPiecewiseFunction()) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() ``` 6. 设置体绘制属性 ```python # 设置颜色和透明度 colorFunc = vtk.vtkColorTransferFunction() colorFunc.AddRGBPoint(-3024, 0.0, 0.0, 0.0) colorFunc.AddRGBPoint(-77, 0.54902, 0.25098, 0.14902) colorFunc.AddRGBPoint(94, 0.882353, 0.603922, 0.290196) colorFunc.AddRGBPoint(179, 1, 0.937033, 0.954531) colorFunc.AddRGBPoint(3071, 1, 1, 1) volumeProperty.SetColor(colorFunc) # 设置不透明度 opacityFunc = vtk.vtkPiecewiseFunction() opacityFunc.AddPoint(-3024, 0.0) opacityFunc.AddPoint(-77, 0.0) opacityFunc.AddPoint(94, 0.29) opacityFunc.AddPoint(179, 0.55) opacityFunc.AddPoint(3071, 0.55) volumeProperty.SetScalarOpacity(opacityFunc) ``` 7. 创建体绘制Actor ```python volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) ren.AddActor(volume) ``` 8. 启动渲染器和窗口 ```python ren.SetBackground(0.1, 0.2, 0.4) renWin.SetSize(600, 600) iren.Initialize() renWin.Render() iren.Start() ``` 以上是基本的流程,具体实现中还需根据数据类型和需求进行相应的调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值