Table of Contents

Class EM

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

The class implements the Expectation Maximization algorithm.

public class EM : Algorithm, IDisposable, ICvPtrHolder
Inheritance
EM
Implements
Inherited Members

Constructors

EM(nint)

Creates instance by pointer cv::Ptr<EM>

protected EM(nint p)

Parameters

p nint

Fields

DEFAULT_MAX_ITERS

public const int DEFAULT_MAX_ITERS = 100

Field Value

int

DEFAULT_NCLUSTERS

public const int DEFAULT_NCLUSTERS = 5

Field Value

int

Properties

ClustersNumber

The number of mixture components in the Gaussian mixture model. Default value of the parameter is EM::DEFAULT_NCLUSTERS=5. Some of EM implementation could determine the optimal number of mixtures within a specified value range, but that is not the case in ML yet.

public int ClustersNumber { get; set; }

Property Value

int

CovarianceMatrixType

Constraint on covariance matrices which defines type of matrices.

public int CovarianceMatrixType { get; set; }

Property Value

int

TermCriteria

The termination criteria of the %EM algorithm. The EM algorithm can be terminated by the number of iterations termCrit.maxCount (number of M-steps) or when relative change of likelihood logarithm is less than termCrit.epsilon. Default maximum number of iterations is EM::DEFAULT_MAX_ITERS=100.

public TermCriteria TermCriteria { get; set; }

Property Value

TermCriteria

Methods

Create()

Creates empty EM model.

public static EM Create()

Returns

EM

DisposeManaged()

Releases managed resources

protected override void DisposeManaged()

GetCovs()

Returns covariation matrices. Returns vector of covariation matrices. Number of matrices is the number of gaussian mixtures, each matrix is a square floating-point matrix NxN, where N is the space dimensionality.

public Mat[] GetCovs()

Returns

Mat[]

GetMeans()

Returns the cluster centers (means of the Gaussian mixture). Returns matrix with the number of rows equal to the number of mixtures and number of columns equal to the space dimensionality.

public Mat GetMeans()

Returns

Mat

GetWeights()

Returns weights of the mixtures. Returns vector with the number of elements equal to the number of mixtures.

public Mat GetWeights()

Returns

Mat

Load(string)

Loads and creates a serialized model from a file.

public static EM Load(string filePath)

Parameters

filePath string

Returns

EM

LoadFromString(string)

Loads algorithm from a String.

public static EM LoadFromString(string strModel)

Parameters

strModel string

he string variable containing the model you want to load.

Returns

EM

Predict2(InputArray, OutputArray?)

Predicts the response for sample

public virtual Vec2d Predict2(InputArray sample, OutputArray? probs = null)

Parameters

sample InputArray

A sample for classification. It should be a one-channel matrix of \f$1 \times dims\f$ or \f$dims \times 1\f$ size.

probs OutputArray

Optional output matrix that contains posterior probabilities of each component given the sample. It has \f$1 \times nclusters\f$ size and CV_64FC1 type.

Returns

Vec2d

TrainE(InputArray, InputArray, InputArray?, InputArray?, OutputArray?, OutputArray?, OutputArray?)

Estimate the Gaussian mixture parameters from a samples set.

public virtual bool TrainE(InputArray samples, InputArray means0, InputArray? covs0 = null, InputArray? weights0 = null, OutputArray? logLikelihoods = null, OutputArray? labels = null, OutputArray? probs = null)

Parameters

samples InputArray

Samples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.

means0 InputArray

Initial means \f$a_k\f$ of mixture components. It is a one-channel matrix of \f$nclusters \times dims\f$ size. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.

covs0 InputArray

The vector of initial covariance matrices \f$S_k\f$ of mixture components. Each of covariance matrices is a one-channel matrix of \f$dims \times dims\f$ size. If the matrices do not have CV_64F type they will be converted to the inner matrices of such type for the further computing.

weights0 InputArray

Initial weights \f$\pi_k\f$ of mixture components. It should be a one-channel floating-point matrix with \f$1 \times nclusters\f$ or \f$nclusters \times 1\f$ size.

logLikelihoods OutputArray

The optional output matrix that contains a likelihood logarithm value for each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.

labels OutputArray

The optional output "class label" for each sample: \f$\texttt{labels}_i=\texttt{arg max}k(p{i,k}), i=1..N\f$ (indices of the most probable mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.

probs OutputArray

The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and CV_64FC1 type.

Returns

bool

TrainEM(InputArray, OutputArray?, OutputArray?, OutputArray?)

Estimate the Gaussian mixture parameters from a samples set.

public virtual bool TrainEM(InputArray samples, OutputArray? logLikelihoods = null, OutputArray? labels = null, OutputArray? probs = null)

Parameters

samples InputArray

Samples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.

logLikelihoods OutputArray

The optional output matrix that contains a likelihood logarithm value for each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.

labels OutputArray

The optional output "class label" for each sample: \f$\texttt{labels}_i=\texttt{arg max}k(p{i,k}), i=1..N\f$ (indices of the most probable mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.

probs OutputArray

The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and CV_64FC1 type.

Returns

bool

TrainM(InputArray, InputArray, OutputArray?, OutputArray?, OutputArray?)

Estimate the Gaussian mixture parameters from a samples set.

public virtual bool TrainM(InputArray samples, InputArray probs0, OutputArray? logLikelihoods = null, OutputArray? labels = null, OutputArray? probs = null)

Parameters

samples InputArray

Samples from which the Gaussian mixture model will be estimated. It should be a one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type it will be converted to the inner matrix of such type for the further computing.

probs0 InputArray

the probabilities

logLikelihoods OutputArray

The optional output matrix that contains a likelihood logarithm value for each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.

labels OutputArray

The optional output "class label" for each sample: \f$\texttt{labels}_i=\texttt{arg max}k(p{i,k}), i=1..N\f$ (indices of the most probable mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.

probs OutputArray

The optional output matrix that contains posterior probabilities of each Gaussian mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and CV_64FC1 type.

Returns

bool