1.3.25 bag of words results are visualized and tested using training results.
Let's take a look at the use of SVM in opencv2.2, as shown in the example in (1.
// Init data
Float fdata [25] = {0.608,-1.590, 0.235, 3.949,-2.249, 2.704,-2.473,-0.672, 0.262, 1.072, 1.773, 0.537, 3.240, 2.400, -2.499, 2.608,-3.458, 0.257, 2.569, 1.415, 1.410,-2.653, 1.396, 3.286,-0.712 };
CV: mat DATA = CV: MAT (25, 1, cv_32fc1, fdata );
Float fbestlabels [25] = };
CV: mat bestlabels = CV: MAT (25, 1, cv_32fc1, fbestlabels );
Int K = 2;
Cvmat mdata = data; // cvmat is a structure. CV: mat is a class. Why can we assign values like this,
// However, this is the case in the source code of opencv. Should it be the benefit of pointers?
Cvmat mbestlabels = bestlabels;
CV: svmparams = CV: svmparams (cvsvm: c_svc, cvsvm: RBF, cvsvm: Degree, cvsvm: gamma, cvsvm: coef, 2, cvsvm:: Nu, 0.0001, null, cvtermcriteria (cv_termcrit_eps, 100, 0.0001); // In the SVM parameter, cvalue does not know what it means to guess the number of classes. Therefore, 2 is entered.
CV: SVM;
SVM. Train (& mdata, & mbestlabels, svmparams); // if you use the train_auto method, you can use CV: mat to replace cvmat. 04.12
// Construct Test Data
Float tdata [1] = {-0.708 };
CV: mat ttdata = CV: MAT (1, 1, cv_32fc1, tdata );
Cvmat mttdata = ttdata;
Float result = SVM. predict (& mttdata); // if you want to test a set of data, you can only call the predict method with one vector and one vector?
// Similarly, predict has three forms, but only supports cvmat input. In fact, the best way is CV: mat.
Printf ("F %", result );
Return 0;
Supplement: When the SVM. Train () parameter is input according to the smart prompt, It is not mentioned that there are two train formats, one of which must be supported by swig. It seems that it takes some time to use vs C ++. Skip this step first.
Swig support is required, but the change log of opencv says swig-based Python wrappers are not supported anymore.
Does not support CV: mat
You can easily convert cvmat into CV: mat, and assign values directly through CV: MAT (cvmat * data. (Refer to SVM. cpp. The first method is actually encapsulated with 2nd methods and assigned values directly .)
CV: the mat is obviously easier to use than the cvmat.