Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LDA.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 __LDA_hh__
26 #define __LDA_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 #define LDAType float
36 
37 
38 namespace BIAS {
39 
40  /** @class LDA
41  @brief linear discriminant analysis on a set of classes of vectors
42  LDA is a approach for dimension-reduction of vector-classes while keeping
43  class-separability
44  @author haertel 01/06
45  */
46  class BIASMathAlgo_EXPORT LDA : public BIAS::Debug {
47 
48  public:
49 
50  LDA(bool equalClassWeights=true):equalClassWeights_(equalClassWeights){}
51 
52  /** @brief Computes a reduction-matrix
53  the first dimension of the matrix is taken out of the first vector
54  @param vec the input: a vector of classes of vectors
55  @param matrix the matrix returned
56  @param reductionSize the second dimension of the matrix
57  @param covs pass vector of correct size to retrieve class covs, leave
58  zero size vector to save memory and destroy covs after usage
59  */
60  void ComputeReductionMatrix(const std::vector<std::vector<
61  BIAS::Vector<LDAType> > > &vec,
62  BIAS::Matrix<LDAType> &matrix,
63  int reductionSize=-1,
64  std::vector<BIAS::Matrix<LDAType> > *covs =
65  NULL);
66 
67  /** @brief computes reduction, given average within class cov and
68  inter-class cov matrix */
69  void ComputeReductionMatrix(BIAS::Matrix<LDAType>& classCov,
71  BIAS::Matrix<LDAType> &matrix,
72  int reductionSize);
73 
74  /** @brief given mean and scatter of each class, compute
75  scatter between class means and average class shape */
76  void ComputeWithinAndInterClassCovs(const std::vector<BIAS::Vector<LDAType>
77  >& classmeans,
78  const std::vector<BIAS::Matrix<LDAType>
79  >& covs,
80  BIAS::Matrix<LDAType>& withinClassCov,
81  BIAS::Matrix<LDAType>& interClassCov);
82 
83 
84  /** @brief Computes a reduction-matrix for features of
85  n sets (e.g. n images), whose class relations (correspondence) are
86  anonymous
87  @param vec the input: a vector (one entry for each image) of vectors
88  of features
89  @param matrix the matrix returned
90  @param reductionSize the second dimension of the matrix
91  @param covs vector of covs for each feature in vec
92  */
93  void ComputeAnonymousReduction(const std::vector<std::vector<
94  BIAS::Vector<LDAType> > > &vec,
95  BIAS::Matrix<LDAType> &matrix,
96  int reductionSize=-1,
97  const std::vector< std::vector<BIAS::
98  Matrix<LDAType> > >*covs = NULL);
99 
100 
101  /** @brief get mean of input
102  get results with methods down under
103  */
104  void ComputeMeans(const std::vector<std::vector<BIAS::Vector<LDAType> > >
105  &vec);
106 
107  /** @brief get a mean vector of all vectors
108  ComputeReductionMatrix or ComputeMeans has to be called first
109  */
110  void GetMean(BIAS::Vector<LDAType> &mean);
111 
112  /** @brief get mean vector for each class
113  ComputeReductionMatrix or ComputeMeans has to be called first
114  */
115  void GetClassMeans(std::vector<BIAS::Vector<LDAType> > &means);
116 
117  ///reduction size can be set here
118  void SetReductionSize(int size);
119 
120  /** @brief Generates random classes of vectors
121 
122  @param vectorSize dimensionality
123  @param numberOfClasses how many classes should be generated (number of mean-vectors)
124  @param classSizeMin how many vectors should at least be in every class
125  @param classSizeMax how many vectors should at most be in every class
126  @param vectorEntryMin the min-value of every vectors component
127  @param vectorEntryMax the max-value of every vectors component
128  @param stdDeviation Standard-Deviation of how a vector is distributed around it's class mean
129  @param mainDirectionStdDeviation Standard-Deviation of the same main-direction of all classes
130  @param vec returns a vector of classes, every class is a vector of vectors
131  @param means returns the original mean-vectors used for each class-generation
132  @param Normalize should descriptors be normalized?
133  */
134  void GenerateRandomTestData(int vectorSize, int numberOfClasses,
135  int classSizeMin, int classSizeMax, int vectorEntryMin, int vectorEntryMax,
136  double stdDeviation, double mainDirectionStdDeviation,
137  std::vector<std::vector<BIAS::Vector<LDAType> > > &vec,
138  std::vector<BIAS::Vector<LDAType> > &means, bool Normalize);
139 
140  /** @brief Ouput statistical-data of input-data
141 
142  @param vec the input data
143  */
144 
145  void AnalyzeData(const std::vector<std::vector<BIAS::Vector<LDAType> > > &vec);
146 
147  protected:
148 
149  /// mean vector for each class
150  std::vector<BIAS::Vector<LDAType> > classMeans_;
151  /// mean of all vectors
153  ///reduction size
157  };
158 
159 }
160 
161 #endif
bool equalClassWeights_
Definition: LDA.hh:156
std::vector< BIAS::Vector< LDAType > > classMeans_
mean vector for each class
Definition: LDA.hh:150
linear discriminant analysis on a set of classes of vectors LDA is a approach for dimension-reduction...
Definition: LDA.hh:46
LDA(bool equalClassWeights=true)
Definition: LDA.hh:50
int allCount_
Definition: LDA.hh:155
matrix class with arbitrary size, indexing is row major.
int reductionSize_
reduction size
Definition: LDA.hh:154
BIAS::Vector< LDAType > mean_
mean of all vectors
Definition: LDA.hh:152