OpenCV 2.4.3 (RC)

org.opencv.ml
Class CvSVM

java.lang.Object
  extended by org.opencv.ml.CvStatModel
      extended by org.opencv.ml.CvSVM

public class CvSVM
extends CvStatModel

Support Vector Machines.

See Also:
org.opencv.ml.CvSVM : public CvStatModel

Field Summary
static int C
           
static int C_SVC
           
static int COEF
           
static int DEGREE
           
static int EPS_SVR
           
static int GAMMA
           
static int LINEAR
           
static int NU
           
static int NU_SVC
           
static int NU_SVR
           
static int ONE_CLASS
           
static int P
           
static int POLY
           
static int RBF
           
static int SIGMOID
           
 
Fields inherited from class org.opencv.ml.CvStatModel
nativeObj
 
Constructor Summary
  CvSVM()
          Default and training constructors.
protected CvSVM(long addr)
           
  CvSVM(Mat trainData, Mat responses)
          Default and training constructors.
  CvSVM(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
          Default and training constructors.
 
Method Summary
 void clear()
           
protected  void finalize()
           
 int get_support_vector_count()
           
 int get_var_count()
          Returns the number of used features (variables count).
 void predict_all(Mat samples, Mat results)
          Predicts the response for input sample(s).
 float predict(Mat sample)
          Predicts the response for input sample(s).
 float predict(Mat sample, boolean returnDFVal)
          Predicts the response for input sample(s).
 boolean train_auto(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
          Trains an SVM with optimal parameters.
 boolean train_auto(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params, int k_fold, CvParamGrid Cgrid, CvParamGrid gammaGrid, CvParamGrid pGrid, CvParamGrid nuGrid, CvParamGrid coeffGrid, CvParamGrid degreeGrid, boolean balanced)
          Trains an SVM with optimal parameters.
 boolean train(Mat trainData, Mat responses)
          Trains an SVM.
 boolean train(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
          Trains an SVM.
 
Methods inherited from class org.opencv.ml.CvStatModel
load, load, save, save
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

C

public static final int C
See Also:
Constant Field Values

C_SVC

public static final int C_SVC
See Also:
Constant Field Values

COEF

public static final int COEF
See Also:
Constant Field Values

DEGREE

public static final int DEGREE
See Also:
Constant Field Values

EPS_SVR

public static final int EPS_SVR
See Also:
Constant Field Values

GAMMA

public static final int GAMMA
See Also:
Constant Field Values

LINEAR

public static final int LINEAR
See Also:
Constant Field Values

NU

public static final int NU
See Also:
Constant Field Values

NU_SVC

public static final int NU_SVC
See Also:
Constant Field Values

NU_SVR

public static final int NU_SVR
See Also:
Constant Field Values

ONE_CLASS

public static final int ONE_CLASS
See Also:
Constant Field Values

P

public static final int P
See Also:
Constant Field Values

POLY

public static final int POLY
See Also:
Constant Field Values

RBF

public static final int RBF
See Also:
Constant Field Values

SIGMOID

public static final int SIGMOID
See Also:
Constant Field Values
Constructor Detail

CvSVM

public CvSVM()

Default and training constructors.

The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.

See Also:
org.opencv.ml.CvSVM.CvSVM

CvSVM

protected CvSVM(long addr)

CvSVM

public CvSVM(Mat trainData,
             Mat responses)

Default and training constructors.

The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.

Parameters:
trainData - a trainData
responses - a responses
See Also:
org.opencv.ml.CvSVM.CvSVM

CvSVM

public CvSVM(Mat trainData,
             Mat responses,
             Mat varIdx,
             Mat sampleIdx,
             CvSVMParams params)

Default and training constructors.

The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.

Parameters:
trainData - a trainData
responses - a responses
varIdx - a varIdx
sampleIdx - a sampleIdx
params - a params
See Also:
org.opencv.ml.CvSVM.CvSVM
Method Detail

clear

public void clear()

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class CvStatModel
Throws:
java.lang.Throwable

get_support_vector_count

public int get_support_vector_count()

get_var_count

public int get_var_count()

Returns the number of used features (variables count).

See Also:
org.opencv.ml.CvSVM.get_var_count

predict_all

public void predict_all(Mat samples,
                        Mat results)

Predicts the response for input sample(s).

If you pass one sample then prediction result is returned. If you want to get responses for several samples then you should pass the results matrix where prediction results will be stored.

The function is parallelized with the TBB library.

Parameters:
samples - Input samples for prediction.
results - Output prediction responses for corresponding samples.
See Also:
org.opencv.ml.CvSVM.predict

predict

public float predict(Mat sample)

Predicts the response for input sample(s).

If you pass one sample then prediction result is returned. If you want to get responses for several samples then you should pass the results matrix where prediction results will be stored.

The function is parallelized with the TBB library.

Parameters:
sample - Input sample for prediction.
See Also:
org.opencv.ml.CvSVM.predict

predict

public float predict(Mat sample,
                     boolean returnDFVal)

Predicts the response for input sample(s).

If you pass one sample then prediction result is returned. If you want to get responses for several samples then you should pass the results matrix where prediction results will be stored.

The function is parallelized with the TBB library.

Parameters:
sample - Input sample for prediction.
returnDFVal - Specifies a type of the return value. If true and the problem is 2-class classification then the method returns the decision function value that is signed distance to the margin, else the function returns a class label (classification) or estimated function value (regression).
See Also:
org.opencv.ml.CvSVM.predict

train_auto

public boolean train_auto(Mat trainData,
                          Mat responses,
                          Mat varIdx,
                          Mat sampleIdx,
                          CvSVMParams params)

Trains an SVM with optimal parameters.

The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree from "CvSVMParams". Parameters are considered optimal when the cross-validation estimate of the test set error is minimal.

If there is no need to optimize a parameter, the corresponding grid step should be set to any value less than or equal to 1. For example, to avoid optimization in gamma, set gamma_grid.step = 0, gamma_grid.min_val, gamma_grid.max_val as arbitrary numbers. In this case, the value params.gamma is taken for gamma.

And, finally, if the optimization in a parameter is required but the corresponding grid is unknown, you may call the function "CvSVM.get_default_grid". To generate a grid, for example, for gamma, call CvSVM.get_default_grid(CvSVM.GAMMA).

This function works for the classification (params.svm_type=CvSVM.C_SVC or params.svm_type=CvSVM.NU_SVC) as well as for the regression (params.svm_type=CvSVM.EPS_SVR or params.svm_type=CvSVM.NU_SVR). If params.svm_type=CvSVM.ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.

Parameters:
trainData - a trainData
responses - a responses
varIdx - a varIdx
sampleIdx - a sampleIdx
params - a params
See Also:
org.opencv.ml.CvSVM.train_auto

train_auto

public boolean train_auto(Mat trainData,
                          Mat responses,
                          Mat varIdx,
                          Mat sampleIdx,
                          CvSVMParams params,
                          int k_fold,
                          CvParamGrid Cgrid,
                          CvParamGrid gammaGrid,
                          CvParamGrid pGrid,
                          CvParamGrid nuGrid,
                          CvParamGrid coeffGrid,
                          CvParamGrid degreeGrid,
                          boolean balanced)

Trains an SVM with optimal parameters.

The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree from "CvSVMParams". Parameters are considered optimal when the cross-validation estimate of the test set error is minimal.

If there is no need to optimize a parameter, the corresponding grid step should be set to any value less than or equal to 1. For example, to avoid optimization in gamma, set gamma_grid.step = 0, gamma_grid.min_val, gamma_grid.max_val as arbitrary numbers. In this case, the value params.gamma is taken for gamma.

And, finally, if the optimization in a parameter is required but the corresponding grid is unknown, you may call the function "CvSVM.get_default_grid". To generate a grid, for example, for gamma, call CvSVM.get_default_grid(CvSVM.GAMMA).

This function works for the classification (params.svm_type=CvSVM.C_SVC or params.svm_type=CvSVM.NU_SVC) as well as for the regression (params.svm_type=CvSVM.EPS_SVR or params.svm_type=CvSVM.NU_SVR). If params.svm_type=CvSVM.ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.

Parameters:
trainData - a trainData
responses - a responses
varIdx - a varIdx
sampleIdx - a sampleIdx
params - a params
k_fold - Cross-validation parameter. The training set is divided into k_fold subsets. One subset is used to test the model, the others form the train set. So, the SVM algorithm is executed k_fold times.
Cgrid - a Cgrid
gammaGrid - Iteration grid for the corresponding SVM parameter.
pGrid - Iteration grid for the corresponding SVM parameter.
nuGrid - Iteration grid for the corresponding SVM parameter.
coeffGrid - Iteration grid for the corresponding SVM parameter.
degreeGrid - Iteration grid for the corresponding SVM parameter.
balanced - If true and the problem is 2-class classification then the method creates more balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset.
See Also:
org.opencv.ml.CvSVM.train_auto

train

public boolean train(Mat trainData,
                     Mat responses)

Trains an SVM.

The method trains the SVM model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:

All the other parameters are gathered in the "CvSVMParams" structure.

Parameters:
trainData - a trainData
responses - a responses
See Also:
org.opencv.ml.CvSVM.train

train

public boolean train(Mat trainData,
                     Mat responses,
                     Mat varIdx,
                     Mat sampleIdx,
                     CvSVMParams params)

Trains an SVM.

The method trains the SVM model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:

All the other parameters are gathered in the "CvSVMParams" structure.

Parameters:
trainData - a trainData
responses - a responses
varIdx - a varIdx
sampleIdx - a sampleIdx
params - a params
See Also:
org.opencv.ml.CvSVM.train

Official OpenCV 2.4 Documentation