[Insight-users] vtk-itk-vtk problem

Luis Ibanez luis.ibanez@kitware.com
Wed, 23 Apr 2003 20:50:08 -0400


Hi Nick,

Both VTK and ITK filters own their outputs.
When the filters are destroyed, their outputs
are destroyed along with them.

You can prevent this from happening by unpluging
the output from the filter and passing it to
another pointer, but...

Probably the best solution in your case is to make
the ITK and VTK filters to be member variables of
the class "Test" instead of being simple local variables
in the "Update()" method. In that way they will not
be destroyed at the end of the "Update()" execution.

What you are doing right now is like returning a
reference to alocal variable.




Regards


    Luis



------------------------------

Nick Hu wrote:
> Hi,
> I tried to write a new class which contains a simple test of vtk image 
> to itk image and then back to vtk image. I used the two filters, 
> itkImageToVTKImage and itkVTKImageToImageFilter, to convert the image 
> between itk and vtk. When I build the image rendering pipeline inside 
> the class, everything works fine. But then when I get the vtkimage as 
> output to other program outside (just like regular 
> vtkImageFilter->GetOutput()), always got an error "segmentation error" . 
> Below is the Pseudo-code:
>  
> 1.code don't work:
> vtkImageDate* Test::Update(void)
> {
>     Initialize vtkImageReader reader;
>     Initialize itk::VTKImageToImageFilter vtkToItk;
>     vtkToItk->SetInput(reader->GetOutput());
>     vtkToItk->GetImporter()->Update();
>  
>     //do nothing inside itk
>  
>     Initialize itk::ImageToVTKImageFilter itkToVtk;
>     itkToVtk->SetInput(vtkToItk->GetOutput());
>     itkToVtk->GetImporter()->Update();   
>    
>     return itkToVtk->GetOutput();
> }
> main()
> {
>     Initialize Test t;
>     image=t->Update();
>     image->Update();  //failed here or later when rendering pipeline 
> force it to update, segmentation fault
>    
>     //rendering pipeline below
> }
>  
> 2. If I build pipe line inside the class:
> void Test::Update(void)
> {
>     Initialize vtkImageReader reader;
>     Initialize itk::VTKImageToImageFilter vtkToItk;
>     vtkToItk->SetInput(reader->GetOutput());
>     vtkToItk->GetImporter()->Update();
>  
>     //do nothing inside itk
>  
>     Initialize itk::ImageToVTKImageFilter itkToVtk;
>     itkToVtk->SetInput(vtkToItk->GetOutput());
>     itkToVtk->GetImporter()->Update();   
>    
>     //image rendering pipeline below, without return value
>    
> }
> main()
> {
>     Initialize Test t;
>     t->Update();
> }
>  
> Then everything works correct. I even print the vtkImageData before 
> return it in the first code and it looks right. Both codes can be 
> compiled and linked correctly.
> Can anyone give me a suggestion about the error? 
> By the way, the reason I want to wrap itk in vtk class is I have some 
> existing project written in Python and I need to call itk from python 
> application :(
>  
> Many thanks in advance  
>  
>  
>  
>  
> Nick Hu, M.E.Sc.
> Medical Imaging Software Developer
> Imaging Research, Sunnybrook & Women's
> Suite 6020, 3080 Young Street / PO Box 89, Toronto
> ON M4N 3N1
> Tel: 416-4823856 Fax: 416-4823807