Changeset 97 for foam/trunk/vision
- Timestamp:
- 08/03/2009 01:42:48 PM (11 years ago)
- Location:
- foam/trunk/vision/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
foam/trunk/vision/src/Matrix.h
r96 r97 94 94 void SetRowVector(unsigned int r, const Vector<T> &row); 95 95 void SetColVector(unsigned int c, const Vector<T> &col); 96 96 void NormaliseRows(); 97 void NormaliseCols(); 98 97 99 void Print() const; 98 100 void SetAll(T s); … … 119 121 Matrix CropCols(unsigned int s, unsigned int e); 120 122 121 void Save(FILE *f) ;123 void Save(FILE *f) const; 122 124 void Load(FILE *f); 123 125 … … 230 232 Matrix<T> Matrix<T>::Transposed() const 231 233 { 232 Matrix<T> copy( *this);234 Matrix<T> copy(m_Cols,m_Rows); 233 235 for (unsigned int i=0; i<m_Rows; i++) 234 236 { 235 237 for (unsigned int j=0; j<m_Cols; j++) 236 238 { 237 copy[ i][j]=(*this)[j][i];239 copy[j][i]=(*this)[i][j]; 238 240 } 239 241 } … … 255 257 Matrix<T> Matrix<T>::operator+(const Matrix &other) const 256 258 { 257 assert(m_Rows= other.m_Rows);258 assert(m_Cols= other.m_Cols);259 assert(m_Rows==other.m_Rows); 260 assert(m_Cols==other.m_Cols); 259 261 260 262 Matrix<T> ret(m_Rows,m_Cols); … … 272 274 Matrix<T> Matrix<T>::operator-(const Matrix &other) const 273 275 { 274 assert(m_Rows= other.m_Rows);275 assert(m_Cols= other.m_Cols);276 assert(m_Rows==other.m_Rows); 277 assert(m_Cols==other.m_Cols); 276 278 277 279 Matrix<T> ret(m_Rows,m_Cols); … … 530 532 531 533 template<class T> 532 void Matrix<T>::Save(FILE* f) 534 void Matrix<T>::NormaliseRows() 535 { 536 for(unsigned int i=0; i<m_Rows; i++) 537 { 538 SetRowVector(i,GetRowVector(i).Normalised()); 539 } 540 } 541 542 template<class T> 543 void Matrix<T>::NormaliseCols() 544 { 545 for(unsigned int i=0; i<m_Cols; i++) 546 { 547 SetColVector(i,GetColVector(i).Normalised()); 548 } 549 } 550 551 template<class T> 552 void Matrix<T>::Save(FILE* f) const 533 553 { 534 554 int version = 1; … … 638 658 639 659 assert(j.Inverted()==k); 660 661 Matrix<float> l(2,2); 662 l[0][0]=3; 663 l[0][1]=3; 664 l[1][0]=0; 665 l[1][1]=0; 666 667 Matrix<float> n(2,2); 668 n[0][0]=2; 669 n[0][1]=2; 670 n[1][0]=0; 671 n[1][1]=0; 672 673 n*=l; 674 675 Matrix<float> o(4,4); 676 o.Zero(); 677 o[0][0]=1; 678 o[1][1]=1; 679 o[2][2]=1; 680 o[3][3]=1; 681 682 j*=k; 683 assert(j==o); 684 685 { 686 Matrix<float> a(2,3); 687 Matrix<float> b(3,2); 688 689 a[0][0]=1; a[0][1]=2; a[0][2]=3; 690 a[1][0]=4; a[1][1]=5; a[1][2]=6; 691 692 b[0][0]=2; b[0][1]=3; 693 b[1][0]=-1; b[1][1]=1; 694 b[2][0]=1; b[2][1]=2; 695 696 Matrix<float> result(2,2); 697 result[0][0]=3; result[0][1]=11; 698 result[1][0]=9; result[1][1]=29; 699 700 assert(a*b==result); 701 } 702 640 703 } 641 704 -
foam/trunk/vision/src/PCA.cpp
r96 r97 51 51 52 52 // allocate the transform matrix (this is where it'll run out of memory) 53 cerr<<"Allocating "<<m_FeatureSize*m_FeatureSize*sizeof(float)/1024/1024.0<<" megs for covariance matrix"<<endl; 53 float size= m_FeatureSize*m_FeatureSize*sizeof(float)/1024/1024.0; 54 if (size>1) 55 { 56 cerr<<"Allocating "<<size<<" megs for covariance matrix"<<endl; 57 } 54 58 m_EigenTransform = Matrix<float>(m_FeatureSize,m_FeatureSize); 55 59 m_EigenTransform.Zero(); … … 92 96 } 93 97 94 void PCA::Save(FILE *f) 98 void PCA::Save(FILE *f) const 95 99 { 96 100 int version = 2; … … 158 162 159 163 pca.Calculate(); 160 164 165 161 166 in[0]=.69; in[1]=.49; 162 167 Vector<float> out = pca.Project(in); 163 168 assert(feq(out[0],-0.82797f) && feq(out[1],-0.175115f)); 169 170 PCA pcasub(2); 171 172 in[0]=-0.677; in[1]=-0.735; 173 pcasub.AddFeature(in); 174 in[0]=0; in[1]=0; 175 pcasub.AddFeature(in); 176 in[0]=0.677; in[1]=0.735; 177 pcasub.AddFeature(in); 178 179 pcasub.Calculate(); 180 pca.Mult(pca); 181 164 182 165 183 } -
foam/trunk/vision/src/PCA.h
r96 r97 50 50 const Vector<float> &GetEigenValues() const { return m_EigenValues; } 51 51 const Matrix<float> &GetEigenTransform() const { return m_EigenTransform; } 52 Matrix<float> &EigenTransform() { return m_EigenTransform; } 52 53 const FeatureVec &GetFeatures() const { return m_Features; } 53 54 const Vector<float> &GetMean() const { return m_Mean; } … … 57 58 58 59 void Load(FILE *f); 59 void Save(FILE *f) ;60 void Save(FILE *f) const; 60 61 61 62 private: -
foam/trunk/vision/src/Vector.h
r91 r97 60 60 T DistanceFrom(const Vector &other) const; 61 61 T Magnitude() const; 62 Vector Normalised() const; 62 63 63 64 Vector &operator=(const Vector &other); … … 75 76 Vector &operator/=(T v); 76 77 77 void Save(FILE *f) ;78 void Save(FILE *f) const; 78 79 void Load(FILE *f); 79 80 … … 186 187 187 188 template<class T> 189 Vector<T> Vector<T>::Normalised() const 190 { 191 Vector<T> ret(*this); 192 ret/=ret.Magnitude(); 193 return ret; 194 } 195 196 template<class T> 188 197 Vector<T> Vector<T>::operator+(const Vector &other) const 189 198 { … … 329 338 330 339 template<class T> 331 void Vector<T>::Save(FILE* f) 340 void Vector<T>::Save(FILE* f) const 332 341 { 333 342 int version = 1;
Note: See TracChangeset
for help on using the changeset viewer.