opencv学习笔记七十二:SVM训练分类

简单二分类 实验:

#include 
#include   
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace cv::ml;
using namespace std;

int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);  //创建窗口可视化

// 设置训练数据
int labels[10] = { 1, 0, 1, 1,0,1,0,1,0,0 };
Mat labelsMat(10, 1, CV_32SC1, labels);

float trainingData[10][2] = { { 501, 150 },{ 255, 10 },{ 501, 255 },{ 10, 501 },{ 25, 80 },{ 150, 300 },{ 77, 200 } ,{ 300, 300 } ,{ 45, 250 } ,{ 200, 200 } };
Mat trainingDataMat(10, 2, CV_32FC1, trainingData);

// 创建分类器并设置参数
Ptr model = SVM::create();
model->setType(SVM::C_SVC);  //多分类
model->setKernel(SVM::LINEAR);  //核函数

//设置训练数据 
Ptr tData = TrainData::create(trainingDataMat, ROW_SAMPLE, labelsMat);

// 训练分类器
model->train(tData);

Vec3b green(0, 255, 0), blue(255, 0, 0);
// Show the decision regions given by the SVM
for (int i = 0; i < image.rows; ++i) {
	for (int

你可能感兴趣的:(opencv)