[Insight-users] copy an image

Alex. GOUAILLARD hanfei at caltech.edu
Wed Dec 12 22:09:23 EST 2007


hi all,

I developped (read: copied from the examples) a simple function that 
should resample a given 2D image according to the result of a 
registration (see attached file).

I have a scope problem: after I copy the output of the filter in a 
separate disconnected image, if I write the image in a file from within 
the function, I have what I expect, but If I try to pass the pointer out 
and to print it from outside later, it fails. It looks like the buffer 
and a few other attributes are being wiped out.

Any idea on what I am doing wrong? Better, any suggestion on what to do 
to make it right ?

alex.
-------------- next part --------------
#include "itkImageFileWriter.h"
#include "itkVectorLinearInterpolateImageFunction.h"
#include "itkVectorResampleImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkTransform.h"


template<class ImageType, class RegistrationType>
int Resample(  typename ImageType::Pointer InImage,
               typename ImageType::Pointer OutImage, // should be allocated outside
               typename RegistrationType::Pointer InRegistration,
               bool Write)
{
  //------------- resample RGB => Vector
  typedef itk::VectorResampleImageFilter< ImageType, ImageType > ResampleFilterType;
  ResampleFilterType::Pointer resampler = ResampleFilterType::New();
  typedef itk::VectorLinearInterpolateImageFunction< ImageType, double > 
                                                          ResamplerInterpolatorType;
  ResamplerInterpolatorType::Pointer interpolator = ResamplerInterpolatorType::New();
  resampler->SetInterpolator( interpolator );  
  resampler->SetInput( InImage );
  resampler->SetTransform( InRegistration->GetOutput()->Get() );

  resampler->SetSize( InImage->GetLargestPossibleRegion().GetSize() );
  resampler->SetOutputOrigin(  InImage->GetOrigin() );
  resampler->SetOutputSpacing( InImage->GetSpacing() );
  resampler->SetDefaultPixelValue( 100 );

  OutImage = resampler->GetOutput();
  OutImage->DisconnectPipeline();

  if(Write)
    {
    typedef itk::ImageFileWriter< ImageType >  WriterType;
    WriterType::Pointer writer =  WriterType::New();
    writer->SetFileName( "Resample.jpg" );
    writer->SetInput( OutImage );
    try
      {
      writer->Update();
      }
    catch ( itk::ExceptionObject &err)
      {
      std::cout << "ExceptionObject caught !" << std::endl; 
      std::cout << err << std::endl; 
      return -1;   
      }
    }
  return 0;
}


More information about the Insight-users mailing list