c++ - Error while executing Eigenfaces algorithm in OpenCV -


i have problem code eigenfaces i've found on opencv tutorial page.

the code this, same can find on related page (http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html#eigenfaces-in-opencv):

/*  * copyright (c) 2011. philipp wagner <bytefish[at]gmx[dot]de>.  * released public domain under terms of bsd simplified license.  *  * redistribution , use in source , binary forms, or without  * modification, permitted provided following conditions met:  *   * redistributions of source code must retain above copyright  *     notice, list of conditions , following disclaimer.  *   * redistributions in binary form must reproduce above copyright  *     notice, list of conditions , following disclaimer in  *     documentation and/or other materials provided distribution.  *   * neither name of organization nor names of contributors  *     may used endorse or promote products derived software  *     without specific prior written permission.  *  *   see <http://www.opensource.org/licenses/bsd-license>  */  #include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp"  #include <iostream> #include <fstream> #include <sstream>  using namespace cv; using namespace std;  static mat norm_0_255(inputarray _src) {     mat src = _src.getmat();     // create , return normalized image:     mat dst;     switch(src.channels()) {     case 1:         cv::normalize(_src, dst, 0, 255, norm_minmax, cv_8uc1);         break;     case 3:         cv::normalize(_src, dst, 0, 255, norm_minmax, cv_8uc3);         break;     default:         src.copyto(dst);         break;     }     return dst; }  static void read_csv(const string& filename, vector<mat>& images, vector<int>& labels, char separator = ';') {     std::ifstream file(filename.c_str(), ifstream::in);     if (!file) {         string error_message = "no valid input file given, please check given filename.";         cv_error(cv_stsbadarg, error_message);     }     string line, path, classlabel;     while (getline(file, line)) {         stringstream liness(line);         getline(liness, path, separator);         getline(liness, classlabel);         if(!path.empty() && !classlabel.empty()) {             images.push_back(imread(path, 0));             labels.push_back(atoi(classlabel.c_str()));         }     } }  int main(int argc, const char *argv[]) {     // check valid command line arguments, print usage     // if no arguments given.     if (argc < 2) {         cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;         exit(1);     }     string output_folder = ".";     if (argc == 3) {         output_folder = string(argv[2]);     }     // path csv.     string fn_csv = string(argv[1]);     // these vectors hold images , corresponding labels.     vector<mat> images;     vector<int> labels;     // read in data. can fail if no valid     // input filename given.     try {         read_csv(fn_csv, images, labels);     } catch (cv::exception& e) {         cerr << "error opening file \"" << fn_csv << "\". reason: " << e.msg << endl;         // nothing more can         exit(1);     }     // quit if there not enough images demo.     if(images.size() <= 1) {         string error_message = "this demo needs @ least 2 images work. please add more images data set!";         cv_error(cv_stserror, error_message);     }     // height first image. we'll need     // later in code reshape images original     // size:     int height = images[0].rows;     // following lines last images     // dataset , remove vector.     // done, training data (which learn     // cv::facerecognizer on) , test data test     // model with, not overlap.     mat testsample = images[images.size() - 1];     int testlabel = labels[labels.size() - 1];     images.pop_back();     labels.pop_back();     // following lines create eigenfaces model     // face recognition , train images ,     // labels read given csv file.     // here full pca, if want keep     // 10 principal components (read eigenfaces), call     // factory method this:     //     //      cv::createeigenfacerecognizer(10);     //     // if want create facerecognizer     // confidence threshold (e.g. 123.0), call with:     //     //      cv::createeigenfacerecognizer(10, 123.0);     //     // if want use _all_ eigenfaces , have threshold,     // call method this:     //     //      cv::createeigenfacerecognizer(0, 123.0);     //     ptr<facerecognizer> model = createeigenfacerecognizer();     model->train(images, labels); //<--error!!     // following line predicts label of given     // test image:     int predictedlabel = model->predict(testsample);     //     // confidence of prediction call model with:     //     //      int predictedlabel = -1;     //      double confidence = 0.0;     //      model->predict(testsample, predictedlabel, confidence);     //     string result_message = format("predicted class = %d / actual class = %d.", predictedlabel, testlabel);     cout << result_message << endl;     // here how eigenvalues of eigenfaces model:     mat eigenvalues = model->getmat("eigenvalues");     // , can same display eigenvectors (read eigenfaces):     mat w = model->getmat("eigenvectors");     // sample mean training data     mat mean = model->getmat("mean");     // display or save:     if(argc == 2) {         imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));     } else {         imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));     }     // display or save eigenfaces:     (int = 0; < min(10, w.cols); i++) {         string msg = format("eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));         cout << msg << endl;         // eigenvector #i         mat ev = w.col(i).clone();         // reshape original size & normalize [0...255] imshow.         mat grayscale = norm_0_255(ev.reshape(1, height));         // show image & apply jet colormap better sensing.         mat cgrayscale;         applycolormap(grayscale, cgrayscale, colormap_jet);         // display or save:         if(argc == 2) {             imshow(format("eigenface_%d", i), cgrayscale);         } else {             imwrite(format("%s/eigenface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));         }     }      // display or save image reconstruction @ predefined steps:     for(int num_components = min(w.cols, 10); num_components < min(w.cols, 300); num_components+=15) {         // slice eigenvectors model         mat evs = mat(w, range::all(), range(0, num_components));         mat projection = subspaceproject(evs, mean, images[0].reshape(1,1));         mat reconstruction = subspacereconstruct(evs, mean, projection);         // normalize result:         reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));         // display or save:         if(argc == 2) {             imshow(format("eigenface_reconstruction_%d", num_components), reconstruction);         } else {             imwrite(format("%s/eigenface_reconstruction_%d.png", output_folder.c_str(), num_components), reconstruction);         }     }     // display if not writing output folder:     if(argc == 2) {         waitkey(0);     }     return 0; } 

when try execute code error:

    opencv error: image step wrong (the matrix not continuous, number of rows can not changed) in reshape, file /tmp/opencv-ppduga/opencv-2.4.9/modules/core/src /matrix.cpp, line 802 libc++abi.dylib: terminating uncaught exception of type cv::exception: /tmp/opencv-ppduga/opencv-2.4.9/modules/core/src/matrix.cpp:802: error: (-13) matrix not continuous, number of rows can not changed in function reshape  abort trap: 6 

the error appears during train phase (as i've reported on code).

i'm using xcode on mac last os yosemite, dataset at&t facedatabase can downloaded here: http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html

thanks in advance

for eigen , fisherfaces, images have 'flattened' 1 single row, possible, if mat continuous. (lbph not constraind way)

but i'd rather suspect, resp. mat empty, because not image @ all.

please check data again carefully. contain non-images (like .txt file) ?

you generated csv file via supplied python script. again, check outcome.

when reading csv, try replace

images.push_back(imread(path, 0)); 

with:

mat im = imread(path, 0); if ( im.empty() ) {      cerr << path << " empty !" << endl;      exit(-1); } if ( !im.iscontinuous() ) {      // .bmp files 'padded' multiples of 4      // (some image editors that)      // if end here, try to:      //   * convert images png, pbm or such      //   * use im.clone(); instead (the deep copy force continuity)      //      cerr << path << " not continuous !" << endl;      exit(-2); } images.push_back(im); 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -