我想将vtkImageData(3个组件)转换为numpy。
对于具有1个组件的vtkImageData,以下代码有效:
temp = vtk_to_numpy(data.GetPointData().GetScalars())
dims = data.GetDimensions()
numpy_data = temp.reshape(dims[2], dims[1], dims[0])
numpy_data = numpy_data.transpose(2,1,0)
但是,当组件数为3时,以上代码提供了错误的图像。如何解决?
更新:
以下代码起作用:
img_scalar = data.GetPointData().GetScalars()
dims = data.GetDimensions()
n_comp = img_scalar.GetNumberOfComponents()
temp = numpy_support.vtk_to_numpy(img_scalar)
numpy_data = temp.reshape(dims[1],dims[0],n_comp)
numpy_data = numpy_data.transpose(0,1,2)
numpy_data = np.flipud(numpy_data)
如果图像每个像素有3个分量(可能是RGB),那么从numpy的角度来看,它实际上是一个4维数组。所以也许应该是:
numpy_data = temp.reshape(dims[2], dims[1], dims[0], 3)