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!<br>
<br><br>/*=========================================================================*/<br><br>#if defined(_MSC_VER)<br>#pragma warning ( disable : 4786 )<br>#endif<br><br>#include "itkImage.h"<br>#include "itkImageFileReader.h"<br>
#include "itkImageFileWriter.h"<br><br>#include "itkMetaImageIO.h"<br><br>#include <iostream><br>#include <limits><br>#include <iomanip><br>#include <string><br>#include <sstream><br>
#include <typeinfo><br>using namespace std;<br><br><br>int main( int argc, char **argv ) {<br><br> //Input and output image data directory<br> std::string inputImage = "A.mhd";<br> std::string outputHeaderName = "Output.mhd";<br>
<br> //Define the pixel type and dimension for reader<br> typedef float InternalPixelType;<br> const unsigned int Dimension=3;<br> typedef itk::Image<InternalPixelType, Dimension> InternalImageType;<br>
<br> //Define the reader<br> typedef itk::ImageFileReader<InternalImageType> ReaderType;<br> ReaderType::Pointer reader = ReaderType::New();<br> typedef itk::MetaImageIO MetaImageIOType;<br> MetaImageIOType::Pointer metaIO=MetaImageIOType::New();<br>
reader->SetImageIO(metaIO);<br> reader->SetFileName(inputImage);<br><br> //Update the reader<br> try<br> {<br> reader->Update();<br> }<br> catch(itk::ExceptionObject & ex_reader)<br>
{<br> std::cerr << "ExceptionObject caught @ _reader !" << std::endl;<br> std::cout<<ex_reader<<std::endl;<br> return EXIT_FAILURE;<br> }<br><br><br> //Initialize the writer<br>
typedef unsigned char OutputPixelType;<br> typedef itk::Image<OutputPixelType, Dimension> OutputImageType;<br> typedef itk::ImageFileWriter<OutputImageType> WriterType;<br> WriterType::Pointer writer=WriterType::New();<br>
writer->SetImageIO(metaIO);<br> writer->SetFileName(outputHeaderName);<br><br><br> writer->SetInput(reader->GetOutput());<br><br> try<br> {<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & ex)<br>
{<br> std::cerr << "ExceptionObject caught @ _writer !" << std::endl;<br> std::cerr << ex << std::endl;<br> return EXIT_FAILURE;<br> }<br><br> return EXIT_SUCCESS;<br>
}<br><br>