[Insight-users] (New User Question) how do I use itkImageSeriesWriter?

John Dailey john.w.dailey at gmail.com
Mon Jul 14 15:35:19 EDT 2008


I am having some problems using the itkImageSeriesWriter.  I am quite
certain that I must be setting things up wrong, or not understanding
what it is supposed to be used for, or something.

In debugging my 3d segmentation code, I realized that I can't even get
the image series writer to work (and that might be why my segmentation
isn't working).

I wrote a little code that runs the ImageSeriesReader, casts the pixel
type and then runs the ImageSeriesWriter (going to a different file
name).  The code doesn't do anything (it should result in a copy of my
image series, right?), and I am hoping someone can help me figure out
why.  One thing I noticed that I think might be relevant is that when
I just call update on the reader, or the caster instead of the writer,
the runtime of the code is much longer.  Like it is reading the images
and casting them in the case where update is called on the caster
rather than the writer, where when it is called on the writer, the
code just returns...

Any ideas?

Thanks for helping me out, I am still trying to figure out how ITK works...

-John





Here is the program I wrote:

#include "itkImage.h"
#include "itkRescaleIntensityImageFilter.h"
#include <iostream>
#include <string>

#include "itkImageSeriesWriter.h"
#include "itkImageSeriesReader.h"
#include "itkNumericSeriesFileNames.h"
#include "itkPNGImageIO.h"


int main( int argc, char *argv[] )
{
  if( argc < 4 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImage  outputImage #ofSlices"  << std::endl;
    return 1;
    }

  typedef   float           InternalPixelType;
  const     unsigned int    Dimension = 3;
  typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;

  typedef unsigned char OutputPixelType;
  typedef itk::Image< OutputPixelType, Dimension-1 > OutputImageType;
  typedef itk::Image< OutputPixelType, Dimension > OutputSeriesType;



  // We instantiate reader and writer types in the following lines.
  //
	typedef itk::NumericSeriesFileNames    NameGeneratorType;

	NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
	const std::string infilebase = argv[1];
	const unsigned int first = 0;
  	const unsigned int last  = atoi( argv[3] );

	std::string seriesFormat = infilebase+"%03d.png";
	nameGenerator->SetSeriesFormat(seriesFormat.c_str());
	nameGenerator->SetStartIndex(first);
	nameGenerator->SetEndIndex(last);
	nameGenerator->SetIncrementIndex(1);
	std::cout <<"seriesFormat:"<<seriesFormat.c_str()<<std::endl;

	NameGeneratorType::Pointer outGenerator = NameGeneratorType::New();
	const std::string outfilebase = argv[2];

	std::string outFormat = outfilebase+"%03d.png";
	outGenerator->SetSeriesFormat(outFormat.c_str());
	outGenerator->SetStartIndex(first);
	outGenerator->SetEndIndex(last);
	outGenerator->SetIncrementIndex(1);

  typedef  itk::ImageSeriesReader< InternalImageType > ReaderType;
  typedef  itk::ImageSeriesWriter< OutputImageType, OutputSeriesType
> WriterType;

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

	reader->SetImageIO(itk::PNGImageIO::New());
	reader->SetFileNames(nameGenerator->GetFileNames());

	writer->SetImageIO(itk::PNGImageIO::New());
  	writer->SetFileNames(outGenerator->GetFileNames());

  typedef itk::RescaleIntensityImageFilter<
                               InternalImageType,
                               OutputImageType >   CastFilterType;

  CastFilterType::Pointer casterA = CastFilterType::New();
  casterA->SetInput(reader->GetOutput() );
  writer->SetInput( casterA->GetOutput() );


  try
    {
    //casterA->Update();
    writer->UpdateLargestPossibleRegion();
    }
  catch( itk::ExceptionObject & excep )
    {
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
    }


  return 0;
}


More information about the Insight-users mailing list