[vtkusers] Pass an itkimage::Pointer to a template function in a non template class

Antonin Perrot-Audet antonin07130 at gmail.com
Wed May 12 11:31:49 EDT 2010


Hello ITK users,

I am facing a problem I guess many of you already solved :
I wrote a non-template class, but I would like this class to accept both 
itk images and vtk images.

In order to process the vtk images, I would like to create a template 
function within my non template class, that takes an itkimage::Pointer 
as input, internally convert it into a vtkimage* and store the vtkimage* 
in an array.

It seems like I can't pass the itkimage::Pointer as an argument : If I 
try to write the image from within the function (with an itk writer), I 
write an empty image (of the good size !).





here is a code example of what I am trying to do :



1) A simplified version of what I want to do :

The version of my function (AddImage) that just writes the images from 
the function :

----------------myclass.h------------------
// plenty of other functions

   template <typename TitkImage>
   void AddImage(typename TitkImage::Pointer iImage)
   {
     typedef itk::ImageFileWriter<TitkImage>    WriterType;
     typename WriterType::Pointer itkwriter = WriterType::New();

     itkwriter->SetInput(iImage);
     itkwriter->SetFileName ("iktout.png");
     itkwriter->Update();
     itkwriter->Write();
   }
-----------------------------------------------


-----------------main.cpp-------------------
int main( )
{
// declare an instance of myclass

   typedef itk::Image<unsigned short,2> ImageType;
   typedef itk::ImageFileReader<ImageType> itkreaderType;

// setup the itk reader with the correct filename, and update it

myclass->AddImage<ImageType>( itkreader->GetOutput() );

return 0;
}

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








2) what I actually want to do :

The version of myclass that imports an itkimage converts it as a vtk 
image and store it somewhere in myclass, main.cpp is the same as in 1)

----------------myclass.h-----------------
// non template class definition and heritage
// non template members and functions ...

   template <typename TitkImage>
   void AddImage(typename TitkImage::Pointer iImage, std::string 
iImageName ="")
   {
     typedef itk::ImageToVTKImageFilter<TitkImage>    ConnectorType;
     typename ConnectorType::Pointer itkvtkConnector = ConnectorType::New();

     itkvtkConnector->SetInput(iImage);
   try
     {
     itkvtkConnector->Update();
     }
   catch( itk::ExceptionObject & exp )
     {
     std::cerr << exp << std::endl;
     }

     AddImage( itkvtkConnector->GetOutput() ); // call the other 
importer of myclass

   }


   void AddImage(vtkImageData* iImage); // a function that stores a 
vtkImageData* into an array

// other stuff ....

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




I have already looked at : 
http://old.nabble.com/Smart-pointers-as-function-argument-ts26375719.html#a26375719
but it doesn't answer my problem, as the itkimage is already bad inside 
my function.

The weird thing is that if I print it from my function, it prints that 
the image is a 2D image, of correct dimensions, and of unsigned char 
pixel type.


Well, if someone has an idea, that would help a lot !

Thanks a lot,

Antonin.

PS: As soon as I am done with that thing, I will write a small script to 
build itk's nightly every night.



More information about the vtkusers mailing list