Help Needed with VTK and ParaView: Converting, Saving, and Rendering 3D Numpy Array

Hi everyone,

I’m a beginner seeking guidance on using VTK and ParaView. I have a specific problem and would appreciate your help.

Objective:

  1. Convert a 3D numpy array to a VTI file.
  2. Save the VTI file to the hard drive.
  3. Display the 3D volume in ParaView.
    3.1. Ideally, utilize the GPU for enhanced rendering.

Data:

  1. The 3D volume (numpy array) comprises grayscale images.
  2. Image dimensions: 1028 x 1028.
  3. The numpy array is created by stacking consecutive images.

System Details:

  • OS: Pop OS 22.04
  • GPU: Nvidia Geforce 3050 ti
  • Kernel: 6.6.7-1-liquorix-amd64 (latest version)

Problem:
Objectives 1 and 2 work as intended using the code provided below. However, ParaView encounters issues rendering my volume. Unless my code or the way I try to render the data is faulty.

Anyways, upon loading the data and changing the representation from outline to volume, I receive the following error message:

ERROR: In ./VTK/Rendering/VolumeOpenGL2/vtkVolumeTexture.cxx, line 948
vtkVolumeTexture (0x55d97d59e950): Capabilities check via proxy texture 3D allocation failed!

Which leaves me with a transparent outline of a box with white borders.

Code for Numpy to VTK Conversion & Local Saving:

Code insipiration comes from this post: here

import vtk
import vtk.util.numpy_support as numpy_support

def numpyToVTK(data, output_file):
    data_type = vtk.VTK_FLOAT
    shape = data.shape

    flat_data_array = data.flatten()
    vtk_data = numpy_support.numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)

    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])

    # Save the VTK file
    writer = vtk.vtkXMLImageDataWriter()
    writer.SetFileName(output_file)
    writer.SetInputData(img)
    writer.Write()

if __name__ == "__main__":
    output_file = 'output_volume.vti'  # Use '.vti' extension for XML-based VTK files
    numpyToVTK(DATA, output_file)

Any help for solving my problem is highly appreciated :smiley:

Update

I found the following Post, which made clear that the error originates due to the fact that I’m exceeding the RAM/VRAM on my machine. So, after shrinking the volume size I was able to load the data in Paraview as expected.

Current display

Remaining problem:
The data doesn’t display the true volume representation, which opens the question if it’s a) the fault of my python script and how I convert the images or b) I’m missing some additional steps I need to perform in the Paraview software.

What makes you say that ?

1 Like

Perhaps my expression is not quite clear, but what I’m trying to convey is that I anticipated obtaining the RGB representation, essentially having the individual images (slices of the volumes) stacked on top of each other. However, in its current state, it appears that I’m not capturing all the features of the volume.

Please share the resulting test.vti

1 Like

The test.vti and an example image of how the individual slices look like are accessible via this link

test.vti contains only a scalar and no RGB data

1 Like

Again, I’ve mispoken. Sorry for the confusion. I’m interested in the grayscale representation, and not the RGB, since all the individual slices are in the grayscale color format. Hence, vti in scalar format.

So you mean something like this ?

Thanks for the code example create vtk image date from numpy array. I just want to mention that the flatten should be in column-major (Fortran-style) order. i.e. data.flatten(order='F'). Otherwise, the data would be disturbed.

I’m having the same issue with having a representation of a full 3d box instead of the volume represented in my binary numpy array, as if the entirety of the numpy array is foreground and i’ve checked as there are no issues with the numpy. Any new ideas on how to fix this?