Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenCV: Output of the predict function of Expectation Maximization
    primarykey
    data
    text
    <p><strong>Background:</strong> I have 2 sets of color pixels from an image, one corresponding to the background, another corresponding to the foreground. Next, I train 2 Gaussian Mixture Models using EM from OpenCV for each set. My aim is to find the probability of a random pixel to belong to the foreground and to the background. Thus, I use the function "predict" for each EM on my pixel.</p> <p><strong>Question:</strong> </p> <ul> <li>I don't understand the values returned by this function. In the documentation of OpenCV, it is written:</li> </ul> <blockquote> <p>The method returns a two-element double vector. Zero element is a likelihood logarithm value for the sample. First element is an index of the most probable mixture component for the given sample.</p> </blockquote> <p><a href="http://docs.opencv.org/modules/ml/doc/expectation_maximization.html?highlight=predict#Vec2d%20EM::predict%28InputArray%20sample,%20OutputArray%20probs%29%20const" rel="nofollow noreferrer">http://docs.opencv.org/modules/ml/doc/expectation_maximization.html?highlight=predict#Vec2d%20EM::predict%28InputArray%20sample,%20OutputArray%20probs%29%20const</a></p> <p>I don't understand what means "likehood logarithm". In my results, I have sometimes negative values and values > 1. Is anyone who used the same function has this kind of results or resuts between 0 and 1 ? What can I conclude from my results ?</p> <ul> <li>How can I get the probability of a pixel to belong to the whole GMM (not the probality to belong to each cluster of the GMM) ?</li> </ul> <p>Here is my code:</p> <pre><code>Mat mask = imread("mask.tif", 0); Mat formerImage = imread("ImageFormer.tif"); Mat currentImage = imread("ImageCurrent.tif"); // 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&lt;=40;c++) { for(int l=0; l&lt;=40;l++) { if(mask.at&lt;BYTE&gt;(l, c)==255) { countF++; } else if(mask.at&lt;BYTE&gt;(l, c)==0) { countB++; } } } printf("countB %d countF %d \n", countB, countF); Mat samplesForeground = Mat(countF,3, CV_64F); Mat samplesBackground = Mat(countB,3, CV_64F); // Expectation-Maximisation 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); countB=0; countF=0; // fill the training data from the former image depending of the mask for(int c=0; c&lt;=40;c++) { for(int l=0; l&lt;=40;l++) { if(mask.at&lt;BYTE&gt;(l, c)==255) { color = formerImage.at&lt;Vec3b&gt;(l, c); samplesForeground.at&lt;double&gt;(countF,0)=color[0]; samplesForeground.at&lt;double&gt;(countF,1)=color[1]; samplesForeground.at&lt;double&gt;(countF,2)=color[2]; countF++; } else if(mask.at&lt;BYTE&gt;(l, c)==0) { color = formerImage.at&lt;Vec3b&gt;(l, c); samplesBackground.at&lt;double&gt;(countB, 0)=color[0]; samplesBackground.at&lt;double&gt;(countB, 1)=color[1]; samplesBackground.at&lt;double&gt;(countB, 2)=color[2]; countB++; } } } printf("countB %d countF %d \n", countB, countF); em_foreground.train(samplesForeground); em_background.train(samplesBackground); Mat sample(1, 3, CV_64F); // try every pixel of the current image and get the log likelihood for(int c=0; c&lt;=40;c++) { for(int l=0; l&lt;=40;l++) { color = currentImage.at&lt;Vec3b&gt;(l,c); sample.at&lt;double&gt;(0)=color[0]; sample.at&lt;double&gt;(1)=color[1]; sample.at&lt;double&gt;(2)=color[2]; probFg=em_foreground.predict(sample); probBg=em_background.predict(sample); if(probFg[0]&gt;0 || probBg[0]&gt;0) printf("probFg[0] %f probBg[0] %f \n", probFg[0], probBg[0]); } } </code></pre> <p><strong>EDIT</strong></p> <p>After @BrianL explained, I now understand the log likelihood.</p> <p><strong>My problem is the log probability of the predict function is sometimes >0.</strong> But it should be &lt;=0. Has anyone met this problem before?</p> <p>I have edited the code above to show the problem. I have tried the program with images below:</p> <p>The first image is the ImageCurrent.tif, the second is the ImageFormer.tif and the last one is mask.tif.</p> <p><img src="https://i.stack.imgur.com/WH7We.png" alt="current image"> <img src="https://i.stack.imgur.com/FK3cp.png" alt="former image"> <img src="https://i.stack.imgur.com/jdDf8.png" alt="mask"></p> <p>Is this can be considered a bug in OpenCV? Should I open a ticket on OpenCV bug tracker?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload