This section describes obsolete C
interface of EM algorithm. Details of the algorithm and its C++
interface can be found in the other section Expectation Maximization.
Note
CvEMParams
¶Parameters of the EM algorithm. All parameters are public. You can initialize them by a constructor and then override some of them directly if you want.
The constructors
CvEMParams::
CvEMParams
()¶
CvEMParams::
CvEMParams
(int nclusters, int cov_mat_type=EM::COV_MAT_DIAGONAL, int start_step=EM::START_AUTO_STEP, CvTermCriteria term_crit=cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, FLT_EPSILON), const CvMat* probs=0, const CvMat* weights=0, const CvMat* means=0, const CvMat** covs=0 )¶Parameters: 


The default constructor represents a rough ruleofthethumb:
CvEMParams() : nclusters(10), cov_mat_type(1/*CvEM::COV_MAT_DIAGONAL*/),
start_step(0/*CvEM::START_AUTO_STEP*/), probs(0), weights(0), means(0), covs(0)
{
term_crit=cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, FLT_EPSILON );
}
With another constructor it is possible to override a variety of parameters from a single number of mixtures (the only essential problemdependent parameter) to initial values for the mixture parameters.
CvEM
: public CvStatModel
¶The class implements the EM algorithm as described in the beginning of the section Expectation Maximization.
Estimates the Gaussian mixture parameters from a sample set.
bool CvEM::
train
(const Mat& samples, const Mat& sampleIdx=Mat(), CvEMParams params=CvEMParams(), Mat* labels=0 )¶
bool CvEM::
train
(const CvMat* samples, const CvMat* sampleIdx=0, CvEMParams params=CvEMParams(), CvMat* labels=0 )¶Parameters: 


Unlike many of the ML models, EM is an unsupervised learning algorithm and it does not take responses (class labels or function values) as input. Instead, it computes the
Maximum Likelihood Estimate of the Gaussian mixture parameters from an input sample set, stores all the parameters inside the structure:
in probs
,
in means
,
in covs[k]
,
in weights
, and optionally computes the output “class label” for each sample:
(indices of the most probable mixture component for each sample).
The trained model can be used further for prediction, just like any other classifier. The trained model is similar to the
CvNormalBayesClassifier
.
For an example of clustering random samples of the multiGaussian distribution using EM, see em.cpp
sample in the OpenCV distribution.
Returns a mixture component index of a sample.
float CvEM::
predict
(const Mat& sample, Mat* probs=0 ) const
¶
float CvEM::
predict
(const CvMat* sample, CvMat* probs) const
¶Parameters: 


Returns the number of mixture components in the Gaussian mixture model.
int CvEM::
getNClusters
() const
¶
int CvEM::
get_nclusters
() const
¶Returns mixture means .
Mat CvEM::
getMeans
() const
¶
const CvMat* CvEM::
get_means
() const
¶Returns mixture covariance matrices .
void CvEM::
getCovs
(std::vector<cv::Mat>& covs) const
¶
const CvMat** CvEM::
get_covs
() const
¶Returns mixture weights .
Mat CvEM::
getWeights
() const
¶
const CvMat* CvEM::
get_weights
() const
¶Returns vectors of probabilities for each training sample.
Mat CvEM::
getProbs
() const
¶
const CvMat* CvEM::
get_probs
() const
¶For each training sample (that have been passed to the constructor or to CvEM::train()
) returns probabilities to belong to a mixture component .
Returns logarithm of likelihood.
double CvEM::
getLikelihood
() const
¶
double CvEM::
get_log_likelihood
() const
¶Writes the trained Gaussian mixture model to the file storage.
void CvEM::
write
(CvFileStorage* fs, const char* name) const
¶Parameters: 


Reads the trained Gaussian mixture model from the file storage.
void CvEM::
read
(CvFileStorage* fs, CvFileNode* node)¶Parameters: 

