简体   繁体   English

如何使用pyvtk将numpy数组中的3D矢量字段导出到* .vtk文件?

[英]How to export 3D vector field from numpy array to *.vtk-file using pyvtk?

I'm struggling to export some 3D vector-arrays (numpy arrays) from python to a *.vtk-file for later use in ParaView. 我正在努力将一些3D矢量数组(numpy数组)从python导出到* .vtk文件,以便以后在ParaView中使用。

I have three 3D MR-Velocimetry Images, each of 100x100x200 voxels containing the velocity components in x, y and z. 我有三个3D MR测速图像,每个100x100x200体素包含x,y和z中的速度分量。 What I want is to export this vector field to a *.vtk-file using the pyvtk-module from here . 我想要的是使用pyvtk-module从这里将向量字段导出到* .vtk文件。

Unfortunately I don't understand how it works :-( 不幸的是,我不明白它是如何工作的:-(

What I tried so far: 到目前为止我尝试过的是:

from pyvtk import *

vectors = [flow['vx'], flow['vy'], flow['vz']]

dim=flow['vx'].shape

pointdata=PointData(Vectors([flow['vx'],flow['vy'],flow['vz']]))

vtk=VtkData(StructuredPoints(dim[0],dim[1],dim[2]), pointdata)

Where flow['...'] contains the vector-components. 其中flow ['...']包含矢量分量。 I got the following error: 我收到以下错误:

ValueError: DataSet (size=100) and PointData (size=3) have different sizes

Yeeees, what is it trying to tell me? Yeeees,它想告诉我什么? Ok, guess something like dimension mismatch, but how do I set up my input properly? 好的,猜想类似尺寸不匹配的问题,但是如何正确设置输入呢?

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance 提前致谢

I found a proper solution for my problem, using TVTK instead of PyVTK. 我使用TVTK而不是PyVTK找到了解决问题的合适方法。 So everyone who is interested, a possible workaround could be as follows: 因此,每个感兴趣的人,可能的解决方法如下:

from tvtk.api import tvtk, write_data
# Unpack velocity information

vx=flow['vx']
vy=flow['vy']
vz=flow['vz']

dim=vx.shape

# Generate the grid
xx,yy,zz=np.mgrid[0:dim[0],0:dim[1],0:dim[2]]
pts = empty(vx.shape + (3,), dtype=int)
pts[..., 0] = xx
pts[..., 1] = yy
pts[..., 2] = zz

vectors = empty(vx.shape + (3,), dtype=float)
vectors[..., 0] = vx
vectors[..., 1] = vy
vectors[..., 2] = vz

# We reorder the points and vectors so this is as per VTK's
# requirement of x first, y next and z last.
pts = pts.transpose(2, 1, 0, 3).copy()
pts.shape = pts.size // 3, 3

vectors = vectors.transpose(2, 1, 0, 3).copy()
vectors.shape = vectors.size // 3, 3

sg = tvtk.StructuredGrid(dimensions=xx.shape, points=pts)

sg.point_data.vectors = vectors
sg.point_data.vectors.name = 'velocity'

write_data(sg, 'vtktest.vtk')

Greetings 问候

Phtagen 光气

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM