[vtkusers] vtk image from itk

gregthom992 gregthom992 at gmail.com
Fri Jun 12 15:05:46 EDT 2015


<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



--
View this message in context: http://vtk.1045678.n5.nabble.com/vtk-image-from-itk-tp5732315.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list