[code segments] OpenCV3.0 SVM with C++ interface
talk is cheap, show you the code:
/************************************************************************/
/* Name : OpenCV SVM test */
/* Date : 2015/11/7 */
/* Author : aban */
/************************************************************************/
// note : the code is modified from internet.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
using namespace cv;
bool plotSupportVectors = true;
int numTrainingPoints = 200;
int numTestPoints = 2000;
int size = 200;
int eq = 0;
// accuracy
float evaluate(cv::Mat& predicted, cv::Mat& actual) {
assert(predicted.rows == actual.rows);
int t = 0;
int f = 0;
for (int i = 0; i < actual.rows; i++) {
float p = predicted.at<float>(i, 0);
float a = actual.at<float>(i, 0);
if ((p >= 0.0 && a >= 0.0) || (p <= 0.0 && a <= 0.0)) {
t++;
}
else {
f++;
}
}
return (t * 1.0) / (t + f);
}
// plot data and class
void plot_binary(cv::Mat& data, cv::Mat& classes, string name) {
cv::Mat plot(size, size, CV_8UC3);
plot.setTo(cv::Scalar(255.0, 255.0, 255.0));
for (int i = 0; i < data.rows; i++) {
float x = data.at<float>(i, 0) * size;
float y = data.at<float>(i, 1) * size;
if (classes.at<float>(i, 0) > 0) {
cv::circle(plot, Point(x, y), 2, CV_RGB(255, 0, 0), 1);
}
else {
cv::circle(plot, Point(x, y), 2, CV_RGB(0, 255, 0), 1);
}
}
cv::namedWindow(name, CV_WINDOW_KEEPRATIO);
cv::imshow(name, plot);
}
// function to learn
int f(float x, float y, int equation) {
switch (equation) {
case 0:
return y > sin(x * 10) ? -1 : 1;
break;
case 1:
return y > cos(x * 10) ? -1 : 1;
break;
case 2:
return y > 2 * x ? -1 : 1;
break;
case 3:
return y > tan(x * 10) ? -1 : 1;
break;
default:
return y > cos(x * 10) ? -1 : 1;
}
}
// label data with equation
cv::Mat labelData(cv::Mat points, int equation) {
cv::Mat labels(points.rows, 1, CV_32FC1);
for (int i = 0; i < points.rows; i++) {
float x = points.at<float>(i, 0);
float y = points.at<float>(i, 1);
labels.at<float>(i, 0) = f(x, y, equation);
}
return labels;
}
void svm(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) {
Mat traning_label(trainingClasses.rows, 1, CV_32SC1);
for (int i = 0; i < trainingClasses.rows; i++){
traning_label.at<int>(i, 0) = trainingClasses.at<float>(i, 0);
}
cv::Ptr<cv::ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::Types::C_SVC);
svm->setKernel(ml::SVM::KernelTypes::RBF);
//svm->setDegree(0); // for poly
svm->setGamma(20); // for poly/rbf/sigmoid
//svm->setCoef0(0); // for poly/sigmoid
svm->setC(7); // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
//svm->setNu(0); // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
//svm->setP(0); // for CV_SVM_EPS_SVR
svm->setTermCriteria(TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 1000, 1E-6));
svm->train(trainingData, ml::SampleTypes::ROW_SAMPLE, traning_label);
cv::Mat predicted(testClasses.rows, 1, CV_32F);
svm->predict(testData, predicted);
cout << "Accuracy_{SVM} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions SVM");
// plot support vectors
if (plotSupportVectors) {
cv::Mat plot_sv(size, size, CV_8UC3);
plot_sv.setTo(cv::Scalar(255.0, 255.0, 255.0));
Mat support_vectors = svm->getSupportVectors();
for (int vecNum = 0; vecNum < support_vectors.rows; vecNum++){
cv::circle(plot_sv, Point(support_vectors.row(vecNum).at<float>(0)*size, support_vectors.row(vecNum).at<float>(1)*size), 3, CV_RGB(0, 0, 0));
}
namedWindow("Support Vectors", CV_WINDOW_KEEPRATIO);
cv::imshow("Support Vectors", plot_sv);
}
}
int main(){
cv::Mat trainingData(numTrainingPoints, 2, CV_32FC1);
cv::Mat testData(numTestPoints, 2, CV_32FC1);
cv::randu(trainingData, 0, 1);
cv::randu(testData, 0, 1);
cv::Mat trainingClasses = labelData(trainingData, eq);
cv::Mat testClasses = labelData(testData, eq);
plot_binary(trainingData, trainingClasses, "Training Data");
plot_binary(testData, testClasses, "Test Data");
svm(trainingData, trainingClasses, testData, testClasses);
waitKey(0);
return 0;
}
[code segments] OpenCV3.0 SVM with C++ interface的更多相关文章
- 【OpenCV】opencv3.0中的SVM训练 mnist 手写字体识别
前言: SVM(支持向量机)一种训练分类器的学习方法 mnist 是一个手写字体图像数据库,训练样本有60000个,测试样本有10000个 LibSVM 一个常用的SVM框架 OpenCV3.0 中的 ...
- Atitit opencv3.0 3.1 3.2 新特性attilax总结
Atitit opencv3.0 3.1 3.2 新特性attilax总结 1. 3.0OpenCV 3 的改动在哪?1 1.1. 模块构成该看哪些模块?2 2. 3.1新特性 2015-12-21 ...
- ubuntu14.04下安装cudnn5.1.3,opencv3.0,编译caffe及配置matlab和python接口过程记录
已有条件: ubuntu14.04+cuda7.5+anaconda2(即python2.7)+matlabR2014a 上述已经装好了,开始搭建caffe环境. 1. 装cudnn5.1.3,参照: ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- opencv3.0 在 android 上的使用
下载 OpenCV-3.0.0-android-sdk-1.zip 打开 intellj,新建立一个 android 工程后选择工程属性,导入模块(Import module from externa ...
- Caffe搭建:Ubuntu14.04 + CUDA7.0 + opencv3.0 + Matlab2014A
从Hinton在science上发表深度学习训练开创新的文章以来,深度学习火了整整有3年多,而且随着新的硬件和算法的提出,深度学习正在应用于越来越多的领域,发挥其算法的优势. 实验室并没有赶上第一波深 ...
- opencv3.0中contrib模块的添加+实现SIFT/SURF算法
平台:win10 x64 +VS 2015专业版 +opencv-3.x.+CMake+Anaconda3(python3.7.0) Issue说明:Opencv3.0版本已经发布了有一段时间,在这段 ...
- Windows环境中编译opencv3.0同时加入OPENCV_contrib库及解决遇到相关问题[contrib 必须要3.1以上的opencv才支持了]
更新:现在contrib库必须要opencv3.1以上才能支持编译通过了. 方法和步骤还是和本篇文章一样. ############################################## ...
- OpenCV3 Ref SVM : cv::ml::SVM Class Reference
OpenCV3 Ref SVM : cv::ml::SVM Class Reference OpenCV2: #include <opencv2/core/core.hpp>#inclu ...
随机推荐
- Spring-framework 源码导入 IntelliJ IDEA 记录
前言 想学习spring框架,不看源码怎么行.网上有很多教程,但是自己实施起来还是稍微有点坎坷的,不过最后还是成功了.遂以此文记录. 环境说明: Idea 2017.2.5 spring-frame ...
- ios开发-程序压后台后,悄悄的抓取数据~~
我们使用某个app的时候,当我们将程序压到后台之后,我们希望它还能从服务器抓取一些数据,类似微博,微信,qq这些程序压后台 之后,我们依然能看到icon上显示未读数量.但是ios系统是伪多任务操作系统 ...
- 【数据结构】【平衡树】无旋转treap
最近在研究平衡树,看起来这种东西又丧水又很深,感觉很难搞清楚.在Ditoly学长的建议下,我先学习了正常的treap,个人感觉这应该是平衡树当中比较好懂的而且比较好写的一种. 然而,发现带旋treap ...
- hdu 5885 FFT
XM Reserves Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)T ...
- bzoj 3751: [NOIP2014]解方程
Description 已知多项式方程: a0+a1x+a2x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数). 解题报告: 这题比较诡,看到高精度做不了,就要想到 ...
- [BZOJ]1017 魔兽地图DotR(JSOI2008)
BZOJ第一页做着做着就能碰到毒题,做到BZOJ1082小C就忍了,没想到下一题就是这种东西.这种题目不拖出来枭首示众怎么对得起小C流逝的青春啊. Description DotR (Defense ...
- hdu5635 BestCoder Round #74 (div.2)
LCP Array Accepts: 131 Submissions: 1352 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 13 ...
- hdu5630 BestCoder Round #73 (div.2)
Rikka with Chess Accepts: 393 Submissions: 548 Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- [bzoj4893]项链分赃
来自FallDream的博客,未经允许,请勿转载,谢谢. 有一串长度为n(n<=10^5)的项链,上面有红绿蓝三种颜色的珠子,每种颜色的珠子数目都是偶数,现在要你把它切几刀分成若干段,把其中一些 ...
- 2393Cirno的完美算数教室 容斥
2393: Cirno的完美算数教室 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 652 Solved: 389[Submit][Status][ ...