C++ Opencv 3 SVM 训练
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27114065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opencv 3 SVM training
提问by Hyman L.
As you may know, many things changed in OpenCV 3 (in comparision to the openCV2 or the old first version).
您可能知道,OpenCV 3 中的很多东西都发生了变化(与 openCV2 或旧的第一个版本相比)。
In the old days, to train SVM one would use:
在过去,训练 SVM 会使用:
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.
在 API 的第三个版本中,没有CvSVMParams
nor CvSVM
。令人惊讶的是,有一个关于 SVM 的文档页面,但它说明了一切,但没有说明如何真正使用它(至少我看不懂)。此外,互联网上似乎没有人使用 OpenCV 3.0 中的 SVM。
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?
您能否提供信息,如何将实际训练重写为 openCV 3?
回答by berak
with opencv3.0, it's definitely different , but not difficult:
使用 opencv3.0,绝对不同,但并不难:
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);
回答by BloodyD
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:
我正在将我的代码从 OpenCV 2.4.9 移植到 3.0.0-rc1 并且遇到了同样的问题。不幸的是,自从发布答案以来 API 发生了变化,所以我想相应地更新它:
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);
回答by Jdban101
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
我知道这是一篇旧帖子,但我在寻找相同的解决方案时遇到了它。本教程非常有帮助:http: //docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html