[Insight-users] Re: Re: format conversion

Amadeus minut at cablespeed.com
Sun Apr 24 13:59:32 EDT 2005


On Sun, 24 Apr 2005 12:13:04 -0400, Stephen R. Aylward wrote:

> Hi Amadeus,
> 
> Thanks for the tip.
> 
> I've added this to the ITK file format wiki:
> http://www.itk.org/Wiki/ITK_File_Formats
> 
> Are you willing to contribute your modifications to 
> ImageSeriesReadWrite, if they are cross-platform?
> 

Absolutely. Don't know if the shell in Windows understands
wildcards, maybe cygwin. Here's the code:


// Read a sequence of 2D images, stack them and output a volume. 
// Cosmetic changes were applied to the original ImageSeriesReadWrite.cxx
// in order to allow the convenience of using wildcards on the command line,
// on systems that support that. 
// 
// Author: Silviu Minut
// Date: Apr 23 2005
// minutsil at cse.msu.edu


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

#include <vector>

using std::vector;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;



void usage(char * prog)
{
    cerr << endl << "\e[1;31mUsage: \e[0m" 
	 << prog << " 2Dframe*.xxx output.yyy" << endl 
	 << endl;
    
    cerr << "\e[1;34mPurpose: \e[0m" << endl
	 << "\tRead a sequence of at least two images, stack them into a\n"
	 << "\tvolume and output a 3D itk image, which can be then used\n"
	 << "\te.g. in the GeodesicActiveContour application." << endl << endl
	 << "\tThe format of the input and output images is inferred" << endl
	 << "\tfrom the extension." << endl
	 << endl
	 << "\tIf you input images in an unsupported format, itk will" << endl
	 << "\tcomplain and will output a list of supported formats." << endl
	 << "\tSee also: http://www.itk.org/Wiki/ITK_File_Formats ."
	 << endl
	 << endl;
    exit(-1);
}


int main( int argc, char ** argv )
{   
    size_t i=0;
    vector<string> ifile;

    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;
    WriterType::Pointer writer;


    // itkImageSeriesReader requires at least two input files.
    // We also need an output file name. 
    if(argc<4)  
	usage(argv[0]);
    
    // Store the input file names into a vector of strings,
    // which will be passed on to the reader. 
    for(i=1; i<argc-1; i++)
	ifile.push_back(string(argv[i]));

    // Read the input images.
    reader = ReaderType::New();

    // This is how one would enforce reading a certain format (e.g. png):
    // reader->SetImageIO( itk::PNGImageIO::New() );
    reader->SetFileNames( ifile );
    try { 
	reader->Update();
    }
    catch( itk::ExceptionObject & excp ) {
	cerr << "Error reading the series." << endl;
	cerr << excp << endl;
    }


    // Write out.
    writer = WriterType::New();
    writer->SetFileName( argv[argc-1] );
    writer->SetInput( reader->GetOutput() );
    writer->Update();

    cerr << "Output: " << argv[argc-1] << endl;

    return 0;
}












More information about the Insight-users mailing list