我如何从vtkImageData(对应RGB图像)转换为numpy?

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

我想将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)
numpy vtk
1个回答
0
投票

如果图像每个像素有3个分量(可能是RGB),那么从numpy的角度来看,它实际上是一个4维数组。所以也许应该是:

numpy_data = temp.reshape(dims[2], dims[1], dims[0], 3)
© www.soinside.com 2019 - 2024. All rights reserved.