【问题标题】:How to import a trained SVM detector in OpenCV 2.4.13如何在 OpenCV 2.4.13 中导入训练有素的 SVM 检测器
【发布时间】:2017-11-06 10:36:21
【问题描述】:

所以我按照本指南训练我自己的行人 HOG 检测器。 https://github.com/DaHoC/trainHOG/wiki/trainHOG-Tutorial

并且成功生成了 4 个文件。

  • cvHOGClassifier.yaml
  • descriptorvector.dat
  • features.dat
  • svmlightmodel.dat

有谁知道如何将descriptorvector.dat 文件加载为向量? 我已经尝试过了,但失败了。

    vector<float> detector;
    std::ifstream file;
    file.open("descriptorvector.dat");
    file >> detector;
    file.close();

这是我最终想使用的东西。

    gpu::HOGDescriptor hog(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8),9);
    hog.setSVMDetector(detector);

提前感谢您!

【问题讨论】:

    标签: c++ opencv gpu svm


    【解决方案1】:

    您可以读取文件并将每个值推回float 的数组,因此首先声明它:

    vector<float> myDescriptorVector;
    

    然后推送每个值:

    ifstream infile ("yourFile.dat".c_str());
    
    float number;
    
    while (infile >> number)
        myDescriptorVector.push_back(number);
    
    
    return myDescriptorVector;
    

    最后使用 HOG 的标准初始化并将向量传递给您已经猜到的 SVM 检测器:

    hog.setSVMDetector(myDescriptorVector);
    

    【讨论】:

      【解决方案2】:

      如果您已经训练了 SVM,则可以将权重和截距保存在 TXT 文件中,然后将其加载到数组/向量中。然后您可以按如下方式使用它:

      std::vector<float> descriptorsValues;   //A vector to store the computed HoG values
      std::vector<cv::Point> locations;
      hog.compute(image, descriptorsValues, cv::Size(0, 0), cv::Size(0, 0), locations);
      double res = 0;
      for (int i = 0; i < svmDimension - 1; i++)
      {
          res += w[i] * descriptorsValues.at(i);
      }
      res = res + w[svmDimension - 1];            
      return res;
      

      svmDimension 是数组/向量,其中包含 SVM 权重和 SVM 截距,res 是 SVM 响应

      【讨论】:

        猜你喜欢
        • 2015-05-04
        • 2015-07-25
        • 2017-06-27
        • 1970-01-01
        • 2016-05-13
        • 2016-10-31
        • 2015-04-18
        • 2015-01-22
        相关资源
        最近更新 更多