如何解决如何从VTP XML文件读取数据
我正在尝试从VTKFile type =“ PolyData”的.vtp文件读取数据
在标准文本编辑器中打开的.vtp文件看起来像
<?xml version="1.0"?>
<VTKFile type="PolyData" version="0.1" ... >
<PolyData>
<Piece NumberOfPoints="99686" NumberOfVerts="0" NumberOfLines="0"
NumberOfStrips="0" NumberOfPolys="199368" >
<PointData>
<DataArray type="UInt8" Name="name1" format="appended" ... />
<DataArray type="Int16" Name="name2" RangeMax="589" .../>
</PointData>
<CellData>
</CellData>
<Points>
<DataArray type="Float32" Name="Points" NumberOfComponents="3" ... />
</Points>
<Verts>
我可以通过以下方式读取文件:
reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName(path_to_file)
reader.Update()
polyDataOutput = reader.GetOutput()
我可以通过numpy数组访问所有点的坐标
from vtk.util.numpy_support import vtk_to_numpy
polydata = reader.GetOutput()
points = polydata.GetPoints()
array = points.GetData()
point_coordinates = vtk_to_numpy(array)
,但仅适用于vtk == 8.1.2,而不适用于vtk == 9.x
现在,我需要获取隐藏在PointData“ name1”和“ name2”中的数据作为numpy数组。我可以通过以下方式访问PointData
pointData = polydata.GetPointData()
但是当我尝试使用vtk_to_numpy进行转换时,我得到了错误
AttributeError: 'vtkCommonDataModelPython.vtkPointData' object has no attribute 'GetDataType'
如何以numpy数组或pyhton列表访问PointData的数据?
解决方法
name1 = pointData.GetArray('name1')
name1_array = vtk_to_numpy(name1)
似乎可以达成协议。
,这时您的代码正确,并且读取了数据并将其包含在vtkPolyData类型的polyDataOutput
变量中,有关c ++ doc https://vtk.org/doc/nightly/html/classvtkPolyData-members.html的信息,请参见此处(Python与同一API一样,请参见:{{ 3}})
然后,vtk代表Visualization Toolkit
,因此,如果通过显示表示3d视图,请在此处查看:
https://gitlab.kitware.com/vtk/vtk/-/blob/master/Wrapping/Python/README.md
对于numpy集成,可以使用dataset_adaptor
模块:https://lorensen.github.io/VTKExamples/site/Python/IO/ReadPolyData/。
您还可以使用vtk_to_numpy
模块(from vtk.util.numpy_support import vtk_to_numpy
)将数据数组转换为numpy
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。