CBICA Toolkit  1.0.0
cbicaITKComputeAverageMap.h
Go to the documentation of this file.
1 /**
2 \file cbicaITKComputeAverageMap.h
3 
4 \brief Declaration of the ComputeAverageMap class
5 
6 This class computes the average of a series of 2 or 3 dimension images.
7 
8 https://www.cbica.upenn.edu/sbia/software/ <br>
9 software@cbica.upenn.edu
10 
11 Copyright (c) 2015 University of Pennsylvania. All rights reserved. <br>
12 See COPYING file or https://www.cbica.upenn.edu/sbia/software/license.html
13 
14 */
15 #pragma once
16 
17 #include <iostream>
18 #include <string>
19 #include <vector>
20 
21 #include "itkImageIOBase.h"
22 #include "itkImageIOFactory.h"
23 #include "itkDiffusionTensor3D.h"
24 #include "itkImage.h"
25 #include "itkImageFileReader.h"
26 #include "itkImageFileWriter.h"
27 #include "itkNiftiImageIO.h"
28 
29 #include "cbicaITKCommonHolder.h"
31 #include "itkNaryMeanImageFilter.h"
33 #include "itkDiffusionTensor3D.h"
34 
35 
36 using namespace itk;
37 
38 /*
39 \namespace cbica
40 \brief Namespace for differentiating functions written for internal use
41 */
42 namespace cbica
43 {
44  /**
45  \class ComputeAverageMap
46 
47  \brief Computes the average of a series of images and writes the output
48  */
50  {
51  public:
52 
53  /**
54  \brief Default Constructor
55  */
56  explicit ComputeAverageMap();
57 
58  /**
59  \brief Actual Constructor
60 
61  \param inputFiles A vector of strings containing file names
62  \param output The directory/file in which to write the files
63  */
64  explicit ComputeAverageMap( const std::vector<std::string> &inputFiles,
65  const std::string &output);
66 
67  /**
68  \brief Actual Constructor
69 
70  \param inputFiles A vector of strings containing file names
71  \param output The directory/file in which to write the files
72  \param verbose A boolean to show verbose output
73  */
74  explicit ComputeAverageMap( const std::vector<std::string> &inputFiles,
75  const std::string &output,
76  const bool verbose);
77 
78  /**
79  \brief Actual Constructor
80 
81  \param inputFiles A vector of strings containing file names
82  \param output The directory/file in which to write the files
83  \param prefix A string which is a prefix for all written files
84  */
85  explicit ComputeAverageMap( const std::vector<std::string> &inputFiles,
86  const std::string &output,
87  const std::string &prefix);
88 
89  /**
90  \brief Actual Constructor
91 
92  \param inputFiles A vector of strings containing file names
93  \param output The directory/file in which to write the files
94  \param verbose A boolean to show verbose output
95  \param prefix A string which is a prefix for all written files
96  */
97  explicit ComputeAverageMap( const std::vector<std::string> &inputFiles,
98  const std::string &output,
99  const bool verbose, const std::string &prefix);
100 
101  /**
102  \brief The Destructor
103  */
104  virtual ~ComputeAverageMap();
105 
106  /**
107  \brief Set the object parameters for the default () constructor
108 
109  Since this class utilizes only a single image (for multiple images, it can be called in a loop),
110  setting the input as a vector will tell the algorithm to take the first file only.
111 
112  \param inputFileNames Vector of input files
113  \param output Output file/dir
114  */
115  void SetParameters( const std::vector<std::string> &inputFileNames,
116  const std::string &output, const std::string &prefix );
117 
118  protected:
119 
120  /**
121  \brief Main mean algorithm
122  */
123  template< typename PixelType, unsigned int Dimension >
124  inline void computeMeanRunner( std::vector<std::string> &inpFiles, std::string &outputBase )
125  {
126  typedef typename itk::Image< PixelType, Dimension > InputImageType;
127  typedef typename itk::ImageFileReader< InputImageType > ReaderType;
128  typedef typename ReaderType::Pointer ReaderPointerType;
129  typedef typename itk::ImageFileWriter< InputImageType > WriterType;
131  typename FilterType::Pointer meanFilter = FilterType::New();
132 
133  // Lets hold on to all the readers
134  typename std::vector< ReaderPointerType > readers;
135  for( unsigned int i=0; i<inpFiles.size(); i++ )
136  {
137  typename ReaderType::Pointer reader = ReaderType::New();
138  itk::NiftiImageIO::Pointer imageIOr = itk::NiftiImageIO::New();
139  reader->SetFileName( inpFiles[i] );
140 
141  readers.push_back(reader);
142 
143  reader->SetImageIO( imageIOr );
144  meanFilter->SetInput(i,reader->GetOutput());
145  }
146 
147  // Write out the result
148  typename WriterType::Pointer writer = WriterType::New();
149  itk::NiftiImageIO::Pointer imageIOw = itk::NiftiImageIO::New();
150  writer->SetImageIO( imageIOw );
151  writer->SetFileName( outputBase );
152 
153  writer->SetInput( meanFilter->GetOutput() );
154 
155  writer->Update();
156 
157  }
158 
159  /**
160  \brief Main mean algorithm for DTI images
161  */
162  template <typename PixelType,unsigned int Dimension>
163  inline void computeMeanDTIRunner( std::vector<std::string> &inpFiles, std::string &outputBase )
164  {
165  typedef typename itk::Image< PixelType, Dimension > InputImageType;
166  typedef typename itk::ImageFileReader< InputImageType > ReaderType;
167  typedef typename ReaderType::Pointer ReaderPointerType;
168  typedef typename itk::ImageFileWriter< InputImageType > WriterType;
170  < InputImageType, InputImageType > FilterType;
171  typename FilterType::Pointer meanFilter = FilterType::New();
172 
173  // Lets hold on to all the readers
174  typename std::vector< ReaderPointerType > readers;
175  for( unsigned int i=0; i<inpFiles.size(); i++ )
176  {
177  typename ReaderType::Pointer reader = ReaderType::New();
178  itk::NiftiImageIO::Pointer imageIOr = itk::NiftiImageIO::New();
179  reader->SetFileName( inpFiles[i] );
180 
181  readers.push_back(reader);
182 
183  reader->SetImageIO( imageIOr );
184  meanFilter->SetInput(i,reader->GetOutput());
185  }
186 
187  // Write out the result
188  typename WriterType::Pointer writer = WriterType::New();
189  itk::NiftiImageIO::Pointer imageIOw = itk::NiftiImageIO::New();
190  writer->SetImageIO( imageIOw );
191  writer->SetFileName( outputBase );
192 
193  writer->SetInput( meanFilter->GetOutput() );
194 
195  writer->Update();
196  }
197 
198  /**
199  \brief Runs the algorithm
200  */
201  inline void runAlgorithm();
202 
203  private:
204  bool m_verbose, m_isScalar;
205  int m_dimensions;
206 
207  /**
208  \brief Invokes the computeRunner functions
209  */
210  template <const unsigned int Dimension>
211  inline void invokeRunner()
212  {
213  if( m_isScalar )
214  {
215  std::cout << "Computing normal Average Map...\n";
216  switch( m_componentType )
217  {
218  case itk::ImageIOBase::CHAR:
219  {
220  computeMeanRunner<char, Dimension>(m_inputFiles, m_outputFiles[0]);
221  break;
222  }
223  case itk::ImageIOBase::UCHAR:
224  {
225  computeMeanRunner<unsigned char, Dimension>(m_inputFiles, m_outputFiles[0]);
226  break;
227  }
228  case itk::ImageIOBase::SHORT:
229  {
230  computeMeanRunner<short, Dimension>(m_inputFiles, m_outputFiles[0]);
231  break;
232  }
233  case itk::ImageIOBase::USHORT:
234  {
235  computeMeanRunner<unsigned short, Dimension>(m_inputFiles, m_outputFiles[0]);
236  break;
237  }
238  case itk::ImageIOBase::INT:
239  {
240  computeMeanRunner<int, Dimension>(m_inputFiles, m_outputFiles[0]);
241  break;
242  }
243  case itk::ImageIOBase::UINT:
244  {
245  computeMeanRunner<unsigned int, Dimension>(m_inputFiles, m_outputFiles[0]);
246  break;
247  }
248  case itk::ImageIOBase::LONG:
249  {
250  computeMeanRunner<long, Dimension>(m_inputFiles, m_outputFiles[0]);
251  break;
252  }
253  case itk::ImageIOBase::ULONG:
254  {
255  computeMeanRunner<unsigned long, Dimension>(m_inputFiles, m_outputFiles[0]);
256  break;
257  }
258  case itk::ImageIOBase::FLOAT:
259  {
260  computeMeanRunner<float, Dimension>(m_inputFiles, m_outputFiles[0]);
261  break;
262  }
263  case itk::ImageIOBase::DOUBLE:
264  {
265  computeMeanRunner<double, Dimension>(m_inputFiles, m_outputFiles[0]);
266  break;
267  }
268  default:
269  {
270  std::cerr << "Unsupported component type: " << m_componentType_asString << std::endl;
271  break;
272  }
273  }
274  }
275 
276  else if( m_pixelType == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR ||
277  m_pixelType == itk::ImageIOBase::DIFFUSIONTENSOR3D )
278  {
279  std::cout << "Computing Average Map from Tensor...\n";
280  switch( m_componentType )
281  {
282  /*case itk::ImageIOBase::CHAR:
283  {
284  computeMeanDTIRunner<char, Dimension>(m_inputFiles, m_output);
285  break;
286  }
287  case itk::ImageIOBase::UCHAR:
288  {
289  computeMeanDTIRunner<unsigned char, Dimension>(m_inputFiles, m_output);
290  break;
291  }
292  case itk::ImageIOBase::SHORT:
293  {
294  computeMeanDTIRunner<short, Dimension>(m_inputFiles, m_output);
295  break;
296  }
297  case itk::ImageIOBase::USHORT:
298  {
299  computeMeanDTIRunner<unsigned short, Dimension>(m_inputFiles, m_output);
300  break;
301  }
302  case itk::ImageIOBase::INT:
303  {
304  computeMeanDTIRunner<int, Dimension>(m_inputFiles, m_output);
305  break;
306  }
307  case itk::ImageIOBase::UINT:
308  {
309  computeMeanDTIRunner<unsigned int, Dimension>(m_inputFiles, m_output);
310  break;
311  }
312  case itk::ImageIOBase::LONG:
313  {
314  computeMeanDTIRunner<long, Dimension>(m_inputFiles, m_output);
315  break;
316  }
317  case itk::ImageIOBase::ULONG:
318  {
319  computeMeanDTIRunner<unsigned long, Dimension>(m_inputFiles, m_output);
320  break;
321  }*/
322  case itk::ImageIOBase::FLOAT:
323  {
324  computeMeanDTIRunner<float, Dimension>(m_inputFiles, m_outputFiles[0]);
325  break;
326  }
327  case itk::ImageIOBase::DOUBLE:
328  {
329  computeMeanDTIRunner<double, Dimension>(m_inputFiles, m_outputFiles[0]);
330  break;
331  }
332  default:
333  {
334  std::cerr << "Unsupported component type: " << m_componentType_asString << std::endl;
335  break;
336  }
337  }
338  }
339 
340  else
341  {
342  std::cerr << "Unsupported pixel type: " << m_pixelType_asString << std::endl <<
343  "ITKComputeAverageMap class can only work with Scalar or diffusion tensor images.\n";
344  }
345  }
346 
347  /**
348  \brief Extra check for input files
349  */
350  inline void checkInputs() { /* all required parameters are checked by CommonHolder */ };
351 
352  };
353 }
Declaration & Implementation of the NaryMeanDiffusionTensorImageFilter class.
Declaration & Implementation of the NaryMeanImageFilter class.
Declaration of the CommonHolder class.
void computeMeanRunner(std::vector< std::string > &inpFiles, std::string &outputBase)
Main mean algorithm.
Definition: cbicaITKComputeAverageMap.h:124
Computes the average of a series of images and writes the output.
Definition: cbicaITKComputeAverageMap.h:49
Common data container for all statistical computations of images.
Definition: cbicaITKCommonHolder.h:44
void computeMeanDTIRunner(std::vector< std::string > &inpFiles, std::string &outputBase)
Main mean algorithm for DTI images.
Definition: cbicaITKComputeAverageMap.h:163
Declaration of the DTILogEuclideanCalculator class.
Implements an operator for pixel-wise averaging of two images.
Definition: itkNaryMeanImageFilter.h:81
Implements an operator for pixel-wise averaging of two Diffusion Tensor images.
Definition: itkNaryMeanDiffusionTensorImageFilter.h:152