[vtkusers] How to retrun a vtkImageData object pointer from function call

Karthik Krishnan karthik.krishnan at kitware.com
Wed Aug 22 11:01:38 EDT 2007


Michael :

Upon return from your functiion,  the smart pointer goes out of scope and
will cleanup its resources, which includes the generated output, since noone
is holding a reference to it. You could rephrase that method as below, with
a shallow copy. The shallow copy will cause the "output" to hold a reference
to the output of the converter and prevent it from vanishing upon return.

void itk2vtk(ReaderType3D::OutputImageType::Pointer image, vtkImageData
*output)
{
    // --- Same stuff as you have below until converter->Update() ----
    ....
    converter->Update();
    output->ShallowCopy(converter->GetOutput());
}


-- 
Karthik Krishnan
R&D Engineer,
Kitware Inc.
On 8/22/07, Michael Schildt <michael.schildt at ifn-magdeburg.de> wrote:
>
> Hello!
>
> I Want to display data from ITK in VTK but I have trouble to return a
> VTK object from a function to the main function. I call the function
> with a valid 3d ITK image. The y-axis is flipped to account for the
> different coordinate systems of ITK and VTK. Then converted via
> ImageToVTKImageFilter. It works fine. I want to return the generated VTK
> image. It is valid up to the return statement. After that it seems to be
> deleted. How can I tell the program not to delete this object? Do i have
> to return the object instead of its pointer?
>
> Function in question is (vtkImage is valid until return, in main the
> return value is invalid):
>
>   vtkImageData* itk2vtk(ReaderType3D::OutputImageType::Pointer image) {
>
> itk::FlipImageFilter<ReaderType3D::OutputImageType>::FlipAxesArrayType
> SourceAxes;
>     SourceAxes[0] = false;
>     SourceAxes[1] = true;
>     SourceAxes[2] = false;
>
>     itk::FlipImageFilter<ReaderType3D::OutputImageType>::Pointer flip
>       = itk::FlipImageFilter<ReaderType3D::OutputImageType>::New();
>     flip->SetFlipAxes(SourceAxes);
>     flip->SetInput(image);
>     flip->Update();
>
>     itk::ImageToVTKImageFilter<ReaderType3D::OutputImageType>::Pointer
> converter
>       = itk::ImageToVTKImageFilter<ReaderType3D::OutputImageType>::New();
>     converter->SetInput(flip->GetOutput());
>     converter->Update();
>     vtkImageData *vtkImage = converter->GetOutput();
>     return vtkImage;
>   }
>
> Greetings,
>     Michael Schildt
>
>
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070822/95cdc5ed/attachment.htm>


More information about the vtkusers mailing list