[Insight-users] problem reading and writing image series

Xabier Artaechevarria Artieda xabiarta at unav.es
Tue Jul 4 07:07:25 EDT 2006


Hi all,
I am new to ITK and I am using the Software Guide to learn how to read  
and write image files.
I am particularly interested in reading slices corresponding to the  
same volume but that are in independent files, and then creating a 3D  
volume from them (section 7.11 of the guide).
My problem is that when I open the created volume with a viewer, all  
slices are equal to the first one. The number of slices is ok, but  
they are all the same. It might be a problem of the format I use to  
write the 3D volume: I have tried TIFF and DICOM. Are they adequate?  
Should I use any other?
My code is the one of the guide, with little modifications to open  
TIFF files and correspond to the files I want to read (see below).
Thanks for your help and best regards,
Xabier Artaechevarria


#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkTIFFImageIO.h"

int main( int argc, char ** argv )
{
   // Verify the number of parameters in the command line
   if( argc < 4 )
     {
     std::cerr << "Usage: " << std::endl;
     std::cerr << argv[0] << " firstSliceValue lastSliceValue   
outputImageFile " << std::endl;
     return EXIT_FAILURE;
     }

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

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


// The image type is used as a template parameter to instantiate
// the reader and writer.

   typedef itk::ImageSeriesReader< ImageType >  ReaderType;
   typedef itk::ImageFileWriter<   ImageType >  WriterType;

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

   const unsigned int first = atoi( argv[1] );
   const unsigned int last  = atoi( argv[2] );

   const char * outputFilename = argv[3];


// Then, we declare the filenames generator type and create one  
instance of it.
   typedef itk::NumericSeriesFileNames    NameGeneratorType;
   NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();



// Set file name format
   nameGenerator->SetSeriesFormat(  
"/home/xabiarta/Desktop/Raton_tiff/aj_tratado_01_060509_%03d.tif" );

   nameGenerator->SetStartIndex( first );
   nameGenerator->SetEndIndex( last );
   nameGenerator->SetIncrementIndex( 1 );


//  The ImageIO object that actually performs the read process is now  
connected
//  to the ImageSeriesReader. This is the safest way of making sure  
that we use
//  an ImageIO object that is appropriate for the type of files that  
we want to
//  read.

    reader->SetImageIO( itk::TIFFImageIO::New() );


//  The filenames of the input files must be provided to the reader. While the
//  writer is instructed to write the same volume dataset in a single file.

   reader->SetFileNames( nameGenerator->GetFileNames()  );

   writer->SetFileName( outputFilename );

// We connect the output of the reader to the input of the writer.

   writer->SetInput( reader->GetOutput() );

//  Finally, execution of the pipeline can be triggered by invoking the
//  Update() method in the writer. This call must be placed in a try/catch
//  block since exceptions be potentially be thrown in the process of reading
//  or writing the images.

   try
     {
     writer->Update();
     }
   catch( itk::ExceptionObject & err )
     {
     std::cerr << "ExceptionObject caught!" << std::endl;
     std::cerr << err << std::endl;
     return EXIT_FAILURE;
     }

   return EXIT_SUCCESS;
}

----------------------------------------------------------------
Este mensaje ha sido enviado desde https://webmail.unav.es



More information about the Insight-users mailing list