Advertisement
bkit4s0

[generate traindatabase.xml]

May 20th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 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.     {
  18.     case 1 :
  19.         cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  20.         break;
  21.     case 3:
  22.         cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
  23.         break;
  24.     default:
  25.         src.copyTo(dst);
  26.         break;
  27.     }
  28.     return dst;
  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.             }
  46.             else {
  47.                 cout << path << endl;
  48.                 images.push_back(t);
  49.                 labels.push_back(atoi(classlabel.c_str()));
  50.             }
  51.         }
  52.     }
  53.     //namedWindow( "Display window",WINDOW_AUTOSIZE);
  54.     //imshow("Display window", images[10]);
  55.     //waitKey(0);
  56. }
  57. int main(int argc, const char *argv[]) {
  58.     if (argc < 2 ) {
  59.         cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;
  60.         exit(1);
  61.     }
  62.     string output_folder = ".";
  63.     if (argc == 3) {
  64.         output_folder = string(argv[2]);
  65.     }
  66.     string fn_csv = string(argv[1]);
  67.     vector<Mat> images;
  68.     vector<int> labels;
  69.     try {
  70.         read_csv(fn_csv, images, labels);
  71.     } catch (cv::Exception& e) {
  72.         cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
  73.         // nothing more we can do
  74.         exit(1);
  75.     }
  76.     /*
  77.     * stop here !!!
  78.     */
  79.     if(images.size() <= 1) {
  80.         string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
  81.         CV_Error(CV_StsError, error_message);
  82.     }
  83.     int height = images[0].rows;
  84.     Mat testSample = images[images.size() -1];
  85.     int testLabel = labels[labels.size() -1];
  86.     images.pop_back();
  87.     labels.pop_back();
  88.  
  89.     //namedWindow( "Display window",WINDOW_AUTOSIZE);
  90.     //imshow("Display window", testSample);
  91.     //waitKey(0);
  92.  
  93.     int num_components = 10;
  94.     double threshold = 10.0;
  95.  
  96.     Ptr<FaceRecognizer> model = createEigenFaceRecognizer(num_components,threshold);
  97.     model->train(images, labels);
  98.     model->save("traindatabase.xml");
  99.  
  100.     //int predictedLa
  101.     Mat eigenvalues = model->getMat("eigenvalues");
  102.  
  103.     //cout << "eigenvalues = " << endl;
  104.     //cout << eigenvalues << endl;
  105.  
  106.     Mat W = model->getMat("eigenvectors");
  107.     Mat mean = model->getMat("mean");
  108.  
  109.     //namedWindow( "Display window",WINDOW_AUTOSIZE);
  110.     //imshow("Display window", norm_0_255(mean.reshape(1,images[0].rows)));
  111.     //waitKey(0);
  112.     for (int i = 0; i < min(10,W.cols); i++) {
  113.         Mat ev = W.col(i).clone();
  114.         Mat grayscale = norm_0_255(ev.reshape(1,height));
  115.         //Mat cgrayscale;
  116.         //applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
  117.         if (argc == 2) {
  118.             imshow(format("eigenface_%d", i), grayscale);
  119.         } else {
  120.             imwrite(format("%s/eigenface_%d.png", output_folder.c_str(), i), norm_0_255(grayscale));
  121.         }
  122.     }
  123.     Mat evs = Mat(W, Range::all(), Range(0,num_components));
  124.  
  125.     /*
  126.     * Stop here !!!
  127.     */
  128.     for (int num_components = min(W.cols,10); num_components < min (W.cols,300); num_components+=15) {
  129.         Mat evs = Mat(W, Range::all(), Range(0,num_components));
  130.         Mat projection = subspaceProject(evs, mean, images[0].reshape(1,1));
  131.         cout << "projection" << endl;
  132.         cout << projection << endl;
  133.         Mat reconstrction = subspaceReconstruct(evs, mean, projection);
  134.  
  135.         // Normalize the result
  136.         reconstrction = norm_0_255(reconstrction.reshape(1,images[0].rows));
  137.  
  138.     }
  139.     if(argc == 2) {
  140.         waitKey(0);
  141.     }
  142.     getchar();
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement