DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
SparseToVectorImageConverter.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Sparse to Vector Image Converter
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 #include "itkImageFileWriter.h"
15 #include "itkImageIOBase.h"
16 #include "itkVectorImage.h"
20 #include "itkImageRegionIterator.h"
21 #include "itkImageRegionConstIterator.h"
22 #include "SparseToVectorImageConverterCLP.h"
23 
24 int
25 main(int argc, char *argv[])
26 {
27  PARSE_ARGS;
28 
29  // Define Variables
30  typedef double PixelType;
32  typedef itk::VectorImage<PixelType, 3> OutputImageType;
34 
35  InputImageType::Pointer inputImage;
36  OutputImageType::Pointer outputImage = OutputImageType::New();
37  ReaderType::Pointer reader = ReaderType::New();
38 
39  typedef itk::CastVectorImageFileWriter<OutputImageType> OutputImageWriterType;
40  OutputImageWriterType::Pointer writer = OutputImageWriterType::New();
41 
42  // Read input image
43  reader->SetFileName(_InputFile);
44 
45  try
46  {
47  std::cout << "Reading file: " << _InputFile << std::endl;
48  reader->Update();
49  }
50  catch (itk::ExceptionObject & err)
51  {
52  std::cerr << "ExceptionObject caught!" << std::endl;
53  std::cerr << err << std::endl;
54  return EXIT_FAILURE;
55  }
56 
57  inputImage = reader->GetOutput();
58 
59  // Allocate output image
60  outputImage->CopyInformation(inputImage);
61  outputImage->SetRegions(inputImage->GetLargestPossibleRegion());
62  outputImage->Allocate();
63 
64  // Iterator for the input image
65  itk::ImageRegionConstIterator<InputImageType> inputIt(inputImage, inputImage->GetLargestPossibleRegion() );
66 
67  // Iterator for the output image
68  itk::ImageRegionIterator<OutputImageType> outputIt(outputImage, inputImage->GetLargestPossibleRegion() );
69 
70  inputIt.GoToBegin();
71  outputIt.GoToBegin();
72 
73  // Transfer data
74  while( !inputIt.IsAtEnd() )
75  {
76  outputIt.Set(inputIt.Get());
77 
78  ++inputIt;
79  ++outputIt;
80  }
81 
82  // Write Output
83  if (_OutputFileArg.isSet())
84  {
85  reader->UpdateOutputInformation();
86  itk::ImageIOBase::Pointer inputImageIOBase = reader->GetImageIO();
87  inputImageIOBase->ReadImageInformation();
88  itk::ImageIOBase::IOComponentType inputImageComponentType =
89  inputImageIOBase->GetComponentType();
90 
91  try
92  {
93  std::cout << "Writing file: " << _OutputFile << std::endl;
94  writer->SetFileName( _OutputFile );
95  writer->SetInput( outputImage );
96  writer->SetComponentType( inputImageComponentType );
97  writer->Update();
98  }
99  catch ( itk::ExceptionObject & err )
100  {
101  std::cerr << "ExceptionObject caught!" << std::endl;
102  std::cerr << err << std::endl;
103  return EXIT_FAILURE;
104  }
105  }
106 
107  return EXIT_SUCCESS;
108 }
109 
Writes image data, after casting, to a single file.
int main(int argc, char *argv[])
An n-dimensional vector image with a sparse memory model.