将VTK转换为光栅图像(Ruby或Python)

时间:2015-04-20 19:23:38

标签: python ruby vtk raster

我在非结构化2D网格上有模拟结果。我通常将结果导出为VTK并使用Paraview将其可视化。这就是结果。

Unstructured grid results

我想从结果(有或没有插值)中获取光栅图像,以将其用作3D软件中可视化的纹理。从阅读中我已经收集到我需要进行某种重新采样,以便从非结构化网格转换为光栅图像的2d规则网格。

VTK可以导出到栅格,但它只导出一个没有任何定义边界的完整场景,因此需要手动调整才能适合图像。

理想情况下,我只想在结果边界框中导出结果,并使用Ruby或Python以编程方式将它们“映射”为栅格图像。

2 个答案:

答案 0 :(得分:0)

此脚本使用paraview并创建完美居中和缩放的图像,以便可以将其用作纹理。请注意垂直大小的855值。它似乎与屏幕的分辨率有关,根据Paraview邮件列表仅在OSX上需要。

它应该运行到Paraview Python解释器pvbatch

import sys, json
#### import the simple module from the paraview
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()


args = json.loads(sys.argv[1])
# create a new 'Legacy VTK Reader'
vtk_file = args["file"]

data = LegacyVTKReader(FileNames=[vtk_file])

# get active view
renderView1 = GetActiveViewOrCreate('RenderView')
# uncomment following to set a specific view size

xc = float(args["center"][0])
yc = float(args["center"][1])
zc = float(args["center"][2])
width = float(args["width"])
height = float(args["height"])
output_file = args["output_file"]
scalar = args["scalar"]
colormap_min = float(args["colormap_min"])
colormap_max = float(args["colormap_max"])


ratio = height / width
magnification = 2
height_p = 855 * magnification
width_p = int(height_p * 1.0 / ratio / magnification)  

renderView1.ViewSize = [width_p , height_p]

# show data in view
dataDisplay = Show(data, renderView1)
# trace defaults for the display properties.
dataDisplay.ColorArrayName = ['CELLS', scalar]
# set scalar coloring
ColorBy(dataDisplay, ('CELLS', scalar))
# rescale color and/or opacity maps used to include current data range
dataDisplay.RescaleTransferFunctionToDataRange(True)

# get color transfer function/color map for 'irradiation'
irradiationLUT = GetColorTransferFunction(scalar)
# Rescale transfer function
irradiationLUT.RescaleTransferFunction(colormap_min, colormap_max)
irradiationLUT.LockDataRange = 1
irradiationLUT.ColorSpace = 'RGB'
irradiationLUT.NanColor = [0.498039, 0.0, 0.0]

#changing interaction mode based on data extents
renderView1.InteractionMode = '2D'
renderView1.CameraPosition = [xc, yc, 10000.0 + zc]
renderView1.CameraFocalPoint = [xc, yc, zc]

# hide color bar/color legend
dataDisplay.SetScalarBarVisibility(renderView1, False)

# current camera placement for renderView1
renderView1.InteractionMode = '2D'
#renderView1.CameraPosition = [3.641002, 197.944122, 10001.75]
#renderView1.CameraFocalPoint = [3.641002, 197.944122, 1.75]
renderView1.CameraParallelScale = (height / 2.0)

# save screenshot
SaveScreenshot(output_file, magnification=magnification, quality=100, view=renderView1)

答案 1 :(得分:0)

我有一个自己动手做的解决方案。通常,我会执行以下操作:

在QGIS中将我的网格打开为多边形层,然后执行以下操作:

  1. 在QGIS(矢量/几何工具/多边形质心)中计算网格质心
  2. 右键单击新创建的图层,选择另存为,选择CSV格式,然后在“图层选项/ GEOMETRY”下选择xy或xyz

然后,使用一个简单的python脚本,将vtk数据(例如水深)与质心相关联(请注意,ParaView对与QGIS相对的节点偏移了-1,所以ParaView中的节点2是节点3在QGIS中。)

最终,再次在QGIS中,我从矢量点插入栅格,例如使用GRASSS GIS模块v.to.rast.attribute