Open Cv Graphics

I have this problem,

when i start with vs2010 (debug) (open cv 2.4.0) facerec_demo.cpp gives me this error

OpenCV error: incorrect image step (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

This error brings me to this line in facerec.cpp
(Fisherfaces :: train (InputArray src, InputArray _lbls)

Mat data = asRowMatrix(src, CV_64FC1); <-- this gets a exeption, not handled.

and I use the pgm img database, and this is my original file * facerec_demo.cpp *

#include "stdafx.h" 
#include <opencv2/opencv.hpp>


#include <iostream>
#include <fstream>

#include <vector>
#include <string>
#include <sstream>

using namespace cv;
using namespace std;


vector<string> split_at_commas(const string& row)
{
  vector<string> res;
  istringstream buf(row);
  string s;
  while (getline(buf, s, ';'))
    res.push_back(s);
  return res;
}

Mat toGrayscale(InputArray _src) {
    Mat src = _src.getMat();
    // only allow one channel
    if(src.channels() != 1)
        CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
    // create and return normalized image
    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);
    std::ifstream file(_T("D:\\Users\\PC ACER\\Documents\\mycsv4.csv"));
    if (!file)
        throw std::exception();
    string line="", path="", classlabel="";
    while (getline(file, line)) {
        //vector<string> values = split_at_commas(line);
        stringstream liness(line);
        getline(liness, path, ';');
        getline(liness, classlabel);
        images.push_back(imread(path, 0));
        labels.push_back(atoi(classlabel.c_str()));
    }
}

int main(int argc, const char *argv[]) {
    // check for command line arguments
    if (argc != 2) {
        cout << "usage: " << argv[0] << " <csv.ext>" << endl;
        exit(1);
    }
    // path to your CSV
    string fn_csv = string(argv[1]);
    // images and corresponding labels
    vector<Mat> images;
    vector<int> labels;
    // read in the data
    try {
        read_csv(fn_csv, images, labels);
    } catch (exception&) {
        cerr << "Error opening file \"" << fn_csv << "\"." << endl;
        exit(1);
    }
    // get width and height
    //int width = images[0].cols;
    int height = images[0].rows;
    // get test instances
    Mat testSample = images[images.size() - 1];
    int testLabel = labels[labels.size() - 1];
    // ... and delete last element
    images.pop_back();
    labels.pop_back();
    // build the Fisherfaces model
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
    model->train(images, labels);
    // test model
    int predicted = model->predict(testSample);
    cout << "predicted class = " << predicted << endl;
    cout << "actual class = " << testLabel << endl;
    // get the eigenvectors
    Mat W = model->eigenvectors();
    // show first 10 fisherfaces
    for (int i = 0; i < min(10, W.cols); i++) {
        // get eigenvector #i
        Mat ev = W.col(i).clone();
    // reshape to original size AND normalize between [0...255]
    Mat grayscale = toGrayscale(ev.reshape(1, height));
        // show image (with Jet colormap)
    Mat cgrayscale;
        applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
        imshow(format("%d", i), cgrayscale);
    }
    waitKey(0);
    return 0;
}
+5
source share
3 answers

, OpenCV 2.4.0. , , - : , , , , , . , , , , . Eigenfaces Fisherfaces ( ). OpenCV 2.4.0 , ; OpenCV 2.4.2 ( ), ... .

, OpenCV:

, . OpenCV cv:: resize:

, , OpenCV 2.4.2, :

:

, OpenCV 2.4.2, OpenCV 2.4.0, libfacerec:

, OpenCV. , OpenCV 2.4.0, , OpenCV 2.4.2. , OpenCV 2.4.2, .

+2

OpenCv, , , - ( .predict()).

. 100px (< 100px) ( , 100, , 100 ).

150: 150 80:80 !

, -, .

+1

I answered this question in another post, but I want to make sure that people who are looking for help in this error will definitely find the answer.

when you create a model

Ptr<FaceRecognizer> model = createFisherFaceRecognizer();

You need to pass two parameters

createFisherFaceRecognizer(int num_components=0, double threshold=DBL_MAX);

This page contains additional information on how createFisherFaceRecognizer works.

0
source

All Articles