Ask Your Question

Expectation Maximization: logarithm likelihood > 0

asked 2013-02-14 18:41:07 -0500

Seltymar gravatar image

updated 2013-02-18 03:08:03 -0500

I try to find the propability of a pixel from one image (ImageCurrent.tif, see at the end) to belongs of the foreground and background of another image (ImageFormer.tif).

Therefore, I create 2 GMMs (Gaussian Mixture Model) from one image with seperate foreground and background (using a mask). Then I used the predict function of the Expectation Maximization from OpenCV.

When I get back the logarithm likelihood of the predict function, I have sometimes values >0 which is for me strange as the probability should be between 0 and 1 (so the log likelihood should be <=0).

Is someone have experiment this kind of problem ? And how can I resolve it ?

Here is a test program showing the problem:

Mat mask = imread("mask.tiff", 0);
Mat formerImage = imread("ImageFormer.tiff");
Mat currentImage = imread("ImageCurrent.tiff");

// number of cluster in the GMM 
int nClusters = 5;

int countB=0, countF=0;

Vec3b color;

Vec2d probFg, probBg; // probabilities to belong to the foreground or background from GMMs

//count the number of pixels for each training data
for(int c=0; c<=40;c++) {
    for(int l=0; l<=40;l++) {
        if(<BYTE>(l, c)==255) {
        } else if(<BYTE>(l, c)==0) {
printf("countB %d countF %d \n", countB, countF);

Mat samplesForeground = Mat(countF,3, CV_64F);

Mat samplesBackground = Mat(countB,3, CV_64F);

// Expectation-Maximization able to resolve the GMM and to predict the probability for a pixel to belong to the GMM.
EM em_foreground= EM(nClusters);
EM em_background= EM(nClusters);


// fill the training data from the former image depending of the mask
for(int c=0; c<=40;c++) {
    for(int l=0; l<=40;l++) {
        if(<BYTE>(l, c)==255) {
            color =<Vec3b>(l, c);
        } else if(<BYTE>(l, c)==0) {
            color =<Vec3b>(l, c);
  <double>(countB, 0)=color[0];
  <double>(countB, 1)=color[1];
  <double>(countB, 2)=color[2];

printf("countB %d countF %d \n", countB, countF);

Mat sample(1, 3, CV_64F);

// try every pixel of the current image and get the log likelihood
for(int c=0; c<=40;c++) {
    for(int l=0; l<=40;l++) {
        color =<Vec3b>(l,c);<double>(0)=color[0];<double>(1)=color[1];<double>(2)=color[2];
        if(probFg[0]>0 || probBg[0]>0)
            printf("probFg[0] %f probBg[0] %f \n", probFg[0], probBg[0]);

I tried the program with the followings images:



mask.tiff :

EDIT: Is this can be a bug from OpenCV ? As I'm not sure if it is normal or not, I don't know if I should open a ticket.

edit retag flag offensive close merge delete


Did you ever discover the solution to this? I have the identical problem.

CalumMurray gravatar imageCalumMurray ( 2014-02-18 09:28:21 -0500 )edit

@CalumMurray I haven't discover any solution about that. I also post the question in stackoverflow But didn't get any success... If you find the solution, please post it, thank's.

Seltymar gravatar imageSeltymar ( 2014-09-22 02:38:12 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-05-22 23:46:22 -0500

Seltymar gravatar image

updated 2017-05-23 02:12:42 -0500

I have asked this question on stack overflow and has been answered :

Here is the copy of the answer given in stackoverflow:

As I understand you have two separate GMMs for the foreground and background part of the image.The total probability of a sample pixel 'x' in the test image when evaluated in the foreground GMM is

P_fg(x) = sum_over_j_1_to_k ( Wj_fg * Pj_fg( x ))
k = number of clusters in foreground GMM
x = test sample
Pj_fg(x) = probability that sample x is in j-th  cluster according to the foreground GMM
Wj_fg = weight of the j-th cluster in foreground GMM
also, sum of all weights should be 1 for each GMM.`

We can do a similar calculation for the background GMM.

From looking at the EM code in opencv, it looks like the first part of the 2 values that EM returns is the log likelihood. For the foreground GMM this is


I implemented your algorithm and for each pixel in the test image, I compared the log-likelihoods returned for each of the two GMM-s and classified the pixel with the GMM with higher value. I got decent results.

In that respect, yes this value is an indication of the pixel to be belonging to the entire GMM.

2) In my implementation of your problem, I always got the log likelihoods of all GMMS of all test-sample pixels under 0.

edit flag offensive delete link more


Maybe you could also summarize the Stack Overflow answer here to avoid switching on another page and in case the link is no more accessible?

Eduardo gravatar imageEduardo ( 2016-06-17 11:09:49 -0500 )edit

Question Tools



Asked: 2013-02-14 18:41:07 -0500

Seen: 1,186 times

Last updated: May 23 '17