Open Cv fisherfaces

5

我遇到了这个问题,

当我在vs2010(debug)(opencv 2.4.0)中运行facerec_demo.cpp时,程序出现以下错误:

OpenCV错误:图像步长不正确(矩阵不连续,因此其行数无法更改),未知函数,文件......\src\opencv\modules\core>\src\matrix.cpp,第801行。

这个错误导致我查看了facerec.cpp中的以下代码行:
(Fisherfaces::train(InputArray src, InputArray _lbls)

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

我使用过PGM图像数据库,以下是我的原始*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;
}

现在我遇到了一个错误,在文件-->facerec.cpp的Fisherfaces :: predict方法中,第414行说: Mat q = subspaceProject(_eigenvectors,_mean,src.reshape(1,1));当我加载我的测试样本时,_eigenvectors和_mean消失了!!! - rliberal
3个回答

2
我看到你正在使用OpenCV 2.4.0。作为开发者,我承认混淆是我的错:那时我没有彻底检查传递给训练方法的输入数据,因此传递不正确对齐的数据的人会收到像你这样的错误消息。最有可能出现的错误是,由于您的训练图像大小不相等,这是特征脸和Fisherfaces算法所必需的(而不是本地二进制模式直方图)。OpenCV 2.4.0尝试将数据重塑为矩阵,并显示您看到的错误消息;相反,在训练之前,OpenCV 2.4.2检查输入数据是否正确对齐并抛出一个有意义的异常...带有非常清晰的信息。

本帖假设也可能是由于链接OpenCV库:

如果不是链接库,则可能是由于图像大小。可以使用cv::resize轻松地调整训练图像大小,这是OpenCV中的一个函数。

但你可能应该考虑切换到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,你只需要切换包含文件即可。

太好了!问题具体是什么?也许您可以更新您的帖子或在此处添加解决方案作为评论。这将有助于遇到相同问题的人们。 - bytefish

1
我遇到了相同的OpenCv错误,尝试了这里找到的所有帮助,但仍然会出现异常(异常发生在.Predict()语句上)。
问题在于图像的大小。图像的大小必须小于100px(<100px)(不确定是否完全小于100,也许100仍然可以工作)。
我将我的图片大小从150:150更改为80:80,现在它正常工作了!
希望我能帮助到某些人,因为这是一个恼人的错误。

0
我在另一篇帖子中回答了这个问题,但我想确保搜索此错误以获取帮助的人能够找到答案。
当您创建模型时。
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();

你需要传递两个参数

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

此页面提供有关如何使用createFisherFaceRecognizer的更多信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接