ow to convert vtkImageData to numpy for a RGB image

I want to convert a vtkImageData (3 components) to numpy.

For a vtkImageData with 1 components, the following code works:

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)

However, when the number of components is 3, the above code provide a wrong image. How to fix it?

In addition, how can I covert the RGB numpy to QImage?

Hi,

The following works for me:

    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)

I think this forum may not be the best for this.

Thank you very much!

You can use qimage2ndarray as described here.