What to do about obselete getMat call in facial recognition tutorial?

asked 2016-04-25 11:46:44 -0500

I have an issue compiling the tutorial code for Eigenfaces ( http://docs.opencv.org/2.4/modules/co... ) using the latest master of OpenCV 3.1

I've slightly modifed this code due to contrib.hpp not working, facial recognizer class not being properly recognized, etc. However I still have multiple errors that cannot be resolved. Specifically with the following lines:

    Mat eigenvalues = model->getMat("eigenvalues");
    Mat W = model->getMat("eigenvectors");
    Mat mean = model->getMat("mean");

I've searched far and wide and found multiple possible solutions for correcting this, including something about using getEigenvectors (although that response was vague and unhelpful) but none seem to work. This post is my final hope in trying to figure out what to replace this code with. It seems that the getMat function simply does not work in OpenCV 3.1 and it's not in the API at all. What should I replace this code with to get some working code here?

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/face.hpp>
//#include <opencv2/contrib.hpp>
//#include "opencv2/contrib/contrib.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/objdetect/objdetect.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 and return normalized image:
    Mat dst;
    switch(src.channels()) {
    case 1:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
    case 3:
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
    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 was given, please check the 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));

int main(int argc, const char *argv[]) {
    // Check for valid command line arguments, print usage
    // if no arguments were given.
    if (argc < 2) {
        cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;
    string output_folder;
    if (argc == 3) {
        output_folder = string(argv[2]);
    // Get the path to your CSV.
    string fn_csv = string(argv[1]);
    // These vectors hold the images and corresponding labels.
    vector<Mat> images;
    vector<int> labels;
    // Read in the data. This can fail if no valid
    // input filename is given.
    try {
        read_csv(fn_csv, images, labels);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
    // Quit if there are not enough images for this demo.
    if(images.size() <= 1) {
        string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
        CV_Error(CV_StsError, error_message);
    // Get the height from the first image. We'll need this
    // later in code to reshape the images to their original
    // size:
    int height = images ...
edit retag flag offensive close merge delete


for opencv3.1, please use 3.1 docs and updated samples

berak gravatar imageberak ( 2016-04-25 11:56:41 -0500 )edit