I am using OpenCV v2.4.1
And this is the code that I use
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
if(src.channels() != 1)
CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
Mat dst;
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
return dst;
}
void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
throw std::exception();
string line, path, classlabel;
while (getline(file, line)) {
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
int main(int argc, const char *argv[]) {
string fn_csv = string("at.txt");
vector<Mat> images;
vector<int> labels;
try {
read_csv(fn_csv, images, labels);
} catch (exception&) {
cerr << "Error opening file \"" << fn_csv << "\"." << endl;
exit(1);
}
int height = images[0].rows;
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
images.pop_back();
labels.pop_back();
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
int predicted = model->predict(testSample);
cout << "predicted class = " << predicted << endl;
cout << "actual class = " << testLabel << endl;
Mat W = model->eigenvectors();
for (int i = 0; i < min(10, W.cols); i++) {
Mat ev = W.col(i).clone();
Mat grayscale = toGrayscale(ev.reshape(1, height));
Mat cgrayscale;
applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
imshow(format("%d", i), cgrayscale);
}
waitKey(0);
return 0;
}
contents of at.txt input file:
./in/S10/2.pgm; 9 . / at / s 10 / 7.pgm; 9 ./at/s10/6.pgm; 9. / at / s 10 / 9.pgm; 9 ./at/s10/5.pgm;9
Now the problem is that while I use these pgm files, it works fine. But when I specify any jpg file instead of pgm, I get the following error
OpenCV error: image step is incorrect (the matrix is not continuous, therefore its number of rows cannot be changed) in an unknown function, file ...... \ src \ opencv \ modul es \ core \ src \ matrix.cpp, line 801
While I can track this error, comes from Fisherfaces.train () -> cv :: Mat.reShape ()
But I do not know what could be the reason. Please, help.