source: foam/trunk/vision/src/PCA.cpp @ 86

Revision 86, 2.5 KB checked in by dave, 11 years ago (diff)

lots of code added - pca implementation for eigenfaces and lda started

Line 
1// Copyright (C) 2009 foam
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17#include "PCA.h"
18#include "SVD.h"
19#include <iostream>
20
21using namespace std;
22
23PCA::PCA(unsigned int FeatureSize) :
24m_FeatureSize(FeatureSize),
25m_Mean(FeatureSize)
26{
27}
28
29PCA::~PCA()
30{
31}
32
33
34void PCA::Calculate()
35{
36        // calculate the mean
37        Vector<float> Mean(m_FeatureSize);
38        Mean.Zero();
39        for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi)
40        {
41                cerr<<vi->IsInf()<<endl;
42                Mean+=*vi;
43        }
44       
45        Mean/=m_Features.size();
46               
47        // subtract the mean
48        FeatureVec SubMean;
49        for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi)
50        {
51                SubMean.push_back(*vi-Mean);
52        }
53       
54        // allocate the transform matrix (this is where it'll run out of memory)
55        cerr<<"Allocating "<<m_FeatureSize*m_FeatureSize*sizeof(float)/1024/1024<<" megs for covariance matrix"<<endl;
56        m_EigenTransform = Matrix<float>(m_FeatureSize,m_FeatureSize);
57        m_EigenTransform.Zero();
58       
59        // start by calculating the covariance matrix
60        for (unsigned int i=0; i<m_FeatureSize; i++)
61        {
62                for (unsigned int j=0; j<m_FeatureSize; j++)
63                {
64                        for (FeatureVec::iterator f = SubMean.begin(); f!=SubMean.end(); ++f)
65                        {
66                                m_EigenTransform[i][j]+=(*f)[i]*(*f)[j];
67                        }
68                       
69                        m_EigenTransform[i][j]/=(float)(SubMean.size()-1);
70                }
71        }
72        m_EigenValues = SVD(m_EigenTransform);
73}
74
75void PCA::RunTests()
76{
77        PCA pca(2);
78       
79        Vector<float> in(2);
80        in[0]=2.5; in[1]=2.4;
81        pca.AddFeature(in);
82        in[0]=0.5; in[1]=0.7;
83        pca.AddFeature(in);
84        in[0]=2.2; in[1]=2.9;
85        pca.AddFeature(in);
86        in[0]=1.9; in[1]=2.2;
87        pca.AddFeature(in);
88        in[0]=3.1; in[1]=3.0;
89        pca.AddFeature(in);
90        in[0]=2.3; in[1]=2.7;
91        pca.AddFeature(in);
92        in[0]=2; in[1]=1.6;
93        pca.AddFeature(in);
94        in[0]=1; in[1]=1.1;
95        pca.AddFeature(in);
96        in[0]=1.5; in[1]=1.6;
97        pca.AddFeature(in);
98        in[0]=1.1; in[1]=0.9;
99        pca.AddFeature(in);
100       
101        pca.Calculate();
102}
103
104
105
Note: See TracBrowser for help on using the repository browser.