Skip to main content
The OpenCV Machine Learning module provides several powerful classification algorithms for supervised learning tasks.

SVM - Support Vector Machines

Support Vector Machines are powerful classifiers that work well for both linear and non-linear classification problems.

Creating an SVM Model

Ptr<SVM> svm = SVM::create();

Key Methods

setType
void
Sets the type of SVM formulationParameters:
  • val (int): SVM type. See SVM::Types
Types:
  • SVM::C_SVC (100): C-Support Vector Classification for n-class classification
  • SVM::NU_SVC (101): ν-Support Vector Classification with parameter ν
  • SVM::ONE_CLASS (102): Distribution Estimation (One-class SVM)
  • SVM::EPS_SVR (103): ε-Support Vector Regression
  • SVM::NU_SVR (104): ν-Support Vector Regression
setKernel
void
Initialize with one of predefined kernelsParameters:
  • kernelType (int): Kernel type. See SVM::KernelTypes
Kernel Types:
  • SVM::LINEAR (0): Linear kernel, no mapping done
  • SVM::POLY (1): Polynomial kernel
  • SVM::RBF (2): Radial basis function, good choice in most cases
  • SVM::SIGMOID (3): Sigmoid kernel
  • SVM::CHI2 (4): Exponential Chi2 kernel
  • SVM::INTER (5): Histogram intersection kernel
setC
void
Sets parameter C of a SVM optimization problemParameters:
  • val (double): Parameter C for C_SVC, EPS_SVR or NU_SVR
setGamma
void
Sets parameter γ of a kernel functionParameters:
  • val (double): Parameter gamma for POLY, RBF, SIGMOID or CHI2 kernels
train
bool
Trains the SVM modelParameters:
  • trainData (Ptr<TrainData>): Training data
  • flags (int): Optional flags
Returns: bool - true if training succeeded
predict
float
Predicts response for input samplesParameters:
  • samples (InputArray): Input samples, floating-point matrix
  • results (OutputArray): Optional output matrix of results
  • flags (int): Optional flags
Returns: float - predicted response for single sample
trainAuto
bool
Trains an SVM with optimal parameters using cross-validationParameters:
  • data (Ptr<TrainData>): Training data
  • kFold (int): Cross-validation parameter (default: 10)
  • Cgrid (ParamGrid): Grid for C parameter
  • gammaGrid (ParamGrid): Grid for gamma parameter
  • pGrid (ParamGrid): Grid for p parameter
  • nuGrid (ParamGrid): Grid for nu parameter
  • coeffGrid (ParamGrid): Grid for coeff parameter
  • degreeGrid (ParamGrid): Grid for degree parameter
  • balanced (bool): Create balanced cross-validation subsets
Returns: bool - true if training succeeded
getSupportVectors
Mat
Retrieves all the support vectorsReturns: Mat - matrix where support vectors are stored as rows

Example Usage

#include <opencv2/ml.hpp>

using namespace cv::ml;

// Create and configure SVM
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::RBF);
svm->setGamma(0.5);
svm->setC(1.0);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

// Train the SVM
Ptr<TrainData> trainData = TrainData::create(samples, ROW_SAMPLE, labels);
svm->train(trainData);

// Predict
Mat results;
svm->predict(testSamples, results);

KNearest - K-Nearest Neighbors

The K-Nearest Neighbors algorithm finds the k nearest neighbors and predicts the response based on their values.

Creating a KNearest Model

Ptr<KNearest> knn = KNearest::create();

Key Methods

setDefaultK
void
Sets the default number of neighbors to use in predict methodParameters:
  • val (int): Number of neighbors (must be greater than 1)
setIsClassifier
void
Sets whether classification or regression model should be trainedParameters:
  • val (bool): true for classification, false for regression
setAlgorithmType
void
Sets the algorithm typeParameters:
  • val (int): Algorithm type
Types:
  • KNearest::BRUTE_FORCE (1): Brute force search
  • KNearest::KDTREE (2): KD-tree based search
findNearest
float
Finds the neighbors and predicts responses for input vectorsParameters:
  • samples (InputArray): Input samples (rows are samples)
  • k (int): Number of nearest neighbors
  • results (OutputArray): Vector with prediction results
  • neighborResponses (OutputArray): Optional output for neighbor responses
  • dist (OutputArray): Optional output distances to neighbors
Returns: float - predicted value for single input vector

Example Usage

#include <opencv2/ml.hpp>

using namespace cv::ml;

// Create KNN classifier
Ptr<KNearest> knn = KNearest::create();
knn->setDefaultK(5);
knn->setIsClassifier(true);
knn->setAlgorithmType(KNearest::BRUTE_FORCE);

// Train
knn->train(trainSamples, ROW_SAMPLE, trainLabels);

// Predict
Mat results, neighbors, distances;
knn->findNearest(testSamples, 5, results, neighbors, distances);

DTrees - Decision Trees

Decision trees are tree-based classifiers that split data based on feature values.

Creating a Decision Tree Model

