简体   繁体   中英

How to port the MATLAB libSVM parameters in C++

In my cross-validation in MATLAB with libSVM I found that these are the best parameters to use:

model = svmtrain( labels, training, '-s 0 -t 2 -c 10000 -g 100');

Now I want to replicate the classification in C++ with OpenCV.

But I do not understand how to set the C++ parameters to be the same as MATLAB:

Based on this documentation I tried the following:

CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::RBF;
//params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 10000, 1e-6);
params.Cvalue = 10000;
params.gamma = 100;
CvSVM SVM;
SVM.train(train, labels, Mat(), Mat(), params);

but I get this error:

error: no member named 'Cvalue' in 'CvSVMParams' params.Cvalue = 10000;

Last thing, should I uncomment

 //params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 10000, 1e-6); 

and try other values or is it not important? Because I can't even understand in MATLAB how to set the same parameters.

Not every parameter has an exact equivalent when porting from LibSVM in matlab to OpenCV SVM. The term criteria is one of them. Keep in mind that the SVM of opencv might have some bugs depending on the version you use (not an issue with the latest version).

You should un-comment the line, to have better control of your termination criteria. This line says that the algorithm should end when 10000 iterations are performed. If you use CV_TERMCRIT_EPS, it will stop when a precision less than the specified (for you, its 1e-6) is achieved for each vector. Use both stopping criteria, and it will stop when either of them completes.

Alternatively, could also try using LibSVM for C++, by linking it as a library. This will give you the exact same algorithms and functions that you are using in matlab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM