Orfeo Toolbox  3.16
itkImagePCADecompositionCalculator.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImagePCADecompositionCalculator.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:02:56 $
7  Version: $Revision: 1.7 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 
19 #ifndef __itkImagePCADecompositionCalculator_txx
20 #define __itkImagePCADecompositionCalculator_txx
21 #ifdef _MSC_VER
22 #pragma warning( disable : 4288 )
23 #endif
24 
27 
28 namespace itk
29 {
30 
34 template<class TInputImage, class TBasisImage>
37 {
38  m_Image = NULL;
39  m_MeanImage = NULL;
40  m_BasisMatrixCalculated = false;
41 }
42 
43 template<class TInputImage, class TBasisImage>
44 void
47 {
48  itkDebugMacro(<< "setting BasisImages");
49  this->m_BasisMatrixCalculated = false;
50  // We need this modified setter function so that the calculator
51  // can cache the basis set between calculations. Note that computing the
52  // basis matrix from the input images is rather expensive, and the basis
53  // images are likely to be changed less often than the input images. So
54  // it makes sense to try to cache the pre-computed matrix.
55 
56  this->m_BasisImages = v;
57  this->Modified();
58 }
59 
63 template<class TInputImage, class TBasisImage>
64 void
66 ::Compute(void)
67 {
68  if (!m_BasisMatrixCalculated)
69  {
70  this->CalculateBasisMatrix();
71  }
72  this->CalculateRecenteredImageAsVector();
73  m_Projection = m_BasisMatrix * m_ImageAsVector;
74 }
75 
76 /*
77  * Convert a vector of basis images into a matrix. Each image is flattened into 1-D.
78  */
79 template<class TInputImage, class TBasisImage>
80 void
83  m_Size = m_BasisImages[0]->GetRequestedRegion().GetSize();
84  m_NumPixels = 1;
85  for( unsigned int i = 0; i < BasisImageDimension; i++ )
86  {
87  m_NumPixels *= m_Size[i];
88  }
89 
90  m_BasisMatrix = BasisMatrixType(m_BasisImages.size(), m_NumPixels);
91 
92  int i = 0;
93  for(typename BasisImagePointerVector::const_iterator basis_it = m_BasisImages.begin();
94  basis_it != m_BasisImages.end(); ++basis_it)
95  {
96  if( (*basis_it)->GetRequestedRegion().GetSize() != m_Size)
97  {
98  itkExceptionMacro("All basis images must be the same size!");
99  }
100 
101  ImageRegionConstIterator<BasisImageType> image_it(*basis_it,
102  (*basis_it)->GetRequestedRegion());
103  int j = 0;
104  for (image_it.GoToBegin(); !image_it.IsAtEnd(); ++image_it)
105  {
106  m_BasisMatrix(i, j++) = image_it.Get();
107  }
108  i++;
109  }
110  m_BasisMatrixCalculated = true;
111  m_ImageAsVector.set_size(m_NumPixels);
112 }
113 
117 template<class TInputImage, class TBasisImage>
118 void
121  if ( m_Image->GetRequestedRegion().GetSize() != m_Size)
122  {
123  itkExceptionMacro("Input image must be the same size as the basis images!");
124  }
125 
127  m_Image->GetRequestedRegion());
128  typename BasisVectorType::iterator vector_it;
129  for (image_it.GoToBegin(), vector_it = m_ImageAsVector.begin();
130  !image_it.IsAtEnd(); ++image_it, ++vector_it)
131  {
132  *vector_it = static_cast<BasisPixelType> (image_it.Get());
133  }
134 
135  if (m_MeanImage)
136  {
137  ImageRegionConstIterator<BasisImageType> mimage_it(m_MeanImage,
138  m_MeanImage->GetRequestedRegion());
139  for (mimage_it.GoToBegin(), vector_it = m_ImageAsVector.begin();
140  !mimage_it.IsAtEnd(); ++mimage_it, ++vector_it)
141  {
142  *vector_it -= (mimage_it.Get());
143  }
144  }
145 }
146 
147 
148 template<class TInputImage, class TBasisImage>
149 void
153  unsigned int nImages = model->GetNumberOfPrincipalComponentsRequired();
154  images.reserve(nImages);
155  for(int i = 1; i <= nImages; i++)
156  {
157  images.push_back(model->GetOutput(i));
158  }
159  this->SetBasisImages(images);
160  this->SetMeanImage(model->GetOutput(0));
161 }
162 
163 template<class TInputImage, class TBasisImage>
164 void
166 ::PrintSelf( std::ostream& os, Indent indent ) const
167 {
168  Superclass::PrintSelf(os,indent);
169  os << indent << "Projection: " << m_Projection << std::endl;
170  os << indent << "Image: " << m_Image.GetPointer() << std::endl;
171  os << indent << "Mean Image: " << m_MeanImage.GetPointer() << std::endl;
172 }
173 
174 } // end namespace itk
175 
176 #endif

Generated at Sat Feb 2 2013 23:43:24 for Orfeo Toolbox with doxygen 1.8.1.1