[Insight-users] pointSet to Image

David Macias Verde david.maciasverde at gobiernodecanarias.org
Sat, 24 Apr 2004 09:35:21 +0100


Hi:

Could anyone tell what is the problem I have when converting a pointset
to an image with the PointSetToImage class? I attached below my code.

I have converted an image to a pointset and then going back to an image.

Thanks,

David Macias-Verde



#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"


#include "itkImage.h"
#include "itkPointSet.h"
#include "itkImageRegionConstIterator.h"
#include "itkPointSetToImageFilter.h" 
#include "itkImageFileWriter.h"


int main( int argc, const char * argv[] )
{
  
  if( argc < 2 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  " << std::endl;
    return -1;
    }

  typedef unsigned short PixelType;
  const   unsigned int Dimension = 2;

  typedef itk::Image< PixelType, Dimension >    ImageType;

  typedef itk::PointSet< PixelType, Dimension > PointSetType;

  typedef itk::ImageFileReader< ImageType >  ReaderType;

  ReaderType::Pointer reader = ReaderType::New();

  const char * inputFilename  = argv[1];
  reader->SetFileName( inputFilename  );

  try 
    { 
    reader->Update(); 
    } 
  catch( itk::ExceptionObject & err ) 
    { 
    std::cout << "ExceptionObject caught !" << std::endl; 
    std::cout << err << std::endl; 
    return -1;
    } 

  PointSetType::Pointer  pointSet = PointSetType::New();


  typedef itk::ImageRegionConstIterator< ImageType > IteratorType;

  const ImageType * image = reader->GetOutput();

  IteratorType it( image, image->GetBufferedRegion() );

  it.GoToBegin();

  typedef PointSetType::PointType     PointType;
  PointType point;

  unsigned long pointId = 0;

  while( !it.IsAtEnd() )
    {
    image->TransformIndexToPhysicalPoint( it.GetIndex() , point );
    pointSet->SetPoint( pointId, point );
    pointSet->SetPointData( pointId, it.Get() );
    ++it;
    ++pointId;
    }
   
  typedef itk::PointSetToImageFilter <PointSetType, ImageType>
FilterType;
  
  FilterType::Pointer finalImage = FilterType::New();
    finalImage->SetInput(pointSet);
   
  typedef itk::ImageFileWriter <ImageType> WriterType;
        
  WriterType::Pointer writer =  WriterType::New();
   
   try 
    { 
      writer->SetFileName("test.png");
      writer->SetInput(finalImage->GetOutput());
      writer->Update();
    } 
  catch(itk::ExceptionObject & err) 
    { 
    std::cout << "ExceptionObject caught !" << std::endl; 
    std::cout << err << std::endl; 
    return -1;
    }    

}