[Insight-users] Pass an itkimage::Pointer to a template function in a non template class

Luis Ibanez luis.ibanez at kitware.com
Tue May 25 14:00:12 EDT 2010


Hi Antonin

You may find useful to look at the following
paper in the Insight Journal:

http://www.insight-journal.org/browse/publication/146

Please note that the source code is available
in the InsightApplications directory:

InsightApplications/Auxiliary/vtk
vtkKWImage.cxx
vtkKWImage.h
vtkKWImageIO.cxx
vtkKWImageIO.h


This seems to be what you  are looking for.


    Regards,


            Luis


--------------------------------------------------------------
On Wed, May 12, 2010 at 2:57 PM, Antonin Perrot-Audet
<antonin07130 at gmail.com> wrote:
> 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.
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list