在硬盘中存储CvSVM对象

时间:2014-01-30 08:56:22

标签: c++ opencv svm

我在opencv中为面部创建了SVM检测器。我想分开训练和预测的过程。因此,我想找到一种方法来存储我的硬盘CvSVM SVM对象,以便随时访问它而无需再次训练新模型。通过这种方式,我可以通过从硬盘加载SVM对象来使用预测方法。这在Matlab中非常简单,但我怎样才能在opencv c ++中成功呢?

使用加载和保存函数来加载和保存SVM模型会导致munmap_chunk()::无效指针消息(检测到glibc)消息。

    CvSVM SVM =  detect.SVMtrain("dbpath/");
    SVM.save("svmTrain.xml", 0);

    cout <<"Detection system in progress progress...\n";
    string pathFile = databasePath+ imageFilename;
    vector<float> confidence;
    detections = detect.detection(pathFile);
    CvSVM SVM;
    SVM.load("svmTrain.xml",0);
    confidence =  detect.SVMpredict(detections.at(0), SVM);
    //cout << "confidence is: " << confidence.at(0) << endl;

当我运行上面的代码时,我收到了上述消息。问题在于svmpredict功能。和SVMpredict功能:

vector<float> Detection::SVMpredict(Mat image, CvSVM SVM){


vector<float> res;

//Mat image = imread(fileName,0); //Read as a gray image
//cvtColor(sampleMat, sampleMat, CV_BGR2GRAY);
image = eigenfacesExtraction(image);
image.convertTo(image, CV_32FC1); // <-- Convert to CV_32F for matrix mult

float response = SVM.predict(image); res.push_back(response);
float val = SVM.predict(image,true); res.push_back(val);
cout << "svm result: "<< response <<" val: "<< val << endl;

return res;

}

1 个答案:

答案 0 :(得分:1)

直截了当。它是CvStatModel,因此它附带saveload方法。