Skip to main content
Clustering algorithms group data points into clusters based on their similarity without requiring labeled training data.

kmeans - K-Means Clustering

The k-means algorithm finds centers of clusters and groups input samples around the clusters. It’s one of the most popular clustering algorithms.

Function Signature

Parameters

data
InputArray
required
Data for clustering. An array of N-dimensional points with float coordinates.Examples:
  • Mat points(count, 2, CV_32F) - 2D points as rows
  • Mat points(count, 1, CV_32FC2) - 2D points as single channel
  • Mat points(1, count, CV_32FC2) - 2D points as columns
  • std::vector<cv::Point2f> points(sampleCount) - vector of points
K
int
required
Number of clusters to split the set by. Must be at least 2.
bestLabels
InputOutputArray
required
Input/output integer array that stores the cluster indices for every sample.Each element is in the range [0, K-1] indicating which cluster the sample belongs to.
criteria
TermCriteria
required
The algorithm termination criteria: maximum number of iterations and/or desired accuracy.The accuracy is specified as criteria.epsilon. The algorithm stops when each cluster center moves by less than epsilon.Example: TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 100, 0.01)
attempts
int
required
Number of times the algorithm is executed using different initial labellings.The algorithm returns the labels that yield the best compactness. Use at least 3 attempts for better results.
flags
int
required
Flag specifying the method for center initialization.Flags:
  • KMEANS_RANDOM_CENTERS (0): Select random initial centers in each attempt
  • KMEANS_PP_CENTERS (2): Use kmeans++ center initialization (recommended)
  • KMEANS_USE_INITIAL_LABELS (1): Use user-supplied labels for first attempt
centers
OutputArray
Output matrix of the cluster centers, one row per each cluster center.Size: K × dimensions

Returns

Type: double The function returns the compactness measure computed as: isamplesicenterslabelsi2\sum_i \|\text{samples}_i - \text{centers}_{\text{labels}_i}\|^2 The best (minimum) compactness value is chosen among all attempts, and the corresponding labels and cluster centers are returned.

Example Usage

For best results, use KMEANS_PP_CENTERS flag which implements the kmeans++ initialization algorithm by Arthur and Vassilvitskii. This provides better initial centers than random selection.

EM - Expectation Maximization

The Expectation Maximization algorithm implements Gaussian Mixture Models (GMM) for clustering. It models data as a mixture of multiple Gaussian distributions.

Creating an EM Model

Key Methods

setClustersNumber
void
Sets the number of mixture componentsParameters:
  • val (int): Number of clusters/mixtures (default: 5)
setCovarianceMatrixType
void
Sets the constraint on covariance matricesParameters:
  • val (int): Type of covariance matrices
Types:
  • EM::COV_MAT_SPHERICAL (0): Scaled identity matrix μ_k * I
  • EM::COV_MAT_DIAGONAL (1): Diagonal matrix with positive diagonal elements (recommended)
  • EM::COV_MAT_GENERIC (2): Symmetric positive definite matrix
setTermCriteria
void
Sets the termination criteria of the EM algorithmParameters:
  • val (TermCriteria): Criteria for max iterations or likelihood change
The EM algorithm terminates when:
  • Maximum number of iterations (M-steps) is reached, OR
  • Relative change of likelihood logarithm is less than epsilon
Default: TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 100, epsilon)
trainEM
bool
Estimates Gaussian mixture parameters from a sample set (Expectation step start)Parameters:
  • samples (InputArray): Samples from which GMM will be estimated (CV_64F or will be converted)
  • logLikelihoods (OutputArray): Optional output matrix of likelihood logarithm values
  • labels (OutputArray): Optional output “class label” for each sample
  • probs (OutputArray): Optional posterior probabilities matrix (nsamples × nclusters)
Returns: bool - true if training succeededThis variation starts with Expectation step. Initial values are estimated by k-means algorithm.
trainE
bool
Estimates Gaussian mixture parameters with initial means providedParameters:
  • samples (InputArray): Training samples matrix
  • means0 (InputArray): Initial means of mixture components (nclusters × dims)
  • covs0 (InputArray): Optional initial covariance matrices
  • weights0 (InputArray): Optional initial weights of mixture components
  • logLikelihoods (OutputArray): Optional likelihood logarithm output
  • labels (OutputArray): Optional cluster labels output
  • probs (OutputArray): Optional posterior probabilities output
Returns: bool - true if training succeeded
trainM
bool
Estimates Gaussian mixture parameters starting with Maximization stepParameters:
  • samples (InputArray): Training samples
  • probs0 (InputArray): Initial probabilities
  • logLikelihoods (OutputArray): Optional likelihood output
  • labels (OutputArray): Optional labels output
  • probs (OutputArray): Optional probabilities output
Returns: bool - true if training succeeded
predict
float
Returns posterior probabilities for the provided samplesParameters:
  • samples (InputArray): Input samples matrix
  • results (OutputArray): Optional output matrix of results (nsamples × nclusters)
  • flags (int): Optional flags (ignored)
Returns: float - predicted class for single sample
predict2
Vec2d
Returns likelihood logarithm value and index of most probable mixture componentParameters:
  • sample (InputArray): A sample for classification (1 × dims or dims × 1)
  • probs (OutputArray): Optional posterior probabilities (1 × nclusters, CV_64FC1)
Returns: Vec2d
  • Element [0]: Likelihood logarithm value
  • Element [1]: Index of most probable mixture component
getWeights
Mat
Returns weights of the mixturesReturns: Mat - vector with number of elements equal to number of mixtures
getMeans
Mat
Returns the cluster centers (means of the Gaussian mixture)Returns: Mat - matrix with rows = number of mixtures, cols = space dimensionality
getCovs
void
Returns covariance matricesParameters:
  • covs (std::vector<Mat>&): Output vector of covariance matrices
Returns vector of covariation matrices (one per mixture, each is NxN where N is dimensionality)

Example Usage

When to Use EM vs K-Means

Use EM when:
  • Clusters have different shapes and sizes
  • You need probabilistic cluster assignments
  • Data follows Gaussian distributions
  • You want to model uncertainty in cluster membership
Use K-Means when:
  • Clusters are roughly spherical and similar in size
  • You need hard cluster assignments
  • Speed is critical (K-means is faster)
  • You have very large datasets
The EM algorithm is more flexible than k-means as it can model elliptical clusters with different orientations and sizes. However, it’s more computationally expensive and requires more samples for reliable estimation.

See Also