Changeset 89 for foam/trunk
- Timestamp:
- 06/03/2009 10:01:22 AM (12 years ago)
- Location:
- foam/trunk
- Files:
-
- 8 edited
- 6 copied
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
foam/trunk/vision/Makefile
r87 r89 5 5 src/Rendering.cpp\ 6 6 src/Classifier.cpp\ 7 src/PCA SubspaceClassifier.cpp\7 src/PCAClassifier.cpp\ 8 8 src/LDAClassifier.cpp\ 9 9 src/PCA.cpp\ -
foam/trunk/vision/src/Image.cpp
r86 r89 43 43 } 44 44 45 Image::Image(int w, int h, int c, const Vector<float> &v )45 Image::Image(int w, int h, int c, const Vector<float> &v, float gain) 46 46 { 47 47 m_Image=cvCreateImage(cvSize(w, h), 8, c); … … 54 54 for (int c=0; c<m_Image->nChannels; c++) 55 55 { 56 s.val[c]= v[pos++]*256.0f;56 s.val[c]=(0.5+v[pos++]*gain)*127.0f; 57 57 } 58 58 cvSet2D(m_Image,y,x,s); … … 203 203 y+py>0 && y+py<m_Image->height) 204 204 { 205 cvSet2D(m_Image,y+py,x+px,cvGet2D(image.m_Image,y,x)); 205 if (image.m_Image->nChannels==1) 206 { 207 CvScalar v; 208 v.val[0]=cvGet2D(image.m_Image,y,x).val[0]; 209 v.val[1]=cvGet2D(image.m_Image,y,x).val[0]; 210 v.val[2]=cvGet2D(image.m_Image,y,x).val[0]; 211 cvSet2D(m_Image,y+py,x+px,v); 212 } 213 else 214 { 215 cvSet2D(m_Image,y+py,x+px,cvGet2D(image.m_Image,y,x)); 216 } 206 217 } 207 218 } -
foam/trunk/vision/src/Image.h
r86 r89 29 29 Image(const Image &other); 30 30 Image(const IplImage *other); // copies the given image 31 Image(int w, int h, int c, const Vector<float> &v );31 Image(int w, int h, int c, const Vector<float> &v, float gain=1.0f); 32 32 ~Image(); 33 33 -
foam/trunk/vision/src/Matrix.h
r86 r89 92 92 Vector<T> GetRowVector(unsigned int r) const; 93 93 Vector<T> GetColVector(unsigned int c) const; 94 void SetRowVector(unsigned int r, const Vector<T> &row); 95 void SetColVector(unsigned int c, const Vector<T> &col); 94 96 95 97 void Print() const; … … 107 109 Matrix &operator*=(const Matrix &other); 108 110 111 void SortRows(Vector<T> &v); 112 void SortCols(Vector<T> &v); 113 109 114 static void RunTests(); 110 115 … … 303 308 } 304 309 310 311 //todo: use memcpy for these 4 functions 305 312 template<class T> 306 313 Vector<T> Matrix<T>::GetRowVector(unsigned int r) const … … 326 333 return ret; 327 334 } 335 336 template<class T> 337 void Matrix<T>::SetRowVector(unsigned int r, const Vector<T> &row) 338 { 339 assert(r<m_Rows); 340 assert(row.Size()==m_Cols); 341 for (unsigned int j=0; j<m_Cols; j++) 342 { 343 (*this)[r][j]=row[j]; 344 } 345 } 346 347 template<class T> 348 void Matrix<T>::SetColVector(unsigned int c, const Vector<T> &col) 349 { 350 assert(c<m_Cols); 351 assert(col.Size()==m_Rows); 352 for (unsigned int i=0; i<m_Rows; i++) 353 { 354 (*this)[i][c]=col[i]; 355 } 356 } 357 358 // sort rows by v 359 template<class T> 360 void Matrix<T>::SortRows(Vector<T> &v) 361 { 362 assert(v.Size()==m_Rows); 363 364 bool sorted=false; 365 while(!sorted) 366 { 367 sorted=true; 368 369 for (unsigned int i=0; i<v.Size()-1; i++) 370 { 371 if (v[i]<v[i+1]) 372 { 373 sorted=false; 374 float vtmp = v[i]; 375 v[i]=v[i+1]; 376 v[i+1]=vtmp; 377 378 Vector<float> rtmp = GetRowVector(i); 379 SetRowVector(i,GetRowVector(i+1)); 380 SetRowVector(i+1,rtmp); 381 } 382 } 383 } 384 } 385 386 // sort cols by v 387 template<class T> 388 void Matrix<T>::SortCols(Vector<T> &v) 389 { 390 assert(v.Size()==m_Cols); 391 392 bool sorted=false; 393 while(!sorted) 394 { 395 sorted=true; 396 397 for (unsigned int i=0; i<v.Size()-1; i++) 398 { 399 if (v[i]<v[i+1]) 400 { 401 sorted=false; 402 float vtmp = v[i]; 403 v[i]=v[i+1]; 404 v[i+1]=vtmp; 405 406 Vector<float> rtmp = GetColVector(i); 407 SetColVector(i,GetColVector(i+1)); 408 SetColVector(i+1,rtmp); 409 } 410 } 411 } 412 } 413 328 414 329 415 template<class T> -
foam/trunk/vision/src/PCA.cpp
r86 r89 39 39 for (FeatureVec::iterator vi = m_Features.begin(); vi!=m_Features.end(); ++vi) 40 40 { 41 cerr<<vi->IsInf()<<endl;42 41 Mean+=*vi; 43 42 } … … 53 52 54 53 // 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;54 cerr<<"Allocating "<<m_FeatureSize*m_FeatureSize*sizeof(float)/1024/1024.0<<" megs for covariance matrix"<<endl; 56 55 m_EigenTransform = Matrix<float>(m_FeatureSize,m_FeatureSize); 57 56 m_EigenTransform.Zero(); -
foam/trunk/vision/src/PCA.h
r86 r89 32 32 PCA(unsigned int FeatureSize); 33 33 ~PCA(); 34 35 typedef std::vector<Vector<float> > FeatureVec; 34 36 35 37 void AddFeature(Vector<float> v) { m_Features.push_back(v); } … … 40 42 const Vector<float> &GetEigenValues() { return m_EigenValues; } 41 43 const Matrix<float> &GetEigenTransform() { return m_EigenTransform; } 42 44 const FeatureVec &GetFeatures() { return m_Features; } 45 43 46 private: 44 47 45 48 void CalcMean(); 46 49 47 typedef std::vector<Vector<float> > FeatureVec;48 50 49 51 FeatureVec m_Features; -
foam/trunk/vision/src/PCAClassifier.cpp
r86 r89 15 15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 16 17 #include "PCA SubspaceClassifier.h"17 #include "PCAClassifier.h" 18 18 19 PCA SubspaceClassifier::PCASubspaceClassifier(unsigned int FeatureSize) :19 PCAClassifier::PCAClassifier(unsigned int FeatureSize) : 20 20 Classifier(FeatureSize) 21 21 { 22 22 } 23 23 24 PCA SubspaceClassifier::~PCASubspaceClassifier()24 PCAClassifier::~PCAClassifier() 25 25 { 26 26 } -
foam/trunk/vision/src/PCAClassifier.h
r86 r89 25 25 #define FOAM_PCA_CLASSIFIER 26 26 27 class PCA SubspaceClassifier : public Classifier27 class PCAClassifier : public Classifier 28 28 { 29 29 public: 30 PCA SubspaceClassifier(unsigned int FeatureSize);31 ~PCA SubspaceClassifier();30 PCAClassifier(unsigned int FeatureSize); 31 ~PCAClassifier(); 32 32 33 33 private: -
foam/trunk/vision/src/SVD.cpp
r86 r89 57 57 #include <stdlib.h> 58 58 #include <math.h> 59 #include <algorithm> 60 #include <list> 59 61 #include "SVD.h" 62 63 using namespace std; 60 64 61 65 #define SIGN(a, b) ((b) >= 0.0 ? fabs(a) : -fabs(a)) 62 66 #define MAX(x,y) ((x)>(y)?(x):(y)) 63 67 64 static double PYTHAG(double a, double b) 65 { 66 double at = fabs(a), bt = fabs(b), ct, result; 67 68 if (at > bt) { ct = bt / at; result = at * sqrt(1.0 + ct * ct); } 69 else if (bt > 0.0) { ct = at / bt; result = bt * sqrt(1.0 + ct * ct); } 70 else result = 0.0; 71 return(result); 72 } 68 /////////////////////////////////////////////////////// 73 69 74 70 Vector<float> SVD(Matrix<float> &m) … … 77 73 Matrix<float> v(m.GetRows(),m.GetCols()); 78 74 dsvd(m, m.GetRows(), m.GetCols(), w.GetRawData(), v); 75 m.SortCols(w); 79 76 return w; 77 } 78 79 /////////////////////////////////////////////////////// 80 81 static double PYTHAG(double a, double b) 82 { 83 double at = fabs(a), bt = fabs(b), ct, result; 84 85 if (at > bt) { ct = bt / at; result = at * sqrt(1.0 + ct * ct); } 86 else if (bt > 0.0) { ct = at / bt; result = bt * sqrt(1.0 + ct * ct); } 87 else result = 0.0; 88 return(result); 80 89 } 81 90 -
foam/trunk/vision/src/main.cpp
r86 r89 23 23 #include "Vector.h" 24 24 #include "PCA.h" 25 #include <glob.h> 25 26 26 27 using namespace std; … … 32 33 double scale = 1; 33 34 34 PCA pca(Image("data/a-id0001-image0000.png").RGB2GRAY().NumElements()); 35 int tw=Image("data/a-id0001-image0000.png").RGB2GRAY().m_Image->width; 36 int th=Image("data/a-id0001-image0000.png").RGB2GRAY().m_Image->height; 37 int tc=Image("data/a-id0001-image0000.png").RGB2GRAY().m_Image->nChannels; 35 Image ti("data/spot-1.png"); 36 PCA pca(ti.RGB2GRAY().Scale(30,40).NumElements()); 38 37 39 38 void TestPCA() 40 39 { 41 pca.AddFeature(Image("data/a-id0001-image0000.png").RGB2GRAY().ToFloatVector()); 42 pca.AddFeature(Image("data/a-id0001-image0001.png").RGB2GRAY().ToFloatVector()); 43 pca.AddFeature(Image("data/a-id0002-image0000.png").RGB2GRAY().ToFloatVector()); 44 pca.AddFeature(Image("data/a-id0002-image0001.png").RGB2GRAY().ToFloatVector()); 45 cerr<<"pre calc"<<endl; 40 ti=ti.RGB2GRAY().Scale(30,40); 41 glob_t g; 42 glob("data/pca/*.png",GLOB_PERIOD,NULL,&g); 43 for (unsigned int n=0; n<g.gl_pathc; n++) 44 { 45 string path=g.gl_pathv[n]; 46 cerr<<path<<endl; 47 Image im(path); 48 //im.SubMean(); 49 pca.AddFeature(im.RGB2GRAY().Scale(30,40).ToFloatVector()); 50 } 51 globfree (&g); 46 52 pca.Calculate(); 47 cerr<<"post calc"<<endl;48 53 } 49 54 … … 259 264 // PCA display 260 265 261 for (int i=0; i<10; i++) 262 { 263 camera.Blit(Image(tw,th,tc,pca.GetEigenTransform().GetRowVector(i)),i*50,100); 264 } 266 camera.Clear(); 267 268 for (unsigned int i=0; i<pca.GetFeatures().size(); i++) 269 { 270 camera.Blit(Image(30,40,1,pca.GetFeatures()[i]),(i%20)*32,(i/20)*42); 271 } 272 273 for (unsigned int i=0; i<pca.GetEigenTransform().GetCols(); i++) 274 { 275 camera.Blit(Image(30,40,1,pca.GetEigenTransform().GetColVector(i),5),(i%20)*32,200+(i/20)*42); 276 } 277 278 pca.GetEigenValues().Print(); 265 279 266 280 cvShowImage("result", camera.m_Image);
Note: See TracChangeset
for help on using the changeset viewer.