Talk was cheap, show you the code:
/************************************************************************// * NAME:OPENCV SVM Test * // * DATE:2015/11/7 * // * Author:aban * //************************************************************************///Note:the code is modified from the Internet.#include <iostream>#include <Cmath>#include <string>using namespace Std;#include <Opencv2/opencv.Hpp>#include <Opencv2/ml.Hpp>Using namespace Cv;bool plotsupportvectors= true; int numtrainingpoints= $; int numtestpoints= -; int size= $; inteq = 0;//Accuracyfloat Evaluate (CV:: Mat&Predicted, CV:: Mat&Actual) {ASSERT (predicted.rows ==Actual.rows); int T= 0; int F= 0; for (int i= 0; I<Actual.rows; I++) {Float P=Predicted.At<Float>I0); Float A=Actual.At<Float>I0);if(p>= 0.0 &&A>= 0.0)||(p<= 0.0 &&A<= 0.0)) {T++; }Else{F++; } }return(t* 1.0)/(t+f);}//Plot data and classvoidPlot_binary (CV:: Mat& Data, CV:: Mat&ClassesstringName) {CV:: MatPlot (size, size, cv_8uc3); Plot.Setto (CV:: Scalar(255.0,255.0,255.0)); for (int i= 0; I< Data.rows; I++) {float X= Data.At<Float>I0)*Size Float y= Data.At<Float>I1)*Sizeif(Classes.At<Float>I0)> 0) {CV:: Circle(Plot, point (x, y),2, Cv_rgb (255,0,0),1); }Else{CV:: Circle(Plot, point (x, y),2, Cv_rgb (0,255,0),1); }} CV:: Namedwindow(name, cv_window_keepratio); Cv:: Imshow(name, plot);}//function to learnint f (float x, float y, int equation) {switch (equation) { Case 0:returnY>Sin (x* Ten)? - 1 : 1 ; Break case 1 : return y > cos (x * 10 ) - 1 : 1 ; Break case 2 : return y > 2 * x
- 1 : 1; Break Case 3: return y > tan (x * ten) ?
- 1 : 1; Break Default: return y > cos (x * ten) ?
-1:1; }}//Label data with equationCv:: MatLabeldata (CV:: Matpoints, int equation) {CV:: MatLabels (points.rows,1, CV_32FC1); for (int i= 0; I<Points.rows; I++) {float X=Points.At<Float>I0); Float y=Points.At<Float>I1); Labels.At<Float>I0)=f (x, y, equation); }returnLabels;}voidSVM (CV:: Mat&Trainingdata, CV:: Mat&Trainingclasses, CV:: Mat&TestData, CV:: Mat&testclasses) {Mat Traning_label (trainingclasses.rows,1, CV_32SC1); for (int i= 0; I<Trainingclasses.rows; I++) {Traning_label.At<Int>I0)=Trainingclasses.At<Float>I0); } CV::P TR<Cv:: ML:: SVM>Svm=ml:: SVM:: Create(); Svm -SetType (ML:: SVM:: Types:: C_svc); Svm -Setkernel (ML:: SVM:: Kerneltypes:: RBF);//svm->setdegree (0); For PolySvm -SetGamma ( -);//For poly/rbf/sigmoid //svm->setcoef0 (0); For Poly/sigmoidSvm -SETC (7);//For Cv_svm_c_svc, Cv_svm_eps_svr and Cv_svm_nu_svr //svm->setnu (0); For Cv_svm_nu_svc, Cv_svm_one_class, and Cv_svm_nu_svr //svm->setp (0); For Cv_svm_eps_svrSvm -Settermcriteria (Termcriteria (Termcriteria:: COUNT +Termcriteria:: EPS, +,1E-6)); Svm -Train (Trainingdata, ML:: Sampletypes:: Row_sample, Traning_label); Cv:: MatPredicted (testclasses.rows,1, cv_32f); Svm -Predict (TestData, predicted); cout<< "ACCURACY_{SVM} =" <<Evaluate (predicted, testclasses)<<Endl Plot_binary (TestData, predicted,"Predictions SVM");//Plot support vectors if(plotsupportvectors) {CV:: MatPLOT_SV (size, size, cv_8uc3); Plot_sv.Setto (CV:: Scalar(255.0,255.0,255.0)); Mat support_vectors=Svm -Getsupportvectors (); for (int vecnum= 0; Vecnum<Support_vectors.rows; Vecnum++) {CV:: Circle(PLOT_SV, point (support_vectors.Row (Vecnum).At<Float>(0)*Size, support_vectors.Row (Vecnum).At<Float>(1)*Size),3, Cv_rgb (0,0,0)); } Namedwindow ("Support Vectors", Cv_window_keepratio); Cv:: Imshow("Support Vectors", PLOT_SV); }}int Main () {CV:: MatTrainingdata (Numtrainingpoints,2, CV_32FC1); Cv:: MatTestData (Numtestpoints,2, CV_32FC1); Cv:: Randu(Trainingdata,0,1); Cv:: Randu(TestData,0,1); Cv:: MatTrainingclasses=Labeldata (Trainingdata,eq); Cv:: MatTestclasses=Labeldata (TestData,eq); Plot_binary (Trainingdata, Trainingclasses,"Training Data"); Plot_binary (TestData, Testclasses,"Test Data"); SVM (Trainingdata, trainingclasses, TestData, testclasses); Waitkey (0);return 0;}
[Code Segments] OpenCV3.0 SVM with C + + interface