Ptr<DTrees> dtree = DTrees::create();

Key Methods

setMaxDepth
void
Sets the maximum possible depth of the treeParameters:
  • val (int): Maximum depth (root node has depth 0)
setMinSampleCount
void
Sets the minimum number of samples required to split a nodeParameters:
  • val (int): Minimum sample count (default: 10)
setMaxCategories
void
Sets the maximum number of categories for clusteringParameters:
  • val (int): Maximum categories (default: 10)
setCVFolds
void
Sets the number of folds for cross-validation pruningParameters:
  • val (int): Number of folds (default: 10)
setPriors
void
Sets a priori class probabilitiesParameters:
  • val (Mat): Array of class probabilities sorted by label

Example Usage

// Create and configure decision tree
Ptr<DTrees> dtree = DTrees::create();
dtree->setMaxDepth(10);
dtree->setMinSampleCount(10);
dtree->setCVFolds(10);

// Train
dtree->train(trainData);

// Predict
float response = dtree->predict(testSample);

Boost - Boosted Trees

Boosted tree classifier that combines multiple weak classifiers into a strong one.

Creating a Boost Model

Ptr<Boost> boost = Boost::create();

Key Methods

setBoostType
void
Sets the type of boosting algorithmParameters:
  • val (int): Boost type
Types:
  • Boost::DISCRETE (0): Discrete AdaBoost
  • Boost::REAL (1): Real AdaBoost (default, works well with categorical data)
  • Boost::LOGIT (2): LogitBoost (good for regression)
  • Boost::GENTLE (3): Gentle AdaBoost (good with regression data)
setWeakCount
void
Sets the number of weak classifiersParameters:
  • val (int): Number of weak classifiers (default: 100)
setWeightTrimRate
void
Sets the threshold for computational time savingsParameters:
  • val (double): Weight trim rate between 0 and 1 (default: 0.95)

Example Usage

// Create and configure Boost
Ptr<Boost> boost = Boost::create();
boost->setBoostType(Boost::REAL);
boost->setWeakCount(100);
boost->setWeightTrimRate(0.95);
boost->setMaxDepth(1);

// Train
boost->train(trainData);

// Predict
float result = boost->predict(testSample);

RTrees - Random Trees (Random Forest)

Random Forest is an ensemble learning method that constructs multiple decision trees.

Creating a Random Trees Model

Ptr<RTrees> rtrees = RTrees::create();

Key Methods

setCalculateVarImportance
void
Enable/disable variable importance calculationParameters:
  • val (bool): true to calculate variable importance
setActiveVarCount
void
Sets the size of randomly selected subset of features at each tree nodeParameters:
  • val (int): Number of active variables (0 = sqrt of total features)
setTermCriteria
void
Sets termination criteria for trainingParameters:
  • val (TermCriteria): Criteria specifying max iterations or accuracy
getVarImportance
Mat
Returns the variable importance arrayReturns: Mat - variable importance vector (if enabled during training)
getVotes
void
Returns the result of each individual tree in the forestParameters:
  • samples (InputArray): Samples for which votes will be calculated
  • results (OutputArray): Matrix where results will be written
  • flags (int): Flags for defining the type of RTrees

Example Usage

// Create and configure Random Forest
Ptr<RTrees> rtrees = RTrees::create();
rtrees->setMaxDepth(10);
rtrees->setMinSampleCount(10);
rtrees->setCalculateVarImportance(true);
rtrees->setActiveVarCount(0); // sqrt of total features
rtrees->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 0));

// Train
rtrees->train(trainData);

// Get variable importance
Mat varImportance = rtrees->getVarImportance();

// Predict
float result = rtrees->predict(testSample);

NormalBayesClassifier - Naive Bayes

Bayes classifier for normally distributed data using Bayesian statistics.

Creating a Naive Bayes Model

Ptr<NormalBayesClassifier> bayes = NormalBayesClassifier::create();

Key Methods

train
bool
Trains the Bayes classifierParameters:
  • trainData (Ptr<TrainData>): Training data
  • flags (int): Optional flags
Returns: bool - true if training succeeded
predict
float
Predicts response for input samplesParameters:
  • samples (InputArray): Input samples
  • results (OutputArray): Output predictions
  • flags (int): Optional flags
Returns: float - predicted class for single sample
predictProb
float
Predicts the response and returns probabilitiesParameters:
  • inputs (InputArray): Input vectors (one or more)
  • outputs (OutputArray): Predicted classes
  • outputProbs (OutputArray): Output probabilities for each class
  • flags (int): Optional flags
Returns: float - predicted class for single input

Example Usage

// Create Naive Bayes classifier
Ptr<NormalBayesClassifier> bayes = NormalBayesClassifier::create();

// Train
bayes->train(trainData);

// Predict with probabilities
Mat results, probs;
bayes->predictProb(testSamples, results, probs);
The Naive Bayes classifier assumes that features are normally distributed and independent. It works best when these assumptions hold true.

See Also

Build docs developers (and LLMs) love