博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV中使用SVM简介
阅读量:6867 次
发布时间:2019-06-26

本文共 3622 字,大约阅读时间需要 12 分钟。

下面这是opencv官方文档中的代码,我加了一部分注释:

1 #include "stdafx.h" 2 #include "opencv2/core/core.hpp" 3 #include "highgui.h" 4 #include "ml.h" 5  6 using namespace cv; 7  8 int _tmain(int argc, _TCHAR* argv[]) 9 {10     // 11     int width = 512, height = 512;12     Mat image = Mat::zeros(height, width, CV_8UC3);13 14     // set up training data15     float labels[4] = {
1.0, 1.0, -1.0, -1.0};16 Mat labelsMat(4, 1, CV_32FC1, labels);17 18 float trainingData[4][2] = { {
501, 10}, {
255, 10}, {
501, 255}, {
10, 501} };19 Mat trainingDataMat(4, 2, CV_32FC1, trainingData);20 21 // set up SVM's parameters,具体参数设置请看下文22 CvSVMParams params;23 params.svm_type = CvSVM::C_SVC;24 params.kernel_type = CvSVM::LINEAR;25 params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);26 27 // train the svm28 CvSVM SVM;29 SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);30 31 Vec3b green(0,255,0), blue(255,0,0);32 33 // show the decision region given by the SVM34 for (int i = 0; i < image.rows; ++ i)35 {36 for (int j = 0; j < image.cols; ++ j)37 {38 Mat sampleMat = (Mat_
(1,2) << i,j);39 40 // predict 函数使用训练好的SVM模型对一个输入的样本进行分类41 float response = SVM.predict(sampleMat);42 43 if (response == 1)44 {45 // 注意这里是(j,i),不是(i,j)46 image.at
(j,i) = green;47 }48 else49 {50 // 同上51 image.at
(j,i) = blue;52 }53 }54 }55 56 int thickness = -1;57 int lineType = 8;58 59 circle(image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType);60 circle(image, Point(255, 10), 5, Scalar( 0, 0, 0), thickness, lineType);61 circle(image, Point(501, 255), 5, Scalar(255,255,255), thickness, lineType);62 circle(image, Point( 10, 501), 5, Scalar(255,255,255), thickness, lineType);63 64 // show support vectors65 thickness = 2;66 lineType = 8;67 68 // 获得当前的支持向量的个数69 int c = SVM.get_support_vector_count();70 71 for (int i = 0; i < c; ++ i)72 {73 const float* v = SVM.get_support_vector(i);74 circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType);75 }76 77 imwrite("result.png", image); // save the image78 79 imshow("SVM Simple Example", image); // show it to the user80 waitKey(0);81 return 0;82 }

这里说一下CvSVMParams中的参数设置

1 CV_SVM 中的参数设置 2  3 svm_type: 4     CvSVM::C_SVC        C-SVC 5     CvSVM::NU_SVC       v-SVC 6     SvSVM::ONE_CLASS    一类SVM 7     CvSVM::EPS_SVR      e-SVR 8     CvSVM::NU_SVR       v-SVR 9     10 kernel_type:11     CvSVM::LINEAR       线性:u*v12     CvSVM::POLY        多项式(r*u'v + coef0)^degree13     CvSVM::RBF          RBF函数: exp(-r|u-v|^2)14     CvSVM::SIGMOID      sigmoid函数: tanh(r*u'v + coef0)15     16 成员变量17 degree:        针对多项式核函数degree的设置18 gamma:         针对多项式/rbf/sigmoid核函数的设置19 coef0:         针对多项式/sigmoid核函数的设置20 Cvalue:        为损失函数,在C-SVC、e-SVR、v-SVR中有效21 nu:            设置v-SVC、一类SVM和v-SVR参数22 p:             为设置e-SVR中损失函数的值23 class_weights: C_SVC的权重24 term_crit:     为SVM训练过程的终止条件。25                 其中默认值   degree = 0,26                             gamma = 1,27                             coef0 = 0,28                             Cvalue = 1,29                             nu = 0,30                             p = 0,31                             class_weights = 0

 

转载于:https://www.cnblogs.com/pakfahome/p/3607120.html

你可能感兴趣的文章
2-1、FileBeat入门
查看>>
农夫、狼、羊、菜问题
查看>>
CDR中如何让图形沿路径均匀分布
查看>>
HDU2044:一只小蜜蜂...
查看>>
LTRIM、RTRIM和TRIM在ORACLE中的用法:
查看>>
Mysql常用命令和常用函数
查看>>
启发式算法是什么意思
查看>>
吴恩达机器学习笔记43-SVM大边界分类背后的数学(Mathematics Behind Large Margin Classification of SVM)...
查看>>
iOS 内付费 (二)(转)
查看>>
VLOG丨树莓派Raspberry Pi 3安装PLEX并挂载USB硬盘打造最牛的微型家庭影音服务器2018...
查看>>
Android 自定义view实现上下滑动,大众点评,美团地图导航界面。
查看>>
CF1012A Photo of The Sky(思考,模拟)
查看>>
JFreeChart的简单使用
查看>>
linux基础【文件夹含义】
查看>>
char和varchar的区别
查看>>
(写给自己看)最短路系列
查看>>
阔别博客园两年
查看>>
Java 外企面试若干题
查看>>
Linux shell 脚本小记
查看>>
IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)...
查看>>