《OpenCV 4快速入门》案例:
关于SVM的数学理论及详细的几何意义,在大多数机器学习的书籍里都会详细介绍。
知乎有篇文章讲得很好。
【机器学习】支持向量机 SVM(非常详细)
该案例来自于冯振等人编写的《OpenCV 4快速入门》,这是一本好书,个人感觉写得很好,虽然数学原理没有过多描述,但会大致提一下基本思想和原理,很适合应用。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::ml;
int main()
{
//训练数据
Mat samples, labls;
FileStorage fread("point.yml", FileStorage::READ);
fread["data"] >> samples;
fread["labls"] >> labls;
fread.release();
//不同种类坐标点拥有不同的颜色
vector<Vec3b> colors;
colors.push_back(Vec3b(0, 255, 0));
colors.push_back(Vec3b(0, 0, 255));
//创建空白图像用于显示坐标点
Mat img(480, 640, CV_8UC3, Scalar(255, 255, 255));
Mat img2;
img.copyTo(img2);
//在空白图像中绘制坐标点
for (int i = 0; i < samples.rows; i++)
{
Point2f point;
point.x = samples.at<float>(i, 0);
point.y = samples.at<float>(i, 1);
Scalar color = colors[labls.at<int>(i, 0)];
circle(img, point, 3, color, -1);
circle(img2, point, 3, color, -1);
}
imshow("两类像素点图像", img);
//建立模型
Ptr<SVM> model = SVM::create();
//参数设置
model->setKernel(SVM::INTER); //内核的模型
model->setType(SVM::C_SVC); //SVM的类型
model->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 100, 0.01));
//model->setGamma(5.383);
//model->setC(0.01);
//model->setDegree(3);
//训练模型
model->train(TrainData::create(samples, ROW_SAMPLE, labls));
//用模型对图像中全部像素点进行分类
Mat imagePoint(1, 2, CV_32FC1);
for (int y = 0; y < img2.rows; y = y + 2)
{
for (int x = 0; x < img2.cols; x = x + 2)
{
imagePoint.at<float>(0) = (float)x;
imagePoint.at<float>(1) = (float)y;
int color = (int)model->predict(imagePoint);
img2.at<Vec3b>(y, x) = colors[color];
}
}
imshow("图像所有像素点分类结果", img2);
waitKey();
return 0;
}
"point.yml"文件内容如下:
%YAML:1.0
---
data: !!opencv-matrix
rows: 191
cols: 2
dt: f
data: [ 372., 265., 402., 247., 422., 227., 433., 183., 415., 144.,
384., 124., 364., 106., 306., 97., 221., 95., 260., 94., 345.,
283., 310., 287., 266., 295., 285., 316., 328., 315., 375., 301.,
425., 281., 443., 260., 449., 225., 458., 191., 456., 157., 445.,
121., 414., 105., 382., 92., 340., 90., 296., 83., 314., 73.,
265., 76., 232., 79., 192., 82., 151., 110., 118., 148., 93.,
193., 72., 246., 45., 307., 29., 355., 17., 394., 14., 426., 306.,
303., 387., 283., 420., 259., 404., 291., 440., 152., 421., 125.,
350., 74., 289., 62., 253., 62., 196., 67., 146., 96., 114., 146.,
93., 183., 112., 179., 78., 218., 54., 242., 41., 284., 39., 301.,
73., 304., 57., 274., 32., 337., 47., 351., 310., 183., 302.,
171., 262., 155., 283., 165., 276., 182., 287., 186., 244., 184.,
232., 201., 221., 222., 212., 203., 198., 222., 190., 250., 170.,
278., 162., 308., 156., 355., 161., 368., 177., 395., 228., 421.,
289., 426., 347., 416., 411., 404., 469., 383., 506., 365., 523.,
342., 543., 306., 557., 267., 573., 235., 580., 205., 599., 131.,
616., 72., 627., 32., 626., 62., 635., 114., 602., 102., 605.,
148., 614., 177., 590., 172., 595., 221., 599., 247., 625., 142.,
620., 97., 616., 131., 599., 198., 575., 251., 570., 295., 545.,
337., 511., 337., 214., 431., 252., 430., 196., 408., 180., 359.,
176., 328., 189., 291., 194., 269., 223., 228., 249., 199., 289.,
176., 333., 170., 358., 170., 370., 171., 313., 155., 306., 159.,
283., 156., 259., 167., 222., 177., 202., 208., 180., 242., 162.,
272., 145., 311., 138., 354., 147., 396., 161., 419., 196., 441.,
259., 456., 310., 456., 351., 451., 379., 435., 398., 414., 448.,
393., 626., 196., 602., 254., 577., 310., 561., 352., 545., 381.,
482., 416., 442., 436., 392., 449., 344., 467., 313., 464., 278.,
467., 246., 466., 232., 456., 358., 328., 314., 340., 288., 339.,
278., 327., 256., 312., 248., 293., 283., 301., 317., 322., 439.,
101., 396., 73., 343., 42., 357., 56., 273., 51., 298., 48., 232.,
46., 186., 55., 135., 80., 89., 137., 61., 199., 34., 276., 15.,
320., 112., 120., 346., 199., 330., 190., 318., 180., 354., 181.,
252., 159., 190., 190., 174., 231., 147., 265., 137., 310., 174.,
208., 149., 260., 162., 237., 160., 336., 150., 286., 173., 423.,
189., 386., 217., 398. ]
labls: !!opencv-matrix
rows: 191
cols: 1
dt: i
data: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
程序运行效果: