VTK vtkDataSet 到 3D numpy 数组并返回

VTK vtkDataSet to 3D numpy array and back

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

到目前为止我所做的是使用 vtk.util.numpy_support vtk_to_numpy 将点的坐标保存到一个 numpy 数组中,这不是我需要的。但是,我需要一个 3D numpy 矩阵来表示它的体积渲染。 说到张量,我想出了如何以及在何处将我的 9 元素张量保存到文件中。我只是不确定如何正确设置它以与这些点相关。 最后一步,即 3D numpy 数组到 vtk,使用 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)

这可能对您有用: https://github.com/marcomusy/vedo/blob/master/vedo/examples/volumetric/numpy2volume1.py

并使用例如

检索 numpy 对象

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