2015-08-23 66 views
0

我正在努力爲OpenCV3中的圖像分類訓練svm分類器編寫實用程序。但是在訓練過程中我有浮點異常(core dumped)錯誤。如何形成支持向量機的訓練數據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,黑白色&準備。

而且列車實際調用這個方法

有人能告訴我什麼,我做錯了。

謝謝,

回答

1

它很簡單。將標籤格式更改爲CV_32SC1。它肯定會解決您在opencv 3.0毫升的問題。

+0

仍然崩潰... – Steva

相關問題