Changeset 90 for foam/trunk/vision/src/PCA.cpp
 Timestamp:
 07/22/2009 10:59:15 AM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

foam/trunk/vision/src/PCA.cpp
r89 r90 35 35 { 36 36 // calculate the mean 37 Vector<float> Mean(m_FeatureSize); 38 Mean.Zero(); 37 m_Mean.Zero(); 39 38 for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi) 40 39 { 41 Mean+=*vi;40 m_Mean+=*vi; 42 41 } 43 42 44 Mean/=m_Features.size();43 m_Mean/=m_Features.size(); 45 44 46 45 // subtract the mean … … 48 47 for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi) 49 48 { 50 SubMean.push_back(*vi Mean);49 SubMean.push_back(*vim_Mean); 51 50 } 52 51 … … 70 69 } 71 70 m_EigenValues = SVD(m_EigenTransform); 71 m_EigenTransform=m_EigenTransform.Transposed(); 72 } 73 74 void PCA::Compress(unsigned int s, unsigned int e) 75 { 76 m_EigenTransform=m_EigenTransform.CropRows(s,e); 77 } 78 79 Vector<float> PCA::Project(Vector<float> v) 80 { 81 return m_EigenTransform*v; 82 } 83 84 Vector<float> PCA::Synth(Vector<float> v) 85 { 86 return m_Mean+m_EigenTransform.VecMulTransposed(v); 87 } 88 89 void PCA::Save(FILE *f) 90 { 91 int version = 2; 92 fwrite(&version,sizeof(version),1,f); 93 m_EigenTransform.Save(f); 94 m_EigenValues.Save(f); 95 m_Mean.Save(f); 96 fwrite(&m_FeatureSize,sizeof(m_FeatureSize),1,f); 97 for (unsigned int i=0; i<m_Features.size(); i++) 98 { 99 m_Features[i].Save(f); 100 } 101 102 } 103 104 void PCA::Load(FILE *f) 105 { 106 int version; 107 fread(&version,sizeof(version),1,f); 108 m_EigenTransform.Load(f); 109 m_EigenValues.Load(f); 110 111 if (version == 1) 112 { 113 m_EigenTransform=m_EigenTransform.Transposed(); 114 } 115 116 if (version>1) 117 { 118 m_Mean.Load(f); 119 fread(&m_FeatureSize,sizeof(m_FeatureSize),1,f); 120 for (unsigned int i=0; i<m_Features.size(); i++) 121 { 122 m_Features[i].Load(f); 123 } 124 } 72 125 } 73 126 74 127 void PCA::RunTests() 75 128 { 129 Matrix<float>::RunTests(); 76 130 PCA pca(2); 77 131 … … 99 153 100 154 pca.Calculate(); 155 156 in[0]=.69; in[1]=.49; 157 Vector<float> out = pca.Project(in); 158 assert(feq(out[0],0.82797f) && feq(out[1],0.175115f)); 159 101 160 } 102 161
Note: See TracChangeset
for help on using the changeset viewer.