Package org.opencv.ml
Class SVM
- java.lang.Object
-
- org.opencv.core.Algorithm
-
- org.opencv.ml.StatModel
-
- org.opencv.ml.SVM
-
public class SVM extends StatModel
Support Vector Machines. SEE: REF: ml_intro_svm
-
-
Field Summary
Fields Modifier and Type Field Description static int
C
static int
C_SVC
static int
CHI2
static int
COEF
static int
CUSTOM
static int
DEGREE
static int
EPS_SVR
static int
GAMMA
static int
INTER
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.StatModel
COMPRESSED_INPUT, PREPROCESSED_INPUT, RAW_OUTPUT, UPDATE_MODEL
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SVM(long addr)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SVM
__fromPtr__(long addr)
static SVM
create()
Creates empty model.protected void
finalize()
double
getC()
SEE: setCMat
getClassWeights()
SEE: setClassWeightsdouble
getCoef0()
SEE: setCoef0double
getDecisionFunction(int i, Mat alpha, Mat svidx)
Retrieves the decision functionstatic ParamGrid
getDefaultGridPtr(int param_id)
Generates a grid for %SVM parameters.double
getDegree()
SEE: setDegreedouble
getGamma()
SEE: setGammaint
getKernelType()
Type of a %SVM kernel.double
getNu()
SEE: setNudouble
getP()
SEE: setPMat
getSupportVectors()
Retrieves all the support vectors The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows.TermCriteria
getTermCriteria()
SEE: setTermCriteriaint
getType()
SEE: setTypeMat
getUncompressedSupportVectors()
Retrieves all the uncompressed support vectors of a linear %SVM The method returns all the uncompressed support vectors of a linear %SVM that the compressed support vector, used for prediction, was derived from.static SVM
load(java.lang.String filepath)
Loads and creates a serialized svm from a file Use SVM::save to serialize and store an SVM to disk.void
setC(double val)
getC SEE: getCvoid
setClassWeights(Mat val)
getClassWeights SEE: getClassWeightsvoid
setCoef0(double val)
getCoef0 SEE: getCoef0void
setDegree(double val)
getDegree SEE: getDegreevoid
setGamma(double val)
getGamma SEE: getGammavoid
setKernel(int kernelType)
Initialize with one of predefined kernels.void
setNu(double val)
getNu SEE: getNuvoid
setP(double val)
getP SEE: getPvoid
setTermCriteria(TermCriteria val)
getTermCriteria SEE: getTermCriteriavoid
setType(int val)
getType SEE: getTypeboolean
trainAuto(Mat samples, int layout, Mat responses)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid, ParamGrid degreeGrid)
Trains an %SVM with optimal parametersboolean
trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid, ParamGrid degreeGrid, boolean balanced)
Trains an %SVM with optimal parameters-
Methods inherited from class org.opencv.ml.StatModel
calcError, empty, getVarCount, isClassifier, isTrained, predict, predict, predict, train, train, train
-
Methods inherited from class org.opencv.core.Algorithm
clear, getDefaultName, getNativeObjAddr, save
-
-
-
-
Field Detail
-
C
public static final int C
- See Also:
- Constant Field Values
-
GAMMA
public static final int GAMMA
- See Also:
- Constant Field Values
-
P
public static final int P
- See Also:
- Constant Field Values
-
NU
public static final int NU
- 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
-
C_SVC
public static final int C_SVC
- See Also:
- Constant Field Values
-
NU_SVC
public static final int NU_SVC
- See Also:
- Constant Field Values
-
ONE_CLASS
public static final int ONE_CLASS
- See Also:
- Constant Field Values
-
EPS_SVR
public static final int EPS_SVR
- See Also:
- Constant Field Values
-
NU_SVR
public static final int NU_SVR
- See Also:
- Constant Field Values
-
CUSTOM
public static final int CUSTOM
- See Also:
- Constant Field Values
-
LINEAR
public static final int LINEAR
- 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
-
CHI2
public static final int CHI2
- See Also:
- Constant Field Values
-
INTER
public static final int INTER
- See Also:
- Constant Field Values
-
-
Method Detail
-
__fromPtr__
public static SVM __fromPtr__(long addr)
-
getClassWeights
public Mat getClassWeights()
SEE: setClassWeights- Returns:
- automatically generated
-
getSupportVectors
public Mat getSupportVectors()
Retrieves all the support vectors The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows.- Returns:
- automatically generated
-
getUncompressedSupportVectors
public Mat getUncompressedSupportVectors()
Retrieves all the uncompressed support vectors of a linear %SVM The method returns all the uncompressed support vectors of a linear %SVM that the compressed support vector, used for prediction, was derived from. They are returned in a floating-point matrix, where the support vectors are stored as matrix rows.- Returns:
- automatically generated
-
getDefaultGridPtr
public static ParamGrid getDefaultGridPtr(int param_id)
Generates a grid for %SVM parameters.- Parameters:
param_id
- %SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID. The function generates a grid pointer for the specified parameter of the %SVM algorithm. The grid may be passed to the function SVM::trainAuto.- Returns:
- automatically generated
-
create
public static SVM create()
Creates empty model. Use StatModel::train to train the model. Since %SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto.- Returns:
- automatically generated
-
load
public static SVM load(java.lang.String filepath)
Loads and creates a serialized svm from a file Use SVM::save to serialize and store an SVM to disk. Load the SVM from this file again, by calling this function with the path to the file.- Parameters:
filepath
- path to serialized svm- Returns:
- automatically generated
-
getTermCriteria
public TermCriteria getTermCriteria()
SEE: setTermCriteria- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid, ParamGrid degreeGrid, boolean balanced)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nucoeffGrid
- grid for coeffdegreeGrid
- grid for degreebalanced
- 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. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid, ParamGrid degreeGrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nucoeffGrid
- grid for coeffdegreeGrid
- grid for degree balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nucoeffGrid
- grid for coeff balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nu balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid, ParamGrid pGrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for p balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid, ParamGrid gammaGrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for CgammaGrid
- grid for gamma balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm isCgrid
- grid for C balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses, int kFold)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.kFold
- Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the %SVM algorithm is balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
trainAuto
public boolean trainAuto(Mat samples, int layout, Mat responses)
Trains an %SVM with optimal parameters- Parameters:
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples. subset is used to test the model, the others form the train set. So, the %SVM algorithm is balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset. The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal. This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options. This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual %SVM with parameters specified in params is executed.- Returns:
- automatically generated
-
getC
public double getC()
SEE: setC- Returns:
- automatically generated
-
getCoef0
public double getCoef0()
SEE: setCoef0- Returns:
- automatically generated
-
getDecisionFunction
public double getDecisionFunction(int i, Mat alpha, Mat svidx)
Retrieves the decision function- Parameters:
i
- the index of the decision function. If the problem solved is regression, 1-class or 2-class classification, then there will be just one decision function and the index should always be 0. Otherwise, in the case of N-class classification, there will be \(N(N-1)/2\) decision functions.alpha
- the optional output vector for weights, corresponding to different support vectors. In the case of linear %SVM all the alpha's will be 1's.svidx
- the optional output vector of indices of support vectors within the matrix of support vectors (which can be retrieved by SVM::getSupportVectors). In the case of linear %SVM each decision function consists of a single "compressed" support vector. The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.- Returns:
- automatically generated
-
getDegree
public double getDegree()
SEE: setDegree- Returns:
- automatically generated
-
getGamma
public double getGamma()
SEE: setGamma- Returns:
- automatically generated
-
getNu
public double getNu()
SEE: setNu- Returns:
- automatically generated
-
getP
public double getP()
SEE: setP- Returns:
- automatically generated
-
getKernelType
public int getKernelType()
Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.- Returns:
- automatically generated
-
getType
public int getType()
SEE: setType- Returns:
- automatically generated
-
setC
public void setC(double val)
getC SEE: getC- Parameters:
val
- automatically generated
-
setClassWeights
public void setClassWeights(Mat val)
getClassWeights SEE: getClassWeights- Parameters:
val
- automatically generated
-
setCoef0
public void setCoef0(double val)
getCoef0 SEE: getCoef0- Parameters:
val
- automatically generated
-
setDegree
public void setDegree(double val)
getDegree SEE: getDegree- Parameters:
val
- automatically generated
-
setGamma
public void setGamma(double val)
getGamma SEE: getGamma- Parameters:
val
- automatically generated
-
setKernel
public void setKernel(int kernelType)
Initialize with one of predefined kernels. See SVM::KernelTypes.- Parameters:
kernelType
- automatically generated
-
setNu
public void setNu(double val)
getNu SEE: getNu- Parameters:
val
- automatically generated
-
setP
public void setP(double val)
getP SEE: getP- Parameters:
val
- automatically generated
-
setTermCriteria
public void setTermCriteria(TermCriteria val)
getTermCriteria SEE: getTermCriteria- Parameters:
val
- automatically generated
-
setType
public void setType(int val)
getType SEE: getType- Parameters:
val
- automatically generated
-
-