Changeset 96 for foam/trunk
- Timestamp:
- 07/29/2009 03:49:34 PM (10 years ago)
- Location:
- foam/trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
foam/trunk/eigentools/src/main.cpp
r94 r96 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 1 17 #define CV_NO_BACKWARD_COMPATIBILITY 2 18 … … 33 49 double scale = 1; 34 50 35 int w=50;36 int h=80;37 //int w=20;38 //int h=30;51 //int w=50; 52 //int h=80; 53 int w=20; 54 int h=30; 39 55 40 56 PCA pca(w*h); … … 46 62 glob_t g; 47 63 48 glob("../data/images/faces/ spacek-large/*.png",GLOB_PERIOD,NULL,&g);64 glob("../data/images/faces/dave/*.png",GLOB_PERIOD,NULL,&g); 49 65 for (unsigned int n=0; n<g.gl_pathc; n++) 50 66 { … … 62 78 } 63 79 64 void TestPCA() 65 { 66 //Recalc(); 67 //FILE *f=fopen("spacek-50x80.pca", "wb"); 68 //pca.Save(f); 69 70 FILE *f=fopen("../data/eigenspaces/spacek-50x80.pca", "rb"); 80 PCA LoadPCA(string filename) 81 { 82 PCA pca(1); 83 FILE *f=fopen(filename.c_str(), "rb"); 71 84 pca.Load(f); 72 85 fclose(f); 86 return pca; 87 } 88 89 void TestPCA() 90 { 91 //Recalc(); 92 //FILE *f=fopen("davelight-20x30.pca", "wb"); 93 //pca.Save(f); 94 pca = LoadPCA("../data/eigenspaces/spacek-20x30.pca"); 95 96 PCA davelight = LoadPCA("davelight-20x30.pca"); 97 //pca.Mult(davelight); 98 pca = davelight; 73 99 74 100 pca.Compress(0,100); … … 79 105 80 106 } 107 81 108 82 109 … … 298 325 299 326 static float t=0; 300 301 for (unsigned int i=0; i< 30; i++)302 { 303 camera.Blit(Image(w,h,1,(pca.GetEigenTransform().GetRowVector(i)*50 )/((i+1) * 1)+pca.GetMean()304 ),(i% 10)*(w+2),0+(i/10)*(h+2));327 cerr<<sin(t)<<endl; 328 for (unsigned int i=0; i<100; i++) 329 { 330 camera.Blit(Image(w,h,1,(pca.GetEigenTransform().GetRowVector(i)*50*sin(t))/((i+1) * 1)+pca.GetMean() 331 ),(i%30)*(w+2),0+(i/30)*(h+2)); 305 332 } 306 333 -
foam/trunk/faceclassifier/src/App.cpp
r95 r96 18 18 #include "App.h" 19 19 #include "PCAClassifier.h" 20 #include "FileTools.h" 20 21 21 22 //#define SAVE_FRAMES 22 23 23 24 using namespace std; 25 26 //int w=50; 27 //int h=80; 24 28 25 29 int w=20; … … 59 63 pca.Load(f); 60 64 fclose(f); 61 pca.Compress( 3,30);65 pca.Compress(20,300); 62 66 63 67 m_Classifier = new PCAClassifier(pca); 64 m_FaceBank = new FaceBank(w,h,0. 2,0.1,m_Classifier);68 m_FaceBank = new FaceBank(w,h,0.4,0.1,m_Classifier); 65 69 cvInitFont( &m_Font, CV_FONT_HERSHEY_PLAIN, 0.5, 0.5, 0, 1, CV_AA ); 66 70 67 71 cvNamedWindow( "face classifier", 1 ); 72 73 Benchmark(); 68 74 } 69 75 … … 75 81 { 76 82 {{255,255,255}}, 83 {{0,0,0}}, 77 84 {{0,128,255}}, 78 85 {{0,255,255}}, … … 84 91 }; 85 92 86 void App:: Update()87 { 93 void App::Run() 94 { 88 95 frame = cvQueryFrame( m_Capture ); 89 96 if( !frame ) … … 93 100 } 94 101 95 102 if( !frame_copy ) 96 103 frame_copy = cvCreateImage( cvSize(frame->width,frame->height), 97 104 IPL_DEPTH_8U, frame->nChannels ); … … 101 108 cvFlip( frame, frame_copy, 0 ); 102 109 103 /////////////////////////104 105 110 Image camera(frame_copy); 106 111 Update(camera); 112 113 m_FrameNum++; 114 #ifdef SAVE_FRAMES 115 char name[256]; 116 sprintf(name,"out-%0.4d.jpg",m_FrameNum); 117 cerr<<"saving "<<name<<endl; 118 cvSaveImage(name,camera.m_Image); 119 #endif 120 121 cvShowImage("face classifier", camera.m_Image); 122 } 123 124 void App::Update(Image &camera) 125 { 107 126 cvClearMemStorage(m_Storage); 108 127 128 int flags=0; 129 if (m_Learn) flags|=CV_HAAR_FIND_BIGGEST_OBJECT; 130 109 131 CvSeq* faces = cvHaarDetectObjects( camera.m_Image, m_Cascade, m_Storage, 110 1.1, 2, 0132 1.1, 2, flags 111 133 //|CV_HAAR_FIND_BIGGEST_OBJECT 112 134 //|CV_HAAR_DO_ROUGH_SEARCH … … 166 188 { 167 189 char s[32]; 168 sprintf(s,"%d %0.2f",ID,confidence); 190 map<int,string>::iterator d = m_DebugNames.find(ID); 191 if (d!=m_DebugNames.end()) 192 { 193 sprintf(s,"%s %0.2f",d->second.c_str(),confidence); 194 } 195 else 196 { 197 sprintf(s,"%d %0.2f",ID,confidence); 198 } 199 169 200 cvPutText(camera.m_Image, s, cvPoint(r->x,r->y+r->height-5), &m_Font, colors[0]); 170 201 … … 200 231 201 232 m_SceneState.Update(); 202 203 m_FrameNum++; 204 #ifdef SAVE_FRAMES 205 char name[256]; 206 sprintf(name,"out-%0.4d.jpg",m_FrameNum); 207 cerr<<"saving "<<name<<endl; 208 cvSaveImage(name,camera.m_Image); 209 #endif 210 211 cvShowImage("face classifier", camera.m_Image); 212 213 } 214 233 234 } 235 236 void App::Benchmark() 237 { 238 cerr<<"Running benchmark test"<<endl; 239 vector<string> people=Glob("../data/benchmark/trek/training/*"); 240 int ID=0; 241 m_Learn=true; 242 243 for(vector<string>::iterator pi=people.begin(); pi!=people.end(); ++pi) 244 { 245 m_DebugNames[ID]=pi->substr(pi->find_last_of("/")+1,pi->length()); 246 vector<string> images=Glob(*pi+"/*.jpg"); 247 for(vector<string>::iterator ii=images.begin(); ii!=images.end(); ++ii) 248 { 249 cerr<<ID<<" "<<*ii<<endl; 250 m_FaceNum=ID; 251 Image image(*ii); 252 Update(image); 253 string fn=*ii+"-out.png"; 254 cvSaveImage(fn.c_str(),image.m_Image); 255 } 256 ID++; 257 } 258 259 m_Learn=false; 260 261 vector<string> images=Glob("../data/benchmark/trek/control/*.jpg"); 262 for(vector<string>::iterator ti=images.begin(); ti!=images.end(); ++ti) 263 { 264 cerr<<*ti<<endl; 265 Image test(*ti); 266 Update(test); 267 string fn=*ti+"-out.png"; 268 cvSaveImage(fn.c_str(),test.m_Image); 269 } 270 271 images=Glob("../data/benchmark/trek/test/*.jpg"); 272 for(vector<string>::iterator ti=images.begin(); ti!=images.end(); ++ti) 273 { 274 cerr<<*ti<<endl; 275 Image test(*ti); 276 Update(test); 277 string fn=*ti+"-out.png"; 278 cvSaveImage(fn.c_str(),test.m_Image); 279 } 280 } -
foam/trunk/faceclassifier/src/App.h
r95 r96 32 32 ~App(); 33 33 34 void Update(); 34 void Run(); 35 void Update(Image &camera); 35 36 36 37 private: 38 void Benchmark(); 37 39 38 40 CvCapture* m_Capture; … … 52 54 IplImage *frame_copy; 53 55 56 map<int,string> m_DebugNames; 57 54 58 int m_FrameNum; 55 59 }; -
foam/trunk/faceclassifier/src/FaceBank.cpp
r95 r96 51 51 { 52 52 // Subtract the mean as an attempt to deal with global lighting changes 53 //face.SubMean();53 face.SubMean(); 54 54 55 55 m_Classifier->AddFeature(ID,face.Scale(m_FaceWidth,m_FaceHeight).RGB2GRAY().ToFloatVector()); … … 62 62 { 63 63 // Subtract the mean as an attempt to deal with global lighting changes 64 //face.SubMean();64 face.SubMean(); 65 65 66 66 float error; 67 67 ID = m_Classifier->Classify(face.Scale(m_FaceWidth,m_FaceHeight).RGB2GRAY().ToFloatVector(),error); 68 68 69 if (error!=0) error=1/error; 70 else return 1; 71 69 72 // if the error is less than the threshold, return the id 70 //if (error <m_ErrorThresh)73 //if (error>m_ErrorThresh) 71 74 { 72 return 1-error;75 return error; 73 76 } 74 77 -
foam/trunk/faceclassifier/src/main.cpp
r94 r96 40 40 while (1) 41 41 { 42 app. Update();42 app.Run(); 43 43 } 44 44 } -
foam/trunk/vision/Makefile
r91 r96 6 6 src/PCAClassifier.cpp\ 7 7 src/LDAClassifier.cpp\ 8 src/Matrix.cpp\ 8 9 src/PCA.cpp\ 9 10 src/SVD.cpp\ 11 src/FileTools.cpp\ 10 12 src/tinyxml.cpp\ 11 13 src/tinyxmlerror.cpp\ -
foam/trunk/vision/src/Image.cpp
r95 r96 268 268 v.val[c]/=s; 269 269 } 270 271 CvScalar o; 270 272 271 273 // now subtract it from each pixel … … 273 275 { 274 276 for(int x=0; x<m_Image->width; x++) 275 { 277 { 276 278 for (int c=0; c<m_Image->nChannels; c++) 277 279 { 278 280 // force the average to be 127 279 v.val[c]=127+(cvGet2D(m_Image,y,x).val[c] - v.val[c]*256.0f);280 } 281 282 cvSet2D(m_Image,y,x, v);281 o.val[c]=127+(cvGet2D(m_Image,y,x).val[c] - v.val[c]*256.0f); 282 } 283 284 cvSet2D(m_Image,y,x,o); 283 285 } 284 286 } -
foam/trunk/vision/src/Matrix.h
r90 r96 100 100 bool IsInf(); 101 101 Matrix Transposed() const; 102 Matrix Inverted() const; 102 103 103 104 Matrix &operator=(const Matrix &other); … … 110 111 Matrix &operator-=(const Matrix &other); 111 112 Matrix &operator*=(const Matrix &other); 113 bool operator==(const Matrix &other) const; 112 114 113 115 void SortRows(Vector<T> &v); … … 239 241 } 240 242 243 void matrix_inverse(const float *Min, float *Mout, int actualsize); 244 245 template<class T> 246 Matrix<T> Matrix<T>::Inverted() const 247 { 248 assert(m_Rows==m_Cols); // only works for square matrices 249 Matrix<T> ret(m_Rows,m_Cols); 250 matrix_inverse(GetRawDataConst(),ret.GetRawData(),m_Rows); 251 return ret; 252 } 241 253 242 254 template<class T> … … 358 370 } 359 371 372 template<class T> 373 bool Matrix<T>::operator==(const Matrix &other) const 374 { 375 if (m_Rows != other.m_Rows || 376 m_Cols != other.m_Cols) 377 { 378 return false; 379 } 380 381 for (unsigned int i=0; i<m_Cols; i++) 382 { 383 for (unsigned int j=0; j<m_Rows; j++) 384 { 385 if (!feq((*this)[i][j],other[i][j])) return false; 386 } 387 } 388 389 return true; 390 } 360 391 361 392 //todo: use memcpy for these 4 functions … … 553 584 Matrix<T> g=a.CropRows(0,1); 554 585 assert(g.GetRows()==1 && g.GetCols()==3 && g[0][0]==1); 555 586 587 // test matrix invert 588 Matrix<T> h(3,3); 589 h.Zero(); 590 h[0][0]=1; 591 h[1][1]=1; 592 h[2][2]=1; 593 Matrix<T> i=h.Inverted(); 594 i==h; 595 596 // some transforms from fluxus 597 Matrix<T> j(4,4); 598 j[0][0]=1.0; 599 j[0][1]=0.0 ; 600 j[0][2]=0.0; 601 j[0][3]=0.0; 602 603 j[1][0]=0.0 ; 604 j[1][1]=0.7071067690849304 ; 605 j[1][2]=0.7071067690849304 ; 606 j[1][3]=0.0 ; 607 608 j[2][0]=0.0 ; 609 j[2][1]=-0.7071067690849304 ; 610 j[2][2]=0.7071067690849304 ; 611 j[2][3]=0.0 ; 612 613 j[3][0]=1.0 ; 614 j[3][1]=2.0 ; 615 j[3][2]=3.0 ; 616 j[3][3]=1.0 ; 617 618 Matrix<T> k(4,4); 619 k[0][0]=1.0 ; 620 k[0][1]=0.0 ; 621 k[0][2]=0.0 ; 622 k[0][3]=0.0 ; 623 624 k[1][0]=0.0 ; 625 k[1][1]=0.7071068286895752 ; 626 k[1][2]=-0.7071068286895752 ; 627 k[1][3]=0.0 ; 628 629 k[2][0]=0.0 ; 630 k[2][1]=0.7071068286895752 ; 631 k[2][2]=0.7071068286895752 ; 632 k[2][3]=0.0 ; 633 634 k[3][0]=-0.9999999403953552 ; 635 k[3][1]=-3.535533905029297 ; 636 k[3][2]=-0.7071067690849304 ; 637 k[3][3]=0.9999999403953552 ; 638 639 assert(j.Inverted()==k); 556 640 } 557 641 -
foam/trunk/vision/src/PCA.cpp
r94 r96 86 86 return m_Mean+m_EigenTransform.VecMulTransposed(v); 87 87 } 88 89 void PCA::Mult(const PCA &other) 90 { 91 m_EigenTransform *= other.GetEigenTransform().Inverted(); 92 } 88 93 89 94 void PCA::Save(FILE *f) -
foam/trunk/vision/src/PCA.h
r95 r96 40 40 // remove eigenvectors from the transform 41 41 void Compress(unsigned int s, unsigned int e); 42 43 void Mult(const PCA &other); 42 44 43 45 Vector<float> Project(Vector<float> v) const;
Note: See TracChangeset
for help on using the changeset viewer.