CBICA Toolkit  1.0.0
cbicaITKComputeVarianceMap.h
Go to the documentation of this file.
1 /**
2 \file cbicaITKComputeVarianceMap.h
3 
4 \brief Declaration of the ComputeVarianceMap class
5 
6 https://www.cbica.upenn.edu/sbia/software/ <br>
7 software@cbica.upenn.edu
8 
9 Copyright (c) 2015 University of Pennsylvania. All rights reserved. <br>
10 See COPYING file or https://www.cbica.upenn.edu/sbia/software/license.html
11 
12 */
13 #pragma once
14 
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 #include <sys/stat.h>
19 
20 #include "itkImageIOBase.h"
21 #include "itkImageIOFactory.h"
22 #include "itkDiffusionTensor3D.h"
23 #include "itkImage.h"
24 #include "itkImageFileReader.h"
25 #include "itkImageFileWriter.h"
26 #include "itkNiftiImageIO.h"
27 
29 
30 #include "cbicaITKCommonHolder.h"
31 
32 /*
33 \namespace cbica
34 \brief Namespace for differentiating functions written for internal use
35 */
36 namespace cbica
37 {
38  /**
39  \class ComputeVarianceMap
40 
41  \brief Computes the variance map for a single image.
42 
43  \todo Support for calculating variance for multiple images (groundwork is ready,
44  just need to change the runAlgorithm() function
45  */
47  {
48  public:
49 
50  /**
51  \brief Default Constructor
52 
53  Use the SetParameters() method if this is used
54  */
55  explicit ComputeVarianceMap();
56 
57  /**
58  \brief Actual Constructor
59 
60  ALL scalars are computed.
61 
62  \param inputFileName The Input File Name
63  \param output The output file or directory
64  */
65  explicit ComputeVarianceMap( const std::string &inputFileName, const std::string &output );
66 
67  /**
68  \brief Actual Constructor
69 
70  ALL scalars are computed.
71 
72  \param inputFileNames Vector of input file names
73  \param output The output file or directory
74  */
75  explicit ComputeVarianceMap( const std::vector<std::string> &inputFileNames, const std::string &output );
76 
77  /**
78  \brief Actual Constructor
79 
80  \param inputFileName The Input File Name
81  \param output The output file or directory
82  \param prefix Prefix for output. Disregarded if output is file
83  */
84  explicit ComputeVarianceMap( const std::string &inputFileName, const std::string &output,
85  const std::string &prefix );
86 
87  /**
88  \brief Actual Constructor
89 
90  \param inputFileNames Vector of input file names
91  \param output The output file or directory
92  \param prefix Prefix for output. Disregarded if output is file
93  */
94  explicit ComputeVarianceMap( const std::vector<std::string> &inputFileNames, const std::string &output,
95  const std::string &prefix );
96 
97  /**
98  \brief The Destructor
99  */
100  virtual ~ComputeVarianceMap();
101 
102  /**
103  \brief Set the object parameters for the default () constructor
104 
105  Since this class utilizes only a single image (for multiple images, it can be called in a loop),
106  setting the input as a vector will tell the algorithm to take the first file only.
107 
108  \param inputFileNames Vector of input files
109  \param output Output file/dir
110  */
111  void SetParameters( const std::vector<std::string> &inputFileNames,
112  const std::string &output, const std::string &prefix );
113 
114  private:
115  bool m_verbose;
116 
117  /**
118  \brief Function to call the Variance filter
119  */
120  template <typename PixelType,unsigned int Dimension>
121  void computeVarianceRunner( std::vector<std::string> inpFiles, std::string outputFile )
122  {
123  /*bool isScalar;
124  if ( m_pixelType == itk::ImageIOBase::SCALAR )
125  isScalar = true;
126  else
127  isScalar = false;*/
128 
129  // typedefs
130  typedef typename itk::Image< PixelType, Dimension > InputImageType;
131  typedef typename itk::ImageFileReader< InputImageType > ReaderType;
132  typedef typename ReaderType::Pointer ReaderPointerType;
133  typedef typename itk::ImageFileWriter< InputImageType > WriterType;
135  typename FilterType::Pointer varianceFilter = FilterType::New();
136 
137  // Lets hold on to all the readers?
138  typename std::vector< ReaderPointerType > readers;
139  for (size_t i=0; i<inpFiles.size() ;i++)
140  {
141  typename ReaderType::Pointer reader = ReaderType::New();
142  itk::NiftiImageIO::Pointer imageIOr = itk::NiftiImageIO::New();
143  reader->SetFileName( inpFiles[i] );
144 
145  readers.push_back(reader);
146 
147  reader->SetImageIO( imageIOr );
148  varianceFilter->SetInput(i,reader->GetOutput());
149  }
150 
151  // Write out the result
152  typename WriterType::Pointer writer = WriterType::New();
153  itk::NiftiImageIO::Pointer imageIOw = itk::NiftiImageIO::New();
154  writer->SetImageIO( imageIOw );
155  writer->SetFileName( outputFile );
156  writer->SetInput( varianceFilter->GetOutput() );
157  writer->Update();
158 
159  }
160 
161  /**
162  \brief Runs the algorithm right after the constructor
163  */
164  inline void runAlgorithm();
165 
166  /**
167  \brief Extra check for input files
168  */
169  inline void checkInputs() { /* all required parameters are checked by CommonHolder */ };
170 
171  };
172 
173 }
void SetParameters(const std::vector< std::string > &inputFileNames, const std::string &output, const std::string &prefix)
Set the object parameters for the default () constructor.
virtual ~ComputeVarianceMap()
The Destructor.
Implements an operator for calculating pixel-wise variance of two images.
Definition: itkNaryVarianceImageFilter.h:129
Declaration of the CommonHolder class.
ComputeVarianceMap()
Default Constructor.
Common data container for all statistical computations of images.
Definition: cbicaITKCommonHolder.h:44
Computes the variance map for a single image.
Definition: cbicaITKComputeVarianceMap.h:46
Declaration & Implementation of the NaryFunctorImageFilter class.