Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PCA.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __PCA_hh__
26 #define __PCA_hh__
27 
28 #include <bias_config.h>
29 
30 #include <Base/Debug/Debug.hh>
31 #include <vector>
32 #include <Base/Math/Vector.hh>
33 #include <Base/Math/Matrix.hh>
34 
35 
36 #define PCAType float
37 
38 namespace BIAS {
39 
40  /** @class PCA
41  @brief principal component analysis on a set of vectors
42  with PCA it is possible to find the most important dimensions
43  of a set of vectors. the dimension can be reduced while the
44  vectors stay separable
45  @author haertel 01/06
46  */
47  class BIASMathAlgo_EXPORT PCA {
48 
49  public:
50  PCA():reductionSize_(0) {}
51 
52  /** @brief Computes a reduction-matrix
53 
54  the first dimension of the matrix is taken out of the first vector
55  @param vec the input: a vector of data-vectors
56  @param matrix the matrix returned
57  @param reductionSize the second dimension of the matrix
58  */
59  void ComputeReductionMatrix(const std::vector<BIAS::Vector<PCAType> >&vec,
60  BIAS::Matrix<PCAType> &matrix,
61  int reductionSize=-1);
62 
63 
64  /** @brief Computes a reduction-matrix
65  this method uses all float-vectors for analyzing
66 
67  @param vec the input: a vector of vectors
68  @param matrix the matrix returned
69  @param reductionSize the second dimension of the matrix
70  */
71 
72  void ComputeReductionMatrix(const std::vector<std::vector<
73  BIAS::Vector<PCAType> > > & vec,
74  BIAS::Matrix<PCAType> &matrix,
75  int reductionSize=-1);
76 
77 
78  /** @brief uses scatter matrix cov to compute reduction matrix
79  @param normalize set to true when you want only the most important
80  directions, set to false if you want to compare distances in reduced
81  space without mahalanobis distance */
82  void ComputeReductionMatrix(BIAS::Matrix<PCAType> &cov,
83  BIAS::Matrix<PCAType> &matrix,
84  bool normalize = false,
85  int reductionSize=-1);
86 
87 
88 
89  ///computes mean of a set of vectors
90  void ComputeMean(const std::vector<BIAS::Vector<PCAType> > &vec,
91  BIAS::Vector<PCAType> &mean);
92 
93  /// compute unnomalized covariance
94  void ComputeScatter(const std::vector<BIAS::Vector<PCAType> > &vec,
95  const BIAS::Vector<PCAType> &mean,
97 
98  ///get mean of a vector
99  void GetMean(BIAS::Vector<PCAType> &mean);
100 
101  ///get eigenvalues of data (call after ComputeReductionMatrix)
103  S=S_;
104  };
105 
106  protected:
107 
108  void SetReductionSize(int size);
109 
113 
114  };
115 
116 }
117 
118 #endif
PCA()
Definition: PCA.hh:50
BIAS::Vector< double > S_
Definition: PCA.hh:112
principal component analysis on a set of vectors with PCA it is possible to find the most important d...
Definition: PCA.hh:47
BIAS::Vector< PCAType > mean_
Definition: PCA.hh:111
void GetVariances(BIAS::Vector< double > &S)
get eigenvalues of data (call after ComputeReductionMatrix)
Definition: PCA.hh:102
matrix class with arbitrary size, indexing is row major.
int reductionSize_
Definition: PCA.hh:110