【Python】VTK: 三维图像处理和可视化利器

VTK在Java, Tcl, Python中都有接口,本文介绍VTK在Python中的接口

VTK (The Visualization Toolkit ) 是一款用于计算机3D成像、构建模型、图像处理、容积渲染、科学数据可视化等方面的免费软件系统,支持Linux, Windows, Mac, Web, mobile devices等平台;

一些常见使用场景👇👇

4661ec90910f171df7f6df6198644b1f.png
3D成像
7236ea09e77cef6ae1d7a697c391c930.png
图像处理1
bd6ac9fb261d1b0506a282857cc983be.png
图像处理2
ce73cfa17c064081ea2945ab1513eb17.png
构建模型
4e0582aa8e2d80d84b3b305b59d1a084.png
容积渲染

下面简单介绍VTK如何快速上手,

安装

pip install vtk

快速入门VTK

以一个小为例,

import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkCylinderSource
from vtkmodules.vtkRenderingCore import (vtkActor, vtkPolyDataMapper,
                                         vtkRenderWindow,
                                         vtkRenderWindowInteractor,
                                         vtkRenderer)
# 数据准备
colors = vtkNamedColors()
bkg = map(lambda x: x / 255.0, [26, 51, 102, 255])
colors.SetColor("BkgColor", *bkg)
cylinder = vtkCylinderSource()
cylinder.SetResolution(8)

# 映射
cylinderMapper = vtkPolyDataMapper()
cylinderMapper.SetInputConnection(cylinder.GetOutputPort())

# 添加绘制对象
cylinderActor = vtkActor()

#绘制对象添加映射器
cylinderActor.SetMapper(cylinderMapper)
cylinderActor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
cylinderActor.RotateX(30.0)
cylinderActor.RotateY(-45.0)

# 添加绘制器
ren = vtkRenderer()

# 添加绘制窗口
renWin = vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# 绘制器添加对象
ren.AddActor(cylinderActor)
ren.SetBackground(colors.GetColor3d("BkgColor"))
renWin.SetSize(300, 300)
renWin.SetWindowName('CylinderExample')

# 交互器初始化
iren.Initialize()
ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.5)
renWin.Render()

# 交互器启动
iren.Start()

154b0aa067ec3fcb153822296945cfbd.png进一步学习👇👇
https://kitware.github.io/vtk-examples/site/Python/


-END-

48cd7f2bb2e500774146cc518b2bdfc9.png
 
 
 
 
 
 
 
 
往期精彩回顾




适合初学者入门人工智能的路线及资料下载机器学习及深度学习笔记等资料打印机器学习在线手册深度学习笔记专辑《统计学习方法》的代码复现专辑
AI基础下载黄海广老师《机器学习课程》视频课黄海广老师《机器学习课程》711页完整版课件

本站qq群554839127,加入微信群请扫码:

685a778f551c2ce7cd5c13c0d442e216.png

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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() ``` 以上是基本的流程,具体实现中还需根据数据类型和需求进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值