2013-09-22 39 views
0

這裏就是我用我的程序代碼:熵變的值在每次執行

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; 

的第一件事就是當過我進入它像entropy -= binEntry * log2(binEntry);它給我的錯誤上log2,我增加了庫的數學和數字在VS 2010中,但仍然得到的錯誤,並在代碼中的第二點是每當我運行它在同一個視頻它給我不同的值在每次執行,如果它給我10.0 , 2.0 , 0.05比同一幀上下次運行程序給我看8.0 , 1.5 , 0.01these are sample values not exact

+0

你看到了什麼錯誤? –

+0

@AlanStokes標識符log2未定義# – Pieter

+0

你有'#include '嗎? –

回答

1

log2僅在C99標準中定義。一種用於不使用log2解決方法可能是具有不同鹼來代替它,因爲logb(x)可以從xb對數使用以下公式來計算關於一個任意鹼基k對數:

enter image description here

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

,所以你可以替換

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

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 
      } 
+0

你沒有得到我的問題,我說我在聲明'log2'甚至是'log(2)'時有錯誤,你的代碼仍然顯示錯誤 – Pieter

+0

你可以使用其他數學函數嗎? – 4pie0

+0

我爲它使用了三個庫,'#include #include #include ' – Pieter