Different points after conversion from SITK image to VTK Image

I am trying to convert a SITK Image with its corresponding spacing, origin, and direction metadata into a VTK Image, with a conversion to a numpy array in between.

However, when I try to grab a point within the VTK Image ( for reference, ( 0, 0, 0 ) ), I am finding a discrepancy of where that point is located. Is there something that I’m not doing properly when converting to the VTK Image?

image = vtkImageData()
image.SetDimensions( sitkDimensions )
image.SetSpacing( sitkSpacing )
image.SetDirectionMatrix( sitkDirectionMatrix )
image.SetOrigin( sitkOrigin )
image.GetPointData().SetScalars( numpy_to_vtk( array.ravel() ) )
image.Modified()

When I print the spacing, origin, and direction matrix, they are the same between SITK and VTK, so I am really confused as to where I went wrong.

Hello! Welcome to the support forum. I think you might have set the point data incorrectly. Try this instead:

image.GetPointData().AddArray(numpy_to_vtk(array))
# If you have only one data array in the image, this next step is NOT NECESSARY.
image.GetPointData().SetActiveScalars("your array name here")

Hi, when I tried to do image.GetPointData().AddArray( numpy_to_vtk( array ) ), it gives me a Segmentation Fault. Is there a step that I forgot?

Here’s a trivial example on my system:

from vtkmodules.vtkCommonDataModel import vtkImageData
from vtk.util.numpy_support import numpy_to_vtk
import numpy as np

img = vtkImageData()
img.SetDimensions(2, 2, 2)
img.SetSpacing(1, 1, 1)

# Just some data I made up to store:
NUM_POINTS = img.GetNumberOfPoints()
data = np.arange(NUM_POINTS)

img.GetPointData().AddArray(numpy_to_vtk(data))