Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Eigenfaces face recognizer gives error: unresolved external symbols

Hej - I'm having probems with making the Eigenface-part of this turtorial:

When I try to run it I get these errors:

1>main.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::subspaceReconstruct(class cv::_InputArray const &,class cv::_InputArray const &,class cv::_InputArray const &)" (?subspaceReconstruct@cv@@YA?AVMat@1@ABV_InputArray@1@00@Z) referenced in function __catch$_main$0 1>main.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::subspaceProject(class cv::_InputArray const &,class cv::_InputArray const &,class cv::_InputArray const &)" (?subspaceProject@cv@@YA?AVMat@1@ABV_InputArray@1@00@Z) referenced in function __catch$_main$0 1>main.obj : error LNK2019: unresolved external symbol "void __cdecl cv::applyColorMap(class cv::_InputArray const &,class cv::_OutputArray const &,int)" (?applyColorMap@cv@@YAXABV_InputArray@1@ABV_OutputArray@1@H@Z) referenced in function __catch$_main$0 1>main.obj : error LNK2019: unresolved external symbol "class cv::Ptr<class cv::facerecognizer=""> __cdecl cv::createEigenFaceRecognizer(int,double)" (?createEigenFaceRecognizer@cv@@YA?AV?$Ptr@VFaceRecognizer@cv@@@1@HN@Z) referenced in function __catch$_main$0

Is there someone who can help me with this? I cannot figure out what I'm doing wrong...

The actual code look like this:

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;    
// Function, which creates a normalized image

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;

// Reads the csv-file in, which includes the images from the image-database:        

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in);
// If there is no file, then it comes out with an error message:
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;
// As long as there is lines in the file, the program takes in the next line in the csv-file:
while (getline(file, line)) {
    stringstream liness(line);
    getline(liness, path, separator);
    getline(liness, classlabel);
    // If there is no more lines:
    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 this and exit the program
    cout << "usage: " << argv[0] << " <csv.ext> <output_folder> " << endl;
string output_folder;
// if there are 3 arguments given:
if (argc == 3) {
    output_folder = string(argv[2]);

// Get the path to the CSV-file.
string fn_csv = string(argv[1]); //"...\attFaceDatabaseText.csv"; 

// 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:
// Tries to read in the data from the CSV-file (call to read_csv)
try {
    read_csv(fn_csv, images, labels);
    // If it is not possible to read in the csv, then this happens:
} catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
    // nothing more we can do and program closes.

// Quit if there are not enough images available.
if(images.size() <= 1) {
    string error_message = "This program 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[0].rows;

// The following lines simply get the last images from
// your dataset and remove it from the vector. This is
// done, so that the training data (which we learn the
// cv::FaceRecognizer on) and the test data we test
// the model with, do not overlap.
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
// Works like a stack or queue:

// The following lines create an Eigenfaces model for
// face recognition and train it with the images and
// labels read from the given CSV file.

// This here is a full PCA, if you just want to keep
// 10 principal components (read Eigenfaces), then call
// the factory method like this:
//      cv::createEigenFaceRecognizer(10);
// If you want to create a FaceRecognizer with a
// confidence threshold (e.g. 123.0), call it with:
//      cv::createEigenFaceRecognizer(10, 123.0);
// If you want to use _all_ Eigenfaces and have a threshold,
// then call the method like this:
//      cv::createEigenFaceRecognizer(0, 123.0);

Ptr<FaceRecognizer> model = createEigenFaceRecognizer(10, 123.0);
model->train(images, labels);

// The following line predicts the label of a given test image:
int predictedLabel = model->predict(testSample);

// To get the confidence of a prediction call the 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 is how to get the eigenvalues of this Eigenfaces model:
Mat eigenvalues = model->getMat("eigenvalues");

// And we can do the same to display the Eigenvectors (read Eigenfaces):
Mat W = model->getMat("eigenvectors");

// Get the sample mean from the 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 the Eigenfaces:
for (int i = 0; i < min(10, W.cols); i++) {
    string msg = format("Eigenvalue #%d = %.5f", i,<double>(i));
    cout << msg << endl;
    // get eigenvector #i
    Mat ev = W.col(i).clone();
    // Reshape to original size & normalize to [0...255] for imshow.
    Mat grayscale = norm_0_255(ev.reshape(1, height));
    // Show the image & apply a Jet colormap for 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 the image reconstruction at some predefined steps:
for(int num_components = min(W.cols, 10); num_components < min(W.cols, 300); num_components+=15) {
    // slice the eigenvectors from the 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 the 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 we are not writing to an output folder:
if(argc == 2) {
return 0;


And I do have an image database (AT&T) and a CSV-file with the path's to the images (placed inside the project-folder).