使用Python从vtk文件中的数据绘制轮廓图

 西红柿 发布于 2023-01-29 21:15

我有一组存储在VTK文件中的数据,它代表一个带有数组中标量点数据的域的切割.我正在尝试生成所述标量的等高线图,使其看起来有点像使用ParaView制作的附图.我宁愿坚持使用vtk库而不是使用其他东西,比如Matplotlib,因为我认为它们会产生更好的可视化效果.预览我想要实现的目标.

我在网上看过几个例子,但没有一个对我有效(没有错误被抛出,我最终得到的只是背景的空渲染),所有我能做的就是表面图数据(例如:这里).

这是我所拥有的代码的当前版本(非常类似于成功生成曲面图的代码):

# import data
reader = vtk.vtkDataSetReader()
reader.SetFileName('inputDataFiles/k_zCut.vtk')
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()

# access data
data = reader.GetOutput()
d = data.GetPointData()
array=d.GetArray('k')

# create the filter
contours = vtk.vtkContourFilter()
contours.SetInput(reader.GetOutput())
contours.GenerateValues(5,1.,5.)

# create the mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(contours.GetOutput())
mapper.ScalarVisibilityOff()
mapper.SetScalarRange(1., 5.)

# create the actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# create a rendering window and renderer
ren = vtk.vtkRenderer()
ren.SetBackground( 0.329412, 0.34902, 0.427451 ) #Paraview blue

# Assign actor to the renderer
ren.AddActor(actor)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(750, 750)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# render
renWin.Render()

# screenshot
w2if = vtk.vtkWindowToImageFilter()
w2if.SetInput(renWin)
w2if.Update()
w2if.SetMagnification(5.)

writer = vtk.vtkPNGWriter()
writer.SetFileName("screenshot.png")
writer.SetInput(w2if.GetOutput())
writer.Write()

# Enable user interface interactor
iren.Initialize()
iren.Start()

下面你可以看到我的输入文件的缩短部分.任何帮助都感激不尽.

# vtk DataFile Version 2.0
sampleSurface
ASCII
DATASET POLYDATA
POINTS 34813 float
0 0 0
0 -0.000191589 0
0.000264399 0.000157061 0
0 0.000313389 0
0.000264347 -0.000191923 0
0 -0.000383178 0
-0.000395709 0 0
-0.000395709 0.000156695 0
3.60174e-05 0.000486922 0
0.000528387 0 0

POLYGONS 69284 277136
3 4105 4371 3861
3 4102 3861 4371
3 4656 4371 4373
3 4105 4373 4371
3 3624 3861 3390
3 3621 3390 3861
3 4105 3863 3861
3 3624 3861 3863
3 3188 3390 2990
3 3187 2990 3390
3 3624 3390 3391
3 3188 3391 3390

POINT_DATA 34813
FIELD attributes 1
k 1 34813 float
0.849464 0.391519 1.52947 1.05206 0.391519 0.253736 1.39481 1.39481 0.636517 1.21019
0.640193 0.114295 1.12557 0.644143 0.629569 0.114295 0.485032 0.477396 1.39961 0.0860201
1.66665 1.24058 1.45939 0.483719 1.01318 0.163198 0.317574 0.792821 0.317125 0.658835

Joe Kington.. 6

如果您想对VTK使用更"pythonic"的接口,请考虑使用mayavi/ tvtk/mlab.(无论哪种方式,VTK都是一个很好的选择!)

tvtk 是一个稍微更加pythonic,低级,python绑定到VTK与一些非常好的功能(例如透明使用numpy数组).

Mayavi和mlab为VTK提供了更高级别的接口.

您显示的数据文件的片段原样无效,但如果我们使用类似的:

# vtk DataFile Version 2.0
Simple VTK file example
ASCII

DATASET POLYDATA
POINTS 9 float
3.0 0.0 0.0
1.0 1.0 0.0
0.0 3.0 0.0
3.0 0.0 1.0
1.0 1.0 1.0
0.0 3.0 1.0
3.0 2.0 2.0
2.0 2.0 2.0
2.0 3.0 2.0

TRIANGLE_STRIPS 2 14
6 0 3 1 4 2 5
6 3 6 4 7 5 8

POINT_DATA 9
SCALARS nodal float
LOOKUP_TABLE default
0.0 0.1 0.0 0.3 0.6 0.3 0.8 1.0 0.8

在表面上渲染轮廓可以很简单:

from mayavi import mlab

source = mlab.pipeline.open('test.vtk')
lines = mlab.pipeline.contour_surface(source)

mlab.show()

在此输入图像描述

或者我们可以有点发烧友:

from mayavi import mlab

# Make a figure with a black background
fig = mlab.figure(bgcolor=(0,0,0))
# Also see methods like: fig.scene.z_plus_view(), etc
fig.scene.camera.azimuth(215)

source = mlab.pipeline.open('test.vtk')
# Show the surface, colored by the scalars
surf = mlab.pipeline.surface(source)
# Draw contours of the scalars on the surface
lines = mlab.pipeline.contour_surface(source)

mlab.show()

在此输入图像描述

1 个回答
  • 如果您想对VTK使用更"pythonic"的接口,请考虑使用mayavi/ tvtk/mlab.(无论哪种方式,VTK都是一个很好的选择!)

    tvtk 是一个稍微更加pythonic,低级,python绑定到VTK与一些非常好的功能(例如透明使用numpy数组).

    Mayavi和mlab为VTK提供了更高级别的接口.

    您显示的数据文件的片段原样无效,但如果我们使用类似的:

    # vtk DataFile Version 2.0
    Simple VTK file example
    ASCII
    
    DATASET POLYDATA
    POINTS 9 float
    3.0 0.0 0.0
    1.0 1.0 0.0
    0.0 3.0 0.0
    3.0 0.0 1.0
    1.0 1.0 1.0
    0.0 3.0 1.0
    3.0 2.0 2.0
    2.0 2.0 2.0
    2.0 3.0 2.0
    
    TRIANGLE_STRIPS 2 14
    6 0 3 1 4 2 5
    6 3 6 4 7 5 8
    
    POINT_DATA 9
    SCALARS nodal float
    LOOKUP_TABLE default
    0.0 0.1 0.0 0.3 0.6 0.3 0.8 1.0 0.8
    

    在表面上渲染轮廓可以很简单:

    from mayavi import mlab
    
    source = mlab.pipeline.open('test.vtk')
    lines = mlab.pipeline.contour_surface(source)
    
    mlab.show()
    

    在此输入图像描述

    或者我们可以有点发烧友:

    from mayavi import mlab
    
    # Make a figure with a black background
    fig = mlab.figure(bgcolor=(0,0,0))
    # Also see methods like: fig.scene.z_plus_view(), etc
    fig.scene.camera.azimuth(215)
    
    source = mlab.pipeline.open('test.vtk')
    # Show the surface, colored by the scalars
    surf = mlab.pipeline.surface(source)
    # Draw contours of the scalars on the surface
    lines = mlab.pipeline.contour_surface(source)
    
    mlab.show()
    

    在此输入图像描述

    2023-01-29 21:24 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有