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

Revision 89, 2.4 KB checked in by dave, 11 years ago (diff)

renamed faceident as it's not so simple these days

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                Mean+=*vi;
42        }
43       
44        Mean/=m_Features.size();
45               
46        // subtract the mean
47        FeatureVec SubMean;
48        for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi)
49        {
50                SubMean.push_back(*vi-Mean);
51        }
52       
53        // allocate the transform matrix (this is where it'll run out of memory)
54        cerr<<"Allocating "<<m_FeatureSize*m_FeatureSize*sizeof(float)/1024/1024.0<<" megs for covariance matrix"<<endl;
55        m_EigenTransform = Matrix<float>(m_FeatureSize,m_FeatureSize);
56        m_EigenTransform.Zero();
57       
58        // start by calculating the covariance matrix
59        for (unsigned int i=0; i<m_FeatureSize; i++)
60        {
61                for (unsigned int j=0; j<m_FeatureSize; j++)
62                {
63                        for (FeatureVec::iterator f = SubMean.begin(); f!=SubMean.end(); ++f)
64                        {
65                                m_EigenTransform[i][j]+=(*f)[i]*(*f)[j];
66                        }
67                       
68                        m_EigenTransform[i][j]/=(float)(SubMean.size()-1);
69                }
70        }
71        m_EigenValues = SVD(m_EigenTransform);
72}
73
74void PCA::RunTests()
75{
76        PCA pca(2);
77       
78        Vector<float> in(2);
79        in[0]=2.5; in[1]=2.4;
80        pca.AddFeature(in);
81        in[0]=0.5; in[1]=0.7;
82        pca.AddFeature(in);
83        in[0]=2.2; in[1]=2.9;
84        pca.AddFeature(in);
85        in[0]=1.9; in[1]=2.2;
86        pca.AddFeature(in);
87        in[0]=3.1; in[1]=3.0;
88        pca.AddFeature(in);
89        in[0]=2.3; in[1]=2.7;
90        pca.AddFeature(in);
91        in[0]=2; in[1]=1.6;
92        pca.AddFeature(in);
93        in[0]=1; in[1]=1.1;
94        pca.AddFeature(in);
95        in[0]=1.5; in[1]=1.6;
96        pca.AddFeature(in);
97        in[0]=1.1; in[1]=0.9;
98        pca.AddFeature(in);
99       
100        pca.Calculate();
101}
102
103
104
Note: See TracBrowser for help on using the repository browser.