[Insight-users] native function returning an itk::Image

protein proteinbme at gmail.com
Fri Aug 29 10:39:39 EDT 2008


Hey all,

This might be a duplicated question but I really didn't find a answer..

Basically my scenario could be described as to write a native function
to read a .png file, using an instantiated itk::ImageFileReader<unsigned char>,
and return an itk::Image<unsigned char>. How to keep that itk::Image alive
after returning from the native function?

So in the header file I defined:

   const    unsigned int    Dimension = 2;
   typedef  float           PixelType;
   typedef itk::Image< PixelType, Dimension >  ImageType;
   typedef itk::ImageFileReader< ImageType  > ImageReaderType;

then I declear a function, which takes
char* imageFileName as input
and
output an ImageType*

    ImageType* readImg(char* imageFileName);

the definition readImg function:
    ImageReaderType reader = ImageReaderType::New();
    reader->SetFileName(imageFileName);
    reader->Update();
    return reader->GetOutput();


But when calling this function in main(), by:
    ImageType::Pointer img = readImg("test.png");

I know the reader is destructed when going back to main, thus the
there will be error.

So I increased the reference count of reader before returning,
to keep it alive even after returned from the readImg function, I added:
    reader->SetReferenceCount(1+reader->GetReferenceCount());
before "return reader->GetOutput();"

Then in the main, I could use the img.

However this seems not that nice to me since reader won't get released....


Could anyone tell me in general how to deal with such kind of problems?
I really appreciate that!

Thanks,
Yi


More information about the Insight-users mailing list