Opencv 3 SVM training

后端 未结 3 1873
一整个雨季
一整个雨季 2020-12-13 16:34

As you may know, many things changed in OpenCV 3 (in comparision to the openCV2 or the old first version).

In the old days, to train SVM one would use:



        
相关标签:
3条回答
  • 2020-12-13 16:39

    with opencv3.0, it's definitely different , but not difficult:

    Ptr<ml::SVM> svm = ml::SVM::create();
    // edit: the params struct got removed,
    // we use setter/getter now:
    svm->setType(ml::SVM::C_SVC);
    svm->setKernel(ml::SVM::POLY);
    svm->setGamma(3); 
    
    Mat trainData; // one row per feature
    Mat labels;    
    svm->train( trainData , ml::ROW_SAMPLE , labels );
    // ...
    Mat query; // input, 1channel, 1 row (apply reshape(1,1) if nessecary)
    Mat res;   // output
    svm->predict(query, res);
    
    0 讨论(0)
  • 2020-12-13 16:42

    I was porting my code from OpenCV 2.4.9 to 3.0.0-rc1 and had the same issue. Unfortunately the API has changes since the answer was posted, so I would like to update it accordingly:

    Ptr<ml::SVM> svm = ml::SVM::create();
    svm->setType(ml::SVM::C_SVC);
    svm->setKernel(ml::SVM::POLY);
    svm->setGamma(3);
    
    Mat trainData; // one row per feature
    Mat labels;    
    Ptr<ml::TrainData> tData = ml::TrainData::create(trainData, ml::SampleTypes::ROW_SAMPLE, labels);
    svm->train(tData);
    // ...
    Mat query; // input, 1channel, 1 row (apply reshape(1,1) if nessecary)
    Mat res;   // output
    svm->predict(query, res);
    
    0 讨论(0)
  • 2020-12-13 16:44

    I know this is an old post, but i came across it looking for the same solution. This tutorial is extremely helpful: http://docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html

    0 讨论(0)
提交回复
热议问题