opencv SVM

目录

What is a SVM?

官方文档的源代码

函数补充

colRange 和 rowRange

 RNG 随机数,rng.fill

 简化

多分类问题


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值