This series of learning notes is referenced from the OpenCV2.4.10Opencv\sources\samples\cpp\tutorial_code and http://www.opencv.org.cn/opencvdoc/2.3.2/html/genindex.html
SVM is a support vector machine. It is a classifier. Simply put, SVM is an optimal segmentation of a plane by a set of training samples.
introduction_to_svm.cpp (SVM support vector machine)
Demo source and comments are as follows:
#include "stdafx.h"//Precompiled Header file #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inc Lude <opencv2/ml/ml.hpp>using namespace Cv;int main () {//define a 512*512 pixel-sized image for SVM visual rendering int width =, height = 512; Mat image = Mat::zeros (height, width, cv_8uc3);//Set training data. Training data There are two classes 1 and -1 of these two classes have one and three data respectively. Float Labels[4] = {1.0,-1.0,-1.0,-1.0}; Mat Labelsmat (4, 1, CV_32FC1, labels), float trainingdata[4][2] = {{501, 10}, {255, 10}, {501, 255}, {10, 501}}; Mat Trainingdatamat (4, 2, CV_32FC1, trainingdata);//Set SVM parameter Cvsvmparams params;params.svm_type = cvsvm::c_svc; Params.kernel_type = Cvsvm::linear;params.term_crit = Cvtermcriteria (Cv_termcrit_iter, 1e-6);//training SVMCVSVM SVM; Svm.train (Trainingdatamat, Labelsmat, Mat (), Mat (), params); VEC3B Green (0,255,0), Blue (255,0,0);//The SVM classification of each point in the image and shading for (int i = 0; i < image.rows; ++i) for (int j = 0; J < im Age.cols; ++J) {Mat Samplemat = (mat_<float> () << 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;} Show training data int thickness = -1;int Linetype = 8;circle (Image, point (501, ten), 5, Scalar (0, 0, 0), thickness, linetype); Circle (image, point (255, ten), 5, scalar (255, 255, 255), thickness, linetype); Circle (Image, point (501, 255), 5, scalar (255 , 255, 255), thickness, linetype); Circle (image, point (ten, 501), 5, Scalar (255, 255, 255), thickness, linetype);//display support vector th ickness = 2;linetype = 8;int c = svm.get_support_vector_count (); for (int i = 0; i < C; ++i) {Const float* v = SVM.G Et_support_vector (i); Circle (image, point (int) v[0], (int) v[1]), 6, Scalar (n, N, N), thickness, linetype);} Imwrite ("Result.png", image); Save the Imageimshow ("SVM simple Example", image); Show it to the Userwaitkey (0);}
Run:
the first is about the parameters of SVM, Params.svm_type is the SVM type, and Cvsvm::c_svc indicates that SVM can deal with the problem of imperfect classification. Here the data can be split first, so the significance is small. The Params.kernel_type parameter is the SVM kernel type, cvsvm::linear, linear kernel, no mapping, and is the quickest way. The Params.term_crit is the end condition for the SVM. The Cvtermcriteria structure is the end condition, and theCv_termcrit_iter represents the end before the maximum number of iterations, 100 represents the maximum number of iterations, and 1e-6 is the precision requirement. The function of Svm.train is to train the SVM classifier based on the sample, function declarationC + +: BOOLCVSVM::Train(Const mat&Traindata, const mat&Responses, const mat&Varidx=mat (), const mat&Sampleidx=mat (), Cvsvmparamsparams=cvsvmparams ())The parameter traindata is the data to be trained, and theresponses is the classification result of the training data. Cvsvmparams is the SVM classifier parameter. Svm.get_support_vector_count () is used to obtain support vector data. Svm.get_support_vector (i) Gets the support vector based on the index.
OPENCV2.4.10 's Samples_cpp_tutorial-code_learn-----ml (SVM support vector machine one)