[Insight-users] Python Wrapping: Return an itkImage Object
Hua Qian
hqian at imaging . robarts . ca
Mon, 27 Oct 2003 14:32:03 -0500
Hi Glen,
Yes, a deep copy would solve my problem. However, I didn't
find anything like that for itk::Image.
For you VTK to ITK connection problem, you can try to put
"self." before all the filters you use, I bet the "seg. fault"
will disappear. This "trick" works for both ITK to VTK and VTK
to ITK connection. I don't know why, and I am still look for
an answer. I guess it has something to do with reference counting.
We have to keep references to all the filters to keep the image
data pipeline alive.
Hua
Glen Lehmann wrote:
> Hi Hua,
>
> I avoid this when converting from itk::Image to vtkImageData by doing
> the following;
>
> def ITK2VTK(self, itkImage):
> # this will form the end-point of the ITK pipeline segment
> itkExporter = itk.itkVTKImageExportF2_New()
> itkExporter.SetInput( itkImage )
>
> # the vtkImageImport will bring our data back into VTK-land
> vtkImporter = vtk.vtkImageImport()
>
> # do the magic connection call (once again: only available if
> you built
> # ITK with ITK_CSWIG_CONNECTVTKITK set to ON)
> CVIPy.ConnectITKF2ToVTK(itkExporter.GetPointer(), vtkImporter)
>
> imageData = vtk.vtkImageData()
> imageData.DeepCopy( vtkImporter.GetOutput() )
>
> return imageData
>
> However, when converting from vtkImageData to itk::Image I get the
> same seg. faults that you do because I can't preform a deep copy
> operation on the itk::Image. Is there an equivilant operation in itk?
>
> Glen
>
> Hua Qian wrote:
>
>> Hello,
>>
>> Here are two little python classes to test returning an
>> itkImage object. One class works and the other gives
>> segmentation fault. Could someone confirm that and
>> explain why?
>>
>> Regards,
>> Hua
>>
>> from InsightToolkit import *
>>
>> class testReturnImage1:
>> def __init__(self):
>> self._reader = itkImageFileReaderF2_New()
>> self._reader.SetFileName("../../Testing/Data/Input/cthead1.png")
>> self._image = self._reader.GetOutput()
>>
>> def GetImage(self):
>> return self._image
>>
>> class testReturnImage2:
>> def __init__(self):
>> reader = itkImageFileReaderF2_New()
>> reader.SetFileName("../../Testing/Data/Input/cthead1.png")
>> self._image = reader.GetOutput()
>> def GetImage(self):
>> return self._image
>>
>> app1 = testReturnImage1()
>> image = app1.GetImage()
>> print "app1: updating image ... "
>> image.Update()
>> print " Done."
>>
>> app2 = testReturnImage2()
>> image2 = app2.GetImage()
>> print "app2: updating image ... "
>> image2.Update()
>> print " Done."
>>
>>
>>