Opencv 3 SVM training

22,994

Solution 1

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);

Solution 2

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);

Solution 3

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

Share:
22,994
Jack L.
Author by

Jack L.

Poznan University of Technology's IT 2nd year student and Front-end developer. Guitar, bass, drums, harmonica and piano fan.

Updated on July 09, 2022

Comments

  • Jack L.
    Jack L. almost 2 years

    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:

    CvSVMParams params;
    params.svm_type = CvSVM::C_SVC;
    params.kernel_type = CvSVM::POLY;
    params.gamma = 3;
    
    CvSVM svm;
    svm.train(training_mat, labels, Mat(), Mat(), params);
    

    In the third version of API, there is no CvSVMParams nor CvSVM. Surprisingly, there is a documentation page about SVM, but it tells everything, but not how to really use it (at least I cannot make it out). Moreover, it looks like no one in the Internet uses SVM from OpenCV's 3.0.

    Currently, I only managed to get the following:

    ml::SVM.Params params;
    params.svmType = ml::SVM::C_SVC;
    params.kernelType = ml::SVM::POLY;
    params.gamma = 3;
    

    Can you please provide me with information, how to rewrite the actual training to openCV 3?

  • Jack L.
    Jack L. over 9 years
    Works like a charm, thanks for the very quick response! Can you please tell me, how is result represented in res cv::Mat? Do it contain just the same values, that were in labels cv::Mat?
  • berak
    berak over 9 years
    res.at<something>(0) will contain the id. 'something' will be float or int here, depending on the type of labels
  • Koray
    Koray over 7 years
    This information: "Mat query; // input, 1channel, 1 row (apply reshape(1,1) if nessecary)" changed my implemetation's success a lot. Thank you @berak
  • Jake OPJ
    Jake OPJ over 6 years
    That tutorial still uses SVM::Params params which is obsolete.