目录
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-linearly separable part (非线性部分) of the training data ---------------
//! [setup2]
// Generate random points for the classes 1 and 2
trainClass = trainData.rowRange(nLinearSamples, 2 * NTRAINING_SAMPLES - nLinearSamples);
// The x coordinate of the points is in [0.4, 0.6)
c = trainClass.colRange(0, 1);
rng.fill(c, RNG::UNIFORM, Scalar(0.4 * WIDTH), Scalar(0.6 * 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));
//! [setup2]
//------------------------- Set up the labels for the classes ---------------------------------
labels.rowRange(0, NTRAINING_SAMPLES).setTo(1); // Class 1
labels.rowRange(NTRAINING_SAMPLES, 2 * NTRAINING_SAMPLES).setTo(2); // Class 2
//------------------------ 2. Set up the support vector machines parameters --------------------
cout << "Starting training process" << endl;
//! [init]
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setC(0.1);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
//! [init]
//------------------------ 3. Train the svm ----------------------------------------------------
//! [train]
svm->train(trainData, ROW_SAMPLE, labels);
//! [train]
cout << "Finished training process" << endl;
//------------------------ 4. Show the decision regions ----------------------------------------
//! [show]
Vec3b green(0, 100, 0), blue(100, 0, 0);
for (int i = 0; i < I.rows; i++)
{
for (int j = 0; j < I.cols; j++)
{
Mat sampleMat = (Mat_<float>(1, 2) << j, i);
float response = svm->predict(sampleMat);
if (response == 1) I.at<Vec3b>(i, j) = green;
else if (response == 2) I.at<Vec3b>(i, j) = blue;
}
}
//! [show]
//----------------------- 5. Show the training data --------------------------------------------
//! [show_data]
int thick = -1;
float px, py;
// Class 1
for (int i = 0; i < NTRAINING_SAMPLES; i++)
{
px = trainData.at<float>(i, 0);
py = trainData.at<float>(i, 1);
circle(I, Point((int)px, (int)py), 3, Scalar(0, 255, 0), thick);
}
// Class 2
for (int i = NTRAINING_SAMPLES; i < 2 * NTRAINING_SAMPLES; i++)
{
px = trainData.at<float>(i, 0);
py = trainData.at<float>(i, 1);
circle(I, Point((int)px, (int)py), 3, Scalar(255, 0, 0), thick);
}
//! [show_data]
//------------------------- 6. Show support vectors --------------------------------------------
//! [show_vectors]
thick = 2;
Mat sv = svm->getUncompressedSupportVectors();
for (int i = 0; i < sv.rows; i++)
{
const float* v = sv.ptr<float>(i);
circle(I, Point((int)v[0], (int)v[1]), 6, Scalar(128, 128, 128), thick);
}
//! [show_vectors]
imwrite("result.png", I); // save the Image
imshow("SVM for Non-Linear Training Data", I); // show it to the user
waitKey();
return 0;
}
函数补充
colRange 和 rowRange
#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;
int main(int argc, char** argv)
{
Mat Test = (Mat_<double>(3, 3) << 0, 1, 2, 3, 4, 5, 6, 7, 8);
cout << "Total matrix:" << endl;
cout << Test << endl << endl;
Mat Test_col = Test.colRange(0,2);// 保存前2列的元素
cout << "col range:" << endl;
cout << Test_col << endl << endl;
Mat Test_row = Test.rowRange(0, 2);// 保存前2行的元素
cout << "row range:" << endl;
cout << Test_row << endl << endl;
}
RNG 随机数,rng.fill
#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;
int main(int argc, char** argv)
{
RNG rng(10);//里面的数值设定之后,每次产生的随机数都一样,类似于numpy的seed
for (int i = 0; i < 10; i++) {
cout << "round " << i << endl;
cout << "random number = " << rng.uniform(1, 10) << endl;
}
}
#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;
RNG rng(10);
int main(int argc, char** argv)
{
Mat Test = (Mat_<double>(3, 3) << 0, 1, 2, 3, 4, 5, 6, 7, 8);
cout << "Total matrix:" << endl;
cout << Test << endl << endl;
Mat Test_col = Test.colRange(0, 1);
cout << "col range:" << endl;
cout << Test_col << endl << endl;
rng.fill(Test_col, RNG::UNIFORM,2,10);
cout << "new col range:" << endl;
cout << Test_col << endl << endl;
cout << "new Total matrix:" << endl;
cout << Test << endl << endl;
}
简化
仅仅使用简单数据实现整个流程
#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;
RNG rng(10);
int main(int argc, char** argv)
{
//设置训练数据
int labels[4] = { 1, 1, -1, -1 };
float trainingData[4][2] = { {0, 50}, {255, 40}, {501, 255}, {10, 501} };
Mat trainingDataMat(4, 2, CV_32F, trainingData);
Mat labelsMat(4, 1, CV_32SC1, labels);
cout << "训练数据" << "\n" << "trainingDataMat" << "\n" << trainingDataMat << "\n" << "labelsMat" << "\n" << labelsMat << endl;
//训练SVM
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
//开始训练
svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);
//可视化表示
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);
//显示由支持向量机给出的决策区域
Vec3b green(0, 255, 0), blue(255, 0, 0);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
Mat sampleMat = (Mat_<float>(1, 2) << j, i);
float response = svm->predict(sampleMat);
if (response == 1)
image.at<Vec3b>(i, j) = green;
else if (response == -1)
image.at<Vec3b>(i, j) = blue;
}
}
//把训练数据在图片上进行显示
int thickness = -1;
//类别1,白色
circle(image, Point(0, 50), 5, Scalar(255, 255, 255), thickness);
circle(image, Point(255, 40), 5, Scalar(255, 255, 255), thickness);
//类别-1,黑色
circle(image, Point(501, 255), 5, Scalar(0, 0, 0), thickness);
circle(image, Point(10, 501), 5, Scalar(0, 0, 0), thickness);
//显示支持相量,距离最短的
thickness = 2;
Mat sv = svm->getUncompressedSupportVectors();
for (int i = 0; i < sv.rows; i++)
{
const float* v = sv.ptr<float>(i);
circle(image, Point( (int) v[0], (int) v[1]), 6, Scalar(0, 0, 255), thickness);
}
imshow("SVM", image);
waitKey(0);
destroyAllWindows();
return 0;
}
多分类问题
#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;
RNG rng(10);
//三分类问题
int main(int argc, char** argv)
{
//设置训练数据
int labels[6] = { 1, 1, 2, 2, 3, 3 };
float trainingData[6][2] = { {0, 50}, {255, 40}, {501, 255}, {10, 501},{100,350},{400,350} };
Mat trainingDataMat(6, 2, CV_32F, trainingData);
Mat labelsMat(6, 1, CV_32SC1, labels);
cout << "训练数据" << "\n" << "trainingDataMat" << "\n" << trainingDataMat << "\n" << "labelsMat" << "\n" << labelsMat << endl;
//训练SVM
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
//开始训练
svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);
//可视化表示
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);
//显示由支持向量机给出的决策区域
Vec3b green(0, 255, 0), blue(255, 0, 0),red(0,0,255);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
Mat sampleMat = (Mat_<float>(1, 2) << j, i);
float response = svm->predict(sampleMat);
if (response == 1)
image.at<Vec3b>(i, j) = green;
else if (response == 2)
image.at<Vec3b>(i, j) = blue;
else if (response == 3)
image.at<Vec3b>(i, j) = red;
}
}
//把训练数据在图片上进行显示
int thickness = -1;
//类别1,红色 背景绿色
circle(image, Point(0, 50), 5, Scalar(0, 0, 255), thickness);
circle(image, Point(255, 40), 5, Scalar(0, 0, 255), thickness);
//类别2,绿色 背景蓝色
circle(image, Point(501, 255), 5, Scalar(0, 255, 0), thickness);
circle(image, Point(10, 501), 5, Scalar(0, 255, 0), thickness);
//类别3,蓝色 背景红色
circle(image, Point(100, 350), 5, Scalar(0, 255, 0), thickness);
circle(image, Point(400, 350), 5, Scalar(0, 255, 0), thickness);
//显示支持相量,距离最短的
thickness = 2;
Mat sv = svm->getUncompressedSupportVectors();
for (int i = 0; i < sv.rows; i++)
{
const float* v = sv.ptr<float>(i);
circle(image, Point((int)v[0], (int)v[1]), 6, Scalar(255,2555,255), thickness);
}
imshow("SVM", image);
waitKey(0);
destroyAllWindows();
return 0;
}