Numpy to vtk

Hello,

I am able to visualize a volume in vtk of a 3D image using the mask function and volume rendering. I do have two visualizations:

  1. Correct visualized: Original image and binary masked image of expert (both imported immediatly in VTK)
  2. Incorrect visualized: Original image and binary segmented image myself (original image in VTK, binary 3D numpy array imported from pickle file).
    However, the visualization of 2) is not shown correctly. The datapoints are not fully connected to eachother as I think my original maskvolume is an int8 (unsigned char).

Do you have any idea how to solve this problem? Either my np array is incorrect (or the transpose) or how I translate the np to VTK is incorrect.

Some code:

def nptovtk(data_matrix):
# https://vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy
dataImporter = vtk.vtkImageImport()
# The preaviusly created array is converted to a string of chars and imported.
data_string = data_matrix.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
# The type of the newly imported data is set to unsigned char (uint8)
dataImporter.SetDataScalarTypeToUnsignedChar()
# Because the data that is imported only contains an intensity value (it isnt RGB-coded or someting similar), the importer
# must be told this is the case.
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.SetDataExtent(0, 140, 0, 114, 0, 150)
dataImporter.SetWholeExtent(0, 140, 0, 114, 0, 150)
dataImporter.SetDataSpacing(0.240224, 0.240224, 0.240204)

dataImporter.SetDataOrigin(0, 0, 0)
return dataImporter

np_connected_thres = load_datanumpy(text=“connected_thres”)

I tried different transpose options

np_connected_thres2 = np.transpose(np_connected_thres, (2, 1, 0))
np_connected_thres3 = np_connected_thres.transpose(2, 1, 0).ravel(order=‘F’)
np_connected_thres4 = np_connected_thres.transpose(2, 1, 0).flatten(order=‘C’)
vtk_connected_thres = nptovtk(np_connected_thres2)

Thanks in advance.