OpenCV HOG+SVM:断言失败 checkDetectorSize()

OpenCV HOG+SVM: assertion failed checkDetectorSize()

当我尝试将自己的 SVM 检测器设置为 openCV 中的方法 hog::setSVMDetector(Detector) 时,我在 c++ 中遇到了 openCV 问题。

我按照以下步骤操作 SVM classifier based on HOG features for "object detection" in OpenCV 但我卡在了第 3 步。

我正在使用 openCV 3.0,它目前内置于 SVM 中。 这就是我训练和构建检测器的方式:

火车

svm = SVM::create();
svm->setType(type);
svm->setKernel(kernel);
svm->setC(C);

if (kernel == SVM::LINEAR) {
    svm->setDegree(1);
} else if (kernel == SVM::POLY) {
    svm->setDegree(3);
}
svm->train(trainingSamples, ml::ROW_SAMPLE, labels);

建筑探测器

vector<float> alpha;
vector<float> svidx;
vector<float> model;

// Getting Support Vectors
Mat svs = svm->getSupportVectors();

double rho = svm->getDecisionFunction(0, alpha, svidx);

    // Computing w in primal form
for (int i = 0; i < svidx.size(); i++) {
    model.push_back(svs.at<float>(i, 0) * alpha.at(i));
    for (int j = 1; j < svs.cols; j++) {
        model.at(i) += svs.at<float>(i, j) * alpha.at(i);
    }
}

//   Adding rho
    model.push_back(rho);
    return model;

当我尝试将上述模型提供给时出现错误:

hog.setDetector(model); 

OpenCV Error: Assertion failed (checkDetectorSize()) in setSVMDetector, file /home/dario/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/hog.cpp, line 115 terminate called after throwing an instance of 'cv::Exception' what(): /home/dario/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/hog.cpp:115: error: (-215) checkDetectorSize() in function setSVMDetector

知道我做错了什么吗?

我通过重写上面的代码解决了这个问题

    Ptr<SVM> svm;
    HOGDescriptor my_hog;
    ...
    // Load the trained SVM.
    svm = StatModel::load<SVM>( "model.yml" );
    // Set the trained svm to my_hog
    vector< float > hog_detector;
    get_svm_detector( svm, hog_detector );
    my_hog.setSVMDetector( hog_detector );