DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
VectorToSparseImageConverter.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Vector to Sparse 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 "itkVectorImage.h"
17 #include "itkImageFileReader.h"
19 #include "itkImageRegionIterator.h"
20 #include "itkImageRegionConstIterator.h"
21 #include "VectorToSparseImageConverterCLP.h"
22 
23 int
24 main(int argc, char *argv[])
25 {
26  PARSE_ARGS;
27 
28  // Define Variables
29  typedef double PixelType;
30  typedef itk::VectorImage<PixelType, 3> InputImageType;
32  typedef itk::ImageFileReader<InputImageType> ReaderType;
33 
34  InputImageType::Pointer inputImage;
35  OutputImageType::Pointer outputImage = OutputImageType::New();
36  ReaderType::Pointer reader = ReaderType::New();
37 
39  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  try
86  {
87  std::cout << "Writing file: " << _OutputFile << std::endl;
88  writer->SetFileName( _OutputFile );
89  writer->SetInput( outputImage );
90  writer->Update();
91  }
92  catch ( itk::ExceptionObject & err )
93  {
94  std::cerr << "ExceptionObject caught!" << std::endl;
95  std::cerr << err << std::endl;
96  return EXIT_FAILURE;
97  }
98  }
99 
100  return EXIT_SUCCESS;
101 }
102 
int main(int argc, char *argv[])
Writes sparse vector image data to key and value files.
An n-dimensional vector image with a sparse memory model.