C++ 熵改变每次执行时的值
以下是我在程序中使用的代码:C++ 熵改变每次执行时的值,c++,opencv,computer-vision,entropy,C++,Opencv,Computer Vision,Entropy,以下是我在程序中使用的代码: 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_i
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;
calcHist(&pre_img,1,channels,Mat(),//不要使用掩码
历史,1,历史大小,范围,
true,//直方图是一致的
假);
Mat histNorm=hist/(预模拟行*预模拟列);
双熵=0.0;
对于(int i=0;ilog2
仅在C99标准中定义。不使用log2
的解决方法可能是将其替换为不同的基数,因为对数logb(x)
可以使用以下公式从任意基k
的x
和b
的对数计算得出:
所以你可以替换
if (binEntry != 0.0)
{
entropy -= binEntry * log2(binEntry);
}
与
你看到了什么错误?@AlanStokes标识符log2没有定义
你有#include
吗?@AlanStokes是的,我正在使用它log2
仅在C99标准中定义。请注意你没有明白我的问题,我说我在声明log2
甚至log(2)时有错误
,您的代码仍然显示错误。您可以使用其他数学函数吗?我正在使用三个库,#include#include#include
您可以使用math.h中的任何其他函数吗,即sin?请尝试log(2.0)
,因为参数应该是float
的double
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
}