读取.VTK多数据文件并将其转换为Numpy数组 [英] Reading a .VTK polydata file and converting it into Numpy array

查看:135
本文介绍了读取.VTK多数据文件并将其转换为Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将.VTK ASCII多数据文件转换为仅包含点坐标的numpy数组.我首先尝试了这个方法: https://stackoverflow.com/a/11894302 ,但是它存储了一个(3,3)numpy数组,其中每个条目实际上是组成该特定像元(在这种情况下为三角形)的三个点的坐标.但是,我不需要像元,我想要每个点的坐标(无重复).接下来,我尝试了此操作: https://stackoverflow.com/a/23359921/6619666 进行了一些修改.这是我的最终代码.值不是numpy数组,而是存储为元组,但是我不确定该元组是否代表每个点.

I want to convert a .VTK ASCII polydata file into numpy array of just the coordinates of the points. I first tried this: https://stackoverflow.com/a/11894302 but it stores a (3,3) numpy array where each entry is actually the coordinates of THREE points that make that particular cell (in this case a triangle). However, I don't want the cells, I want the coordinates of each point (without repeatition). Next I tried this: https://stackoverflow.com/a/23359921/6619666 with some modifications. Here is my final code. Instead of numpy array, the values are being stored as a tuple but I am not sure if that tuple represents each point.

import sys

import numpy
import vtk
from vtk.util.numpy_support import vtk_to_numpy

reader = vtk.vtkPolyDataReader()
reader.SetFileName('Filename.vtk')
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
nodes_vtk_array= reader.GetOutput().GetPoints().GetData()
print nodes_vtk_array

请提出建议.

推荐答案

您可以使用 vtk.numpy_interface 中的 dataset_adapter :

from vtk.numpy_interface import dataset_adapter as dsa

polydata = reader.GetOutput()
numpy_array_of_points = dsa.WrapDataObject(polydata).Points

来自 Kitware博客:

可以访问PointData,CellData,FieldData,Points(仅vtkPointSet的子类),多边形(仅vtkPolyData)此方式.

It is possible to access PointData, CellData, FieldData, Points (subclasses of vtkPointSet only), Polygons (vtkPolyData only) this way.

这篇关于读取.VTK多数据文件并将其转换为Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