[Insight-users] issue with metaImageIO

ken wastrel at gmail.com
Wed Jun 3 19:56:39 EDT 2009


I am facing some problems with read/write metaImages. The original raw data
file was overwritten by the generated raw data files even a new file name
was assigned to the writer. Code is below and any input will be appreciated!


/*=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

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

#include "itkMetaImageIO.h"

#include <iostream>
#include <limits>
#include <iomanip>
#include <string>
#include <sstream>
#include <typeinfo>
using namespace std;


int main( int argc, char **argv ) {

    //Input and output image data directory
    std::string inputImage = "A.mhd";
    std::string outputHeaderName = "Output.mhd";

    //Define the pixel type and dimension for reader
    typedef float InternalPixelType;
    const unsigned int    Dimension=3;
    typedef itk::Image<InternalPixelType, Dimension>    InternalImageType;

    //Define the reader
    typedef itk::ImageFileReader<InternalImageType>  ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    typedef itk::MetaImageIO    MetaImageIOType;
    MetaImageIOType::Pointer metaIO=MetaImageIOType::New();
    reader->SetImageIO(metaIO);
    reader->SetFileName(inputImage);

    //Update the reader
    try
    {
        reader->Update();
    }
    catch(itk::ExceptionObject & ex_reader)
    {
        std::cerr << "ExceptionObject caught @ _reader !" << std::endl;
        std::cout<<ex_reader<<std::endl;
        return EXIT_FAILURE;
    }


    //Initialize the writer
    typedef unsigned char    OutputPixelType;
    typedef itk::Image<OutputPixelType, Dimension>    OutputImageType;
    typedef itk::ImageFileWriter<OutputImageType>  WriterType;
    WriterType::Pointer writer=WriterType::New();
    writer->SetImageIO(metaIO);
    writer->SetFileName(outputHeaderName);


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

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

    return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090603/fdf2749e/attachment.htm>


More information about the Insight-users mailing list