Advertisement
bkit4s0

[fisherfaces]

Jun 16th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/contrib/contrib.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8.  
  9. using namespace cv;
  10. using namespace std;
  11.  
  12. static Mat norm_0_255(InputArray _src) {
  13.     Mat src = _src.getMat();
  14.     // Create and return normalized image:
  15.     Mat dst;
  16.     switch(src.channels()) {
  17.     case 1:
  18.         cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  19.         break;
  20.     case 3:
  21.         cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
  22.         break;
  23.     default:
  24.         src.copyTo(dst);
  25.         break;
  26.     }
  27.     return dst;
  28. }
  29.  
  30. static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
  31.     std::ifstream file(filename.c_str(), ifstream::in);
  32.     if (!file) {
  33.         string error_message = "No valid input file was given, please check the given filename.";
  34.         CV_Error(CV_StsBadArg, error_message);
  35.     }
  36.     string line, path, classlabel;
  37.     while (getline(file, line)) {
  38.         stringstream liness(line);
  39.         getline(liness, path, separator);
  40.         getline(liness, classlabel);
  41.         if(!path.empty() && !classlabel.empty()) {
  42.             Mat t = imread(path,0);
  43.             if (t.rows < 1) {
  44.                 continue;
  45.             } else {
  46.                 cout << path << endl;
  47.                 images.push_back(t);
  48.                 labels.push_back(atoi(classlabel.c_str()));
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. int main(int argc, const char *argv[]) {
  55.     if (argc < 2) {
  56.         cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;
  57.         exit(1);
  58.     }
  59.     string output_folder = ".";
  60.     if (argc == 3) {
  61.         output_folder = string(argv[2]);
  62.     }
  63.     string fn_csv = string(argv[1]);
  64.  
  65.     vector<Mat> images;
  66.     vector<int> labels;
  67.  
  68.     try {
  69.         read_csv(fn_csv, images, labels);
  70.     } catch (cv::Exception& e) {
  71.         cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
  72.         exit(1);
  73.     }
  74.  
  75.     if(images.size() <= 1) {
  76.         string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
  77.         CV_Error(CV_StsError, error_message);
  78.     }
  79.  
  80.     int height = images[0].rows;
  81.  
  82.     //Mat testSample = images[images.size() - 1];
  83.     //int testLabel = labels[labels.size() - 1];
  84.     //images.pop_back();
  85.     //labels.pop_back();
  86.  
  87.     Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
  88.     model->train(images, labels);
  89.  
  90.     Mat eigenvalues = model->getMat("eigenvalues");
  91.  
  92.     Mat W = model->getMat("eigenvectors");
  93.  
  94.     Mat mean = model->getMat("mean");
  95.  
  96.     if(argc == 2) {
  97.         imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));
  98.     } else {
  99.         imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));
  100.     }
  101.  
  102.     for (int i = 0; i < min(10, W.cols); i++) {
  103.         Mat ev = W.col(i).clone();
  104.         Mat grayscale = norm_0_255(ev.reshape(1, height));
  105.         Mat cgrayscale;
  106.         applyColorMap(grayscale, cgrayscale, COLORMAP_BONE);
  107.         if(argc == 2) {
  108.             imshow(format("fisherface_%d", i), cgrayscale);
  109.         } else {
  110.             imwrite(format("%s/fisherface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));
  111.         }
  112.     }
  113.     if(argc == 2) {
  114.         waitKey(0);
  115.     }
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement