[vtkusers] vtkImageImport crash on Windows via Python

Chen, David (NIH/NLM/LHC) [C] dchen at mail.nih.gov
Wed Mar 23 14:49:22 EDT 2016


I wrote a Python function to convert a SimpleITK volume to VTK, and it has been working fine for me on OS X and Linux.  A user reported to me that my code was crashing on Windows, and I have been able to re-create the crash.

I am using a vtkImageImport object to put a data string into vtkImageData which the function returns.  However when I try and access a scalar in the image, it crashes on Windows.  It seems that the scalar data is being deleted once the function is out of scope.

You can see my original code here:  https://github.com/dave3d/dicom2stl/blob/master/sitk2vtk.py

Here is a simplified version of that code that crashes in the same way.  The crash happens at the last line in the code, where I try and access the first voxel of the image.  The same access that occurs inside the doImport function runs fine.


#! /usr/bin/env vtkpython

import vtk

xmax = ymax = zmax = 102

# simplified version of the function at https://github.com/dave3d/dicom2stl/blob/master/sitk2vtk.py
# which was adapted from http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy
#

def doImport(test_string):
    dataImporter = vtk.vtkImageImport()

    dataImporter.CopyImportVoidPointer( test_string, len(test_string) )

    dataImporter.SetDataScalarType(vtk.VTK_CHAR)

    dataImporter.SetNumberOfScalarComponents(1)
    dataImporter.SetDataExtent  (0, xmax-1, 0, ymax-1, 0, zmax-1)
    dataImporter.SetWholeExtent (0, xmax-1, 0, ymax-1, 0, zmax-1)

    dataImporter.Update()

    vtkvol = dataImporter.GetOutput()
    print "access inside function"
    print "vol[0,0,0] =", vtkvol.GetScalarComponentAsFloat(0,0,0,0)
    print "GetScalarPointer() =", vtkvol.GetScalarPointer()

    return vtkvol

# fill a string with '0's
test_string = "0" * (xmax*ymax*zmax)

vol = doImport(test_string)

print vol
print "access outside function"
print "volume object pointer", hex(id(vol))
print "GetScalarPointer() =", vol.GetScalarPointer()

print vol.GetScalarComponentAsFloat(0,0,0,0)


And suggestions are greatly appreciated.

Thanks,
Dave Chen
David T. Chen, PhD                      [MSC contractor]
mailto:dchen at mail.nih.gov               http://lhncbc.nlm.nih.gov/personnel/david-chen
phone:301.435.3264<tel:301.435.3264>                      iphone:301.524.3174<tel:301.524.3174>
Office of High Performance Computing and Communications
National Library of Medicine

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160323/112fe550/attachment.html>


More information about the vtkusers mailing list