目录
What is a SVM?
A Support Vector Machine (SVM) is a discriminative classifier formally defined by a separating hyperplane(超平面). In other words, given labeled training data (supervised learning), the algorithm outputs an optimal hyperplane which categorizes new examples.

官方文档的源代码
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
using namespace cv;
using namespace cv::ml;
using namespace std;
static void help()
{
cout << "\n--------------------------------------------------------------------------" << endl
<< "This program shows Support Vector Machines for Non-Linearly Separable Data. " << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
int main()
{
help();
const int NTRAINING_SAMPLES = 10;
// Number of training samples per class 每类分类样本的数目
const float FRAC_LINEAR_SEP = 0.9f;
// Fraction of samples which compose the linear separable part 组成线性可分部分的样本的数量
// Data for visual representation
const int WIDTH = 512, HEIGHT = 512;// 最后画的图的尺寸
Mat I = Mat::zeros(HEIGHT, WIDTH, CV_8UC3);// 三通道黑色背景图像
//--------------------- 1. Set up(设置) training data randomly ---------------------------------------
Mat trainData(2 * NTRAINING_SAMPLES, 2, CV_32F);//保存坐标信息,20行*2列
Mat labels(2 * NTRAINING_SAMPLES, 1, CV_32S);//保存标签信息,20行*1列
RNG rng(10); // Random value generation class //构造方法设定一个具体值,表示下面代码每次生成的结果都是一样的
// Set up the linearly separable part of the training data
int nLinearSamples = (int)(FRAC_LINEAR_SEP * NTRAINING_SAMPLES);// 10*0.9=9
//! [setup1]
// Generate random points for the class 1 为第一类创建随机点
Mat trainClass = trainData.rowRange(0, nLinearSamples);//9行2列
// The x coordinate of the points is in [0, 0.4)//x 的变化范围为横轴的0.4,
Mat c = trainClass.colRange(0, 1);
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(0.4 * WIDTH));//给序列填充随机数字 RNG rng(10) 数字越大,可填入随机数的选择越多
// The y coordinate of the points is in [0, 1)
c = trainClass.colRange(1, 2);
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(HEIGHT));
// Generate random points for the class 2 为第二类创建随机点
trainClass = trainData.rowRange(2 * NTRAINING_SAMPLES - nLinearSamples, 2 * NTRAINING_SAMPLES);
// The x coordinate of the points is in [0.6, 1]
c = trainClass.colRange(0, 1);
rng.fill(c, RNG::UNIFORM, Scalar(0.6 * WIDTH), Scalar(WIDTH));
// The y coordinate of the points is in [0, 1)
c = trainClass.colRange(1, 2);
rng.fill(c, RNG::UNIFORM, Scalar(0), Scalar(HEIGHT));
//! [setup1]
//------------------ Set up the non

最低0.47元/天 解锁文章
2053

被折叠的 条评论
为什么被折叠?



