[Insight-users] Help with ImageSeriesReadWrite
Jonathan Wong
jon.the.wong at gmail.com
Tue Jun 6 15:35:42 EDT 2006
I'm new to ITK and I'm currently having problems assemble my series of Tiff
files into a vtkImage. I've simply edited the PNGSeriesWriter example. A vtk
image does get generated, and I've managed to load it in VolView and
MicroView by using ITK 2.4.1. However, when I look at the volume, the whole
volume is composed of only the first Tiff file , and is repeated along the z
axis.
Anyone know how to fix this? Help is greatly appreciated. I've pasted my
code below.
Jon
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: ImageSeriesReadWrite.cxx,v $
Language: C++
Date: $Date: 2005/11/20 13:27:53 $
Version: $Revision: 1.10 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
// Software Guide : BeginLatex
//
// This example illustrates how to read a series of 2D slices from
independent
// files in order to compose a volume. The class
\doxygen{ImageSeriesReader}
// is used for this purpose. This class works in combination with a
generator
// of filenames that will provide a list of files to be read. In this
// particular example we use the \doxygen{NumericSeriesFileNames} class as
// filename generator. This generator uses a \code{printf} style of string
format
// with a ``\code{\%d}'' field that will be successively replaced by a
number specified
// by the user. Here we will use a format like ``\code{file\%03d.png}'' for
reading
// PNG files named file001.png, file002.png, file003.png... and so on.
//
// This requires the following headers as shown.
//
// \index{itk::ImageSeriesReader!header}
// \index{itk::NumericSeriesFileNames!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkTIFFImageIO.h"
// Software Guide : EndCodeSnippet
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;
}
// Software Guide : BeginLatex
//
// We start by defining the \code{PixelType} and \code{ImageType}.
//
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char PixelType;
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > ImageType;
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];
typedef itk::NumericSeriesFileNames NameGeneratorType;
NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
nameGenerator->SetSeriesFormat( "vol%03d.tiff" ); //set file name
imageformat here
nameGenerator->SetStartIndex( first );
nameGenerator->SetEndIndex( last );
nameGenerator->SetIncrementIndex( 1 );
reader->SetImageIO( itk::TIFFImageIO::New() );
reader->SetFileNames( nameGenerator->GetFileNames() );
writer->SetFileName( outputFilename );
try
{
reader->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
/*
// JW: Get Image set spacing
ImageType::Pointer image = reader->GetOutput();
ImageType::SpacingType spacing;
// Note: measurement units (in this application it's in mm)
spacing[0] = 0.021; // spacing along X
spacing[1] = 0.021; // spacing along Y
spacing[2] = 0.022; // spacing along Z
image->SetSpacing( spacing );
ImageType::PointType origin;
origin[0] = 0.0; // coordinates of the
origin[1] = 0.0; // first pixel in N-D
origin[2] = 0.0;
image->SetOrigin( origin );
const ImageType::SpacingType& sp = image->GetSpacing();
std::cout << "Spacing = ";
std::cout << sp[0] << ", " << sp[1] << ", " << sp[2] << std::endl;
writer->SetInput( image );
// Software Guide : EndCodeSnippet
*/
writer->SetInput(reader->GetOutput());
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20060606/4af31e02/attachment-0001.html
More information about the Insight-users
mailing list