【问题标题】:How to form data for SVM training OpenCV3如何为 SVM 训练 OpenCV3 形成数据
【发布时间】:2015-11-17 01:53:36
【问题描述】:

我正在尝试编写用于训练 svm 分类器以在 OpenCV3 中进行图像分类的实用程序。但是我在训练过程中有浮点异常(核心转储)错误。

我的主要问题是我不知道,我不确定如何形成训练数据以提供 svm.train 方法。

这是形成训练数据的代码。

TrainingDataType SVMTrainer::prepareDataForTraining() {

    cv::Mat trainingData(m_numOfAllImages, 28*28, CV_32FC1);
    cv::Mat trainingLabels(m_numOfAllImages, 1, CV_32FC1);

    int rowNum = 0;


    // Item is pair of classId (int) and vector of images.
    for(auto item : m_data){
        int classId = item.first;
        for(auto item1 : item.second){
            Mat temp = item1.reshape(1,1);
            temp.copyTo(trainingData.row(rowNum));

            trainingLabels.at<float>(rowNum) = item.first;
            ++rowNum;
        }
    }

    return cv::ml::TrainData::create(trainingData,
                                     cv::ml::SampleTypes::ROW_SAMPLE, 
                                     trainingLabels) ;

}

void SVMTrainer::train(std::string& configPath){
    // Read and store images in memory.
    formClassifierData(configPath);

    m_classifier = cv::ml::SVM::create();
    // Training parameters:
    m_classifier->setType(cv::ml::SVM::C_SVC);
    m_classifier->setKernel(cv::ml::SVM::POLY);
    m_classifier->setGamma(3);  
    m_classifier->setDegree(3);

    TrainingDataType trainData =  prepareDataForTraining();

    m_classifier->trainAuto(trainData);

}

所有图片均已准备好,尺寸为 28*28,黑白。

实际的火车呼叫是在这个方法中

谁能告诉我我做错了什么。

谢谢,

【问题讨论】:

    标签: c++ opencv machine-learning svm opencv3.0


    【解决方案1】:

    很简单。将标签格式更改为 CV_32SC1。它肯定会在 opencv 3.0 ml 中解决您的问题。

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2018-04-08
      • 2015-09-26
      • 2016-08-09
      • 2015-08-14
      • 2016-06-09
      • 2019-07-12
      • 2015-06-24
      • 2014-05-18
      相关资源
      最近更新 更多