VTK vtkDataSet到3D numpy数组并返回

问题描述 投票:0回答:1

我正在迈向vtk的第一步,由于缺乏文档,我非常挣扎。我有一个.vtk文件,它是我尚未创建的vtkDataSet类型的对象。我需要导出它的内容,并将其转换为3D numpy矩阵,对其进行自定义及其张量,然后将所有内容写入vtkDataSet对象和.vtk文件。

到目前为止,我使用vtk.util.numpy_support vtk_to_numpy将点的坐标(不是我需要的)保存到numpy数组中。但是,我需要一个3D numpy矩阵来表示其体积渲染。谈到张量时,我想出了将9元素张量保存到文件中的方式和位置。我只是不确定如何正确设置它以与要点相关。最后一步是vtk的3D numpy数组,使用来自vtk.util.numpy_support的numpy.ravel和numpy_to_vtk看起来可行。这是一些我正在测试的代码:

# reader for mrtrix vtk file
reader = vtk.vtkDataSetReader()
file_name = 'my_file.vtk'
reader.SetFileName(file_name)
reader.Update()

# get the vtkDataArray
data_set = reader.GetOutput()
# these are the coordinates of the points
# I'd need the 3D numpy volume rendering matrix instead
point_array = data_set.GetPoints().GetData()

# test tensor
# I'd need to save a tensor for every element of the 3D numpy matrix
tensor = numpy_to_vtk(np.zeros([data_set.GetNumberOfPoints(), 9]))
tensor.SetName('Tensors_')
point_data = data_set.GetPointData()
point_data.SetAttribute(tensor, 4)
python numpy vtk
1个回答
0
投票

这在您的情况下可能有用:https://github.com/marcomusy/vtkplotter/blob/master/examples/volumetric/numpy2volume.py

并以例如方式检索numpy对象

print('numpy array from Volume:', vol.getPointArray().shape)

© www.soinside.com 2019 - 2024. All rights reserved.