[Python] How to convert 3D Numpy array into greyscale vtkImageData to use in pyQT

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a 3D numpy array which I am trying to convert into a vtk object to use within pyQT. The code that I have works in that it is showing the volume, but I need the volume to be in greyscale. Can anyone please point me towards what I need to add, or if I am going about this the complete wrong way. Apologies I can not provide array that I am working on as is patient data.

    First step is reading the data in and converting to VTK ImageData

    f = h5py.File(FILE_PATH, 'r')

    data = pull_data_from_hdf5(f, "volume", [49, 496, 512])
    data = np.asarray(data)

    data_shape = data.shape
    data_shape = data_shape[::-1]
    data_vtk = numpy_support.numpy_to_vtk(num_array=data.ravel(), deep=True, array_type=vtk.VTK_FLOAT)

    image_vtk = vtk.vtkImageData()
    image_vtk.SetDimensions(data_shape)
    image_vtk.SetSpacing(np.array([1.0, 1.0, 2.0]))
    image_vtk.GetPointData().SetScalars(data_vtk)


    Then within my pyQT QMainWindow __init()__ I have

    ...
    # Convert the image to a polydata
    imageDataGeometryFilter = vtk.vtkImageDataGeometryFilter()
    imageDataGeometryFilter.SetInputData(image_vtk)
    imageDataGeometryFilter.Update()

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(imageDataGeometryFilter.GetOutputPort())

    # Create an actor -
    # vtkActor is used to represent an entity in a rendering scene
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    ...


    The output in the QT window is the 3D numpy array, which I can rotate etc as expected by the colours are not what I would like. I want them to be converted to greyscale.

    Continue reading...

Compartilhe esta Página