0

Here is code which i am using in my program :

calcHist( &pre_img, 1, channels, Mat(), // do not use mask
                 hist, 1, histSize, ranges,
                 true, // the histogram is uniform
                 false );

       Mat histNorm = hist / (pre_img.rows * pre_img.cols);
       double entropy = 0.0;
       for (int i=0; i<histNorm.rows; i++)
       {
          float binEntry = histNorm.at<float>(i,0);
          if (binEntry != 0.0)
          {
            entropy -= binEntry * log(binEntry);
          }
       }
       cout<<entropy<<endl;

The first thing is when ever i enter it like entropy -= binEntry * log2(binEntry); it give me error on log2 , i added the libraries of math and numeric in VS 2010 , but still getting the error and the second point in the code is whenever i run it on the same video it give me different values on every execution , like if it give me 10.0 , 2.0 , 0.05 than the same frame on the next time running the program show me 8.0 , 1.5 , 0.01 these are sample values not exact

Pieter
  • 161
  • 1
  • 1
  • 10

1 Answers1

1

log2 is only defined in the C99 standard. A workaround for not using log2 might be to replace it with different base because the logarithm logb(x) can be computed from the logarithms of x and b with respect to an arbitrary base k using the following formula:

enter image description here

https://math.stackexchange.com/a/131719/29621

so you can replace

if (binEntry != 0.0)
          {
            entropy -= binEntry * log2(binEntry);
          }

with

if (binEntry != 0.0)
          {
            entropy -= binEntry * log(binEntry)/log(2.0);
                                                     ^
                                                also you should use `log(2.0)`
                                                because the argument should be
                                                double or float
          }
Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118