00001 // This is oul/ouml/eigenfaces.h 00002 #ifndef OTAGO_eigenfaces__h_INCLUDED 00003 #define OTAGO_eigenfaces__h_INCLUDED 00004 //: 00005 // \file 00006 // \brief A simple eigenfaces class 00007 // 00008 // Copyright (c) 2001 Brendan McCane 00009 // University of Otago, Dunedin, New Zealand 00010 // Reproduction rights limited as described in the COPYRIGHT file. 00011 //---------------------------------------------------------------------- 00012 00013 #include <vcl_cfloat.h> // for DBL_MAX 00014 #include <vcl_vector.h> 00015 #include <vnl/vnl_vector.h> 00016 #include <vil1/vil1_memory_image_of.h> 00017 #include "inlines.h" 00018 #include "image_database.h" 00019 #include "io.h" 00020 00021 typedef vil1_memory_image_of<double> Image; 00022 00023 class EigenFace 00024 { 00025 private: 00026 unsigned int image_size; 00027 vcl_vector<vnl_vector<double> *> training_images; 00028 vcl_vector<vnl_vector<double> *> encoded_training_images; 00029 vcl_vector<char *> training_labels; 00030 vcl_vector<vnl_vector<double> *> eigenvectors; 00031 vcl_vector<double> eigenvalues; 00032 vnl_vector<double> *average_training_image; 00033 vnl_vector<double> *convert_image_to_vector(Image *im); 00034 void encode_training_images(); 00035 void cleanup(); 00036 00037 public: 00038 struct LabelDist 00039 { 00040 char *label; 00041 double dist; 00042 LabelDist(char *str, double d):label(str), dist(d){} 00043 bool operator<(const LabelDist &x) const {return dist>x.dist;} 00044 }; 00045 00046 EigenFace(): image_size(0), average_training_image(NULL){} 00047 ~EigenFace(); 00048 bool add_training_image(Image *im, const char * label); 00049 bool calculate_eigenfaces(); 00050 vnl_vector<double> *get_eigenvector(int i); 00051 double get_eigenvalue(int i); 00052 inline char *get_label(unsigned int i){ 00053 if (i<training_labels.size()) return training_labels[i]; 00054 else return NULL;} 00055 bool check_eigenvectors(); 00056 void save_as_images(int width, int height); 00057 vnl_vector<double>* encode(Image *im); 00058 vnl_vector<double>* encode(vnl_vector<double> *im); 00059 vnl_vector<double>* decode(vnl_vector<double> *wts); 00060 inline int num_vectors() const {return training_images.size();} 00061 char *classify(Image *im, double threshold=DBL_MAX, int k=3, int dim=10); 00062 void check_training(); 00063 void output_xgobi(char *basefile); 00064 }; 00065 00066 // a helper class 00067 class EigenSort 00068 { 00069 public: 00070 int eigenindex; 00071 double weight; 00072 bool operator<(const EigenSort o1) const 00073 {return this->weight<o1.weight;} 00074 }; 00075 00076 00077 #endif // OTAGO_eigenfaces__h_INCLUDED