[vtkusers] vtk image from itk

David Gobbi david.gobbi at gmail.com
Fri Jun 12 16:12:50 EDT 2015


Hi Greg,

Part of the problem is this line of code:

    dataImporter.SetDataOrigin(origin)

The VTK image origin and the ITK image origin are not the same.  They have
the same name, but they have different definitions.  Numerically, they will
have the same value only if the ITK orientation matrix is the identity
matrix.

My approach to dealing with oriented images in VTK is to store both the
orientation and the offset in a 4x4 matrix (a vtkMatrix4x4), since the
orientation cannot be stored in the vtkImageData itself.  Because I store
the offset (i.e. the ITK origin) in the 4x4 matrix, I must set the VTK
image origin to zero.

In general, when moving images between ITK and VTK, I suggest always
setting the VTK image origin to zero unless you are certain that all of
your images have no orientation (i.e. identity orientation matrix).

 - David





On Fri, Jun 12, 2015 at 1:05 PM, gregthom992 <gregthom992 at gmail.com> wrote:

> <http://vtk.1045678.n5.nabble.com/file/n5732315/manglesimages.png>
>
> Hi, I am reading an image from SimpleITK but I get these results in vtk any
> help ?
>
> ####
> CODE
> def sitk2vtk(img):
>
>     size     = list(img.GetSize())
>     origin   = list(img.GetOrigin())
>     spacing  = list(img.GetSpacing())
>     sitktype = img.GetPixelID()
>     vtktype  = pixelmap[sitktype]
>     ncomp    = img.GetNumberOfComponentsPerPixel()
>
>     # there doesn't seem to be a way to specify the image orientation in
> VTK
>
>     # convert the SimpleITK image to a numpy array
>     i2 = sitk.GetArrayFromImage(img)
>     #import pylab
>     #i2 = reshape(i2, size)
>
>     i2_string = i2.tostring()
>
>     # send the numpy array to VTK with a vtkImageImport object
>     dataImporter = vtk.vtkImageImport()
>
>     dataImporter.CopyImportVoidPointer( i2_string, len(i2_string) )
>
>     dataImporter.SetDataScalarType(vtktype)
>
>     dataImporter.SetNumberOfScalarComponents(ncomp)
>
>     # VTK expects 3-dimensional parameters
>     if len(size) == 2:
>         size.append(1)
>
>     if len(origin) == 2:
>         origin.append(0.0)
>
>     if len(spacing) == 2:
>         spacing.append(spacing[0])
>
>     # Set the new VTK image's parameters
>     #
>     dataImporter.SetDataExtent (0, size[0]-1, 0, size[1]-1, 0, size[2]-1)
>     dataImporter.SetWholeExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1)
>
>     dataImporter.SetDataOrigin(origin)
>     dataImporter.SetDataSpacing(spacing)
>
>     dataImporter.Update()
>
>     vtk_image = dataImporter.GetOutput()
>     return vtk_image
> ###
> END CODE
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150612/2f3ac4ce/attachment.html>


More information about the vtkusers mailing list