[Insight-users] re:resample filter does not seem to work...

Yixun Liu yxliu at fudan.edu.cn
Mon Aug 28 07:00:29 EDT 2006


hi,

You should call reader->Update() before filter->SetOutputOrigin(reader->GetOutput()->GetOrigin());







Hello,
   
  I am having problem in resampling an image. The code is given below and appreciate if somebody help me out:
   
  #if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
  #include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkResampleImageFilter.h"
#include "itkAffineTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
  
int main( int argc, char * argv[] )
{
  if( argc < 3 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << "  inputImageFile  outputImageFile";
   std::cerr << std::endl;
    return EXIT_FAILURE;
    }
    const     unsigned int   Dimension = 3;
  typedef   unsigned char  InputPixelType;
  typedef   unsigned char  OutputPixelType;
   
    typedef itk::Image< InputPixelType,  Dimension >   InputImageType;
  typedef itk::Image< OutputPixelType, Dimension >   OutputImageType;
   
    typedef itk::ImageFileReader< InputImageType  >  ReaderType;
  typedef itk::ImageFileWriter< OutputImageType >  WriterType;
   
    ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
   
    reader->SetFileName( argv[1] );
  writer->SetFileName( argv[2] );
   
    typedef itk::ResampleImageFilter<
                  InputImageType, OutputImageType >  FilterType;
  FilterType::Pointer filter = FilterType::New();
  
  typedef itk::AffineTransform< double, Dimension >  TransformType;
  TransformType::Pointer transform = TransformType::New();
  transform->SetIdentity();
  filter->SetTransform( transform );
  
  typedef itk::NearestNeighborInterpolateImageFunction< 
                       InputImageType, double >  InterpolatorType;
  InterpolatorType::Pointer interpolator = InterpolatorType::New();
    filter->SetInterpolator( interpolator );
   
    filter->SetOutputOrigin(reader->GetOutput()->GetOrigin());
 
  double spacing[ Dimension ];
  spacing[0] = 0.5; // pixel spacing in millimeters along X
  spacing[1] = 0.5; // pixel spacing in millimeters along Y
  spacing[2] = 0.5; // pixel spacing in millimeters along Z
  filter->SetOutputSpacing( spacing );
   
    InputImageType::SizeType   size;
  size[0] = 80;  // number of pixels along X
  size[1] = 128;  // number of pixels along Y
  size[2] = 128;  // number of pixels along Z
  filter->SetSize( size );
   
    filter->SetInput( reader->GetOutput() );
  writer->SetInput( filter->GetOutput() );
   
    filter->SetDefaultPixelValue( 0 );
   
    try 
  { 
   writer->Update();
  } 
  
  catch( itk::ExceptionObject & err ) 
  { 
   std::cout << "ExceptionObject caught !" << std::endl; 
   std::cout << err << std::endl; 
   system("pause");
   return EXIT_FAILURE;
  }
  
  return EXIT_SUCCESS;
}
   
  Thanks
Shahab
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20060828/c6f8f5f1/attachment.html


More information about the Insight-users mailing list