2012-07-23 73 views
5

我有這個問題,打開了Cv費舍爾

當我用VS2010的(調試)(CV開放2.4.0)運行facerec_demo.cpp gaves我計劃這個錯誤

這個錯誤我派生這條直線在facerec.cpp
(費舍爾::列車(InputArray SRC,InputArray _lbls)

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

和我在PGM IMG數據庫中使用,這是我原來的* 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; 
} 
+0

現在,我得到這個錯誤,在 文件 - > facerec.cpp 方法費舍爾::預測 線414說 墊Q = subspaceProject(_eigenvectors,_mean,src.reshape(1,1)); 當我加載我的樣本_eigenvectors和_mean dissapear !!! – rliberal 2012-07-23 14:44:08

回答

2

我看到你正在使用的OpenCV 2.4.0。作爲開發人員,我承認混淆是我的錯:當時我沒有徹底檢查傳遞給訓練方法的輸入數據,因此通過錯誤對齊數據的人會得到類似您的錯誤消息。很可能你看到的錯誤發生,因爲你的訓練圖像不具有相同的大小。這對於Eigenfaces和Fisherfaces算法(不適用於局部二值模式直方圖)是必需的。 OpenCV 2.4.0只是試圖將數據重塑爲矩陣,並隨着你看到的錯誤信息而爆發;相反,OpenCV 2.4.2會檢查(在訓練之前)輸入數據是否正確對齊,並拋出一個有意義的異常......並帶有非常明確的消息。

這篇文章假定它也可能是由於連接OpenCV的庫:

如果它不將它鏈接可能是由於圖像的大小圖書館。調整你的訓練圖像,可以很容易地實現與opencv cv::resize

但你可能要考慮切換到OpenCV的2.4.2,在這一切增加:

這個版本還附帶了一個廣泛的做cumentation在:

但是,如果你不能改變的OpenCV 2.4.2,你需要留在OpenCV的2.4.0,那麼你也可以使用libfacerec:

這是項目,即得到了合併到OpenCV的。我確信它可以與OpenCV 2.4.0一起使用,並且它將使您盡享與OpenCV 2.4.2版本相同的界面。所以一旦你想更新到OpenCV 2.4.2,你只能切換包含。

+0

謝謝!它現在起作用了! – rliberal 2012-07-25 01:44:07

+0

好聽!這個問題到底有多嚴重?可能您可以更新您的文章或在此添加解決方案作爲評論。它將幫助人們面對同樣的問題。 – bytefish 2012-07-25 16:54:18

0

我在另一篇文章中回答了這個問題,但我想確保搜索此錯誤幫助的人一定會找到答案。

時,你的模型

Ptr<FaceRecognizer> model = createFisherFaceRecognizer(); 

你需要傳遞兩個參數

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

This page has more information on how createFisherFaceRecognizer works

1

我得到了同樣的OpenCV的錯誤,我嘗試所有的幫助,我在這裏找到,它仍然給我一個例外(在.Predict()語句發生異常)。

問題在於圖像的大小。圖片的大小必須小於100像素(< 100px)(不確定是否小於100,大概100也可以)。

我改變我的圖片大小爲150:150到80:80,它的工作!

希望我幫助別人,因爲這是令人討厭的錯誤。