[Insight-users] Problems to read a vtk file using SimpleITK+Python

Ariel Hernán Curiale curiale at gmail.com
Tue Aug 6 16:26:52 EDT 2013


Hi Matt,

You are right, the vtk format is always 3D. If you have a 2D image  and save it as vtk, then the writer automatically add the last dimension with a spacing 1. 
For example, this is the 2D image create with the python code:

ariel at Gohan:~/Uva/LPI/Proyectos/Python-ITK-VTK-QT/Ultrasonido/SpeckleTracking$ head -10 img.vtk 
# vtk DataFile Version 3.0
VTK File Generated by Insight Segmentation and Registration Toolkit (ITK)
BINARY
DATASET STRUCTURED_POINTS
DIMENSIONS 100 100 1 
SPACING 1.0000000000000000e+00 1.0000000000000000e+00 1.0000000000000000e+00 
ORIGIN 0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 
POINT_DATA 10000
SCALARS scalars double 1
LOOKUP_TABLE default
.....

I don't know why the warning say that the spacing must be grater than 0

Ps: I can read this image using vtk. 

Thanks for your quickly response.

Cheers,
__________________________________
| Ariel Hernán Curiale Ph.D Student
| ETSI Telecomunicación
| Universidad de Valladolid
| Campus Miguel Delibes
| 47011 Valladolid, Spain
| Phone: 983-423000 ext. 5590
| Web: www.curiale.com.ar
|_________________________________

El 06/08/2013, a las 17:52, Matt McCormick escribió:

> Hi Ariel,
> 
> The VTK file format [1] is always 3D, so a non-zero spacing will need
> to be specified if using this format.
> 
> HTH,
> Matt
> 
> [1] www.vtk.org/VTK/img/file-formats.pdf
> 
> 
> On Tue, Aug 6, 2013 at 3:00 PM, Ariel Hernán Curiale <curiale at gmail.com> wrote:
>> Hi,
>> I create a simple example to show you the problem that I'm getting when I'm
>> trying to read a vtk file. I'm using the last release of SimpleITK
>> (SimpleITK-0.6.1) and python.
>> In this example, I create, write and read a simple image.
>> 
>> Here is the code:
>> -------------------------
>> import SimpleITK as sitk
>> import scipy as sc
>> 
>> dim = (100, 100)
>> origin = (0, 0)
>> spacing = (0.834496, 0.8223519)
>> img = sc.zeros(dim)
>> 
>> img[20:40, 20:40] = 1
>> 
>> img_sitk = sitk.GetImageFromArray(img)
>> img_sitk.SetOrigin(origin)
>> img_sitk.SetSpacing(spacing)
>> 
>> print img_sitk
>> sitk.Show(img_sitk)
>> sitk.WriteImage(img_sitk,"img.vtk")
>> 
>> im = sitk.ReadImage("img.vtk")
>> 
>> reader = sitk.ImageFileReader()
>> reader.SetFileName("img.vtk")
>> im2 = reader.Execute()
>> --------
>> 
>> 
>> The img_sitk seems to be created properly:
>> ----
>> Image (0x7f9a95ff8550)
>>  RTTI typeinfo:   itk::Image<double, 2u>
>>  Reference Count: 1
>>  Modified Time: 3295
>>  Debug: Off
>>  Observers:
>>    none
>>  Source: (none)
>>  Source output name: (none)
>>  Release Data: Off
>>  Data Released: False
>>  Global Release Data: Off
>>  PipelineMTime: 0
>>  UpdateMTime: 0
>>  RealTimeStamp: 0 seconds
>>  LargestPossibleRegion:
>>    Dimension: 2
>>    Index: [0, 0]
>>    Size: [100, 100]
>>  BufferedRegion:
>>    Dimension: 2
>>    Index: [0, 0]
>>    Size: [100, 100]
>>  RequestedRegion:
>>    Dimension: 2
>>    Index: [0, 0]
>>    Size: [100, 100]
>>  Spacing: [0.834496, 0.822352]
>>  Origin: [0, 0]
>>  Direction:
>> 1 0
>> 0 1
>> 
>>  IndexToPointMatrix:
>> 0.834496 0
>> 0 0.822352
>> 
>>  PointToIndexMatrix:
>> 1.19833 0
>> 0 1.21602
>> 
>>  Inverse Direction:
>> 1 0
>> 0 1
>> 
>>  PixelContainer:
>>    ImportImageContainer (0x7f9a960309b0)
>>      RTTI typeinfo:   itk::ImportImageContainer<unsigned long, double>
>>      Reference Count: 1
>>      Modified Time: 2866
>>      Debug: Off
>>      Observers:
>>        none
>>      Pointer: 0x7f9a95352200
>>      Container manages memory: true
>>      Size: 10000
>>      Capacity: 10000
>> 
>> 
>> 
>> But when I try to read the image using sitk.ReadImage or
>> sitk.ImageFileReader I'm getting this error:
>> ----
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>>  File
>> "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SimpleITK-0.6.1_g1387a-py2.7-macosx-10.7-x86_64.egg/SimpleITK.py",
>> line 4371, in Execute
>>    return _SimpleITK.ImageFileReader_Execute(self)
>> RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute:
>> /Users/ariel/Applications/InsightToolkit-4.4.0/Modules/Core/Common/include/itkImageBase.hxx:189:
>> itk::ERROR: Image(0x7f9a95d9d640): A spacing of 0 is not allowed: Spacing is
>> [1, 0]
>> ----
>> 
>> 
>> 
>> It seems to be a bug but, I don't know if I'm doing something wrong.
>> 
>> 
>> Cheers,
>> __________________________________
>> | Ariel Hernán Curiale Ph.D Student
>> | ETSI Telecomunicación
>> | Universidad de Valladolid
>> | Campus Miguel Delibes
>> | 47011 Valladolid, Spain
>> | Phone: 983-423000 ext. 5590
>> | Web: www.curiale.com.ar
>> |_________________________________
>> 
>> 
>> _____________________________________
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>> 
>> Kitware offers ITK Training Courses, for more information visit:
>> http://www.kitware.com/products/protraining.php
>> 
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-users
>> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20130806/f9dc0074/attachment.htm>


More information about the Insight-users mailing list