opencv中的灰色图像的熵

时间:2014-07-24 09:35:38

标签: c++ opencv information-theory

我需要代码来查找图像的熵。

for(int i=0;i<grey_image.rows;i++)
{
    for(int j=1;j<grey_image.cols;j++)
    {
        //cout<<i<<" "<<j<<" "<<(int)grey_image.at<uchar>(i,j)<<endl;
        int a=(int)grey_image.at<uchar>(i,j);
        int b=(int)grey_image.at<uchar>(i,j-1);
        int x=a-b;
        if(x<0)
            x=0-x;
        probability_array[x]++;
        //grey_image.at<uchar>(i,j) = 255;
    }
}
//calculating probability
int n=rows*cols;
for(int i=0;i<256;i++)
{
    probability_array[i]/=n;
    //cout<<probability_array[i]<<endl;
}
// galeleo team formula
float entropy=0;
for(int i=0;i<256;i++)
{
    if (probability_array[i]>0)
    {
        float x=probability_array[i]*log(probability_array[i]);
        entropy+=x;
    }
}
return 0-entropy;

实际上我正在使用它来转储可编程相机来测量熵。现在我想在windows系统中使用它。我得到一个灰色图像的熵为零。请帮帮我。我哪里出错了。

3 个答案:

答案 0 :(得分:11)

在不知道你使用的是什么图像的情况下,我们无法知道零熵结果是不是正确答案(如@Xocoatzin所建议的)。 此外,您的代码可以从一些最新的OpenCV功能中受益:这是一个使用OpenCV直方图和矩阵表达式的工作实现:

    if (frame.channels()==3) cvtColor(frame,frame,CV_BGR2GRAY);
    /// Establish the number of bins
    int histSize = 256;
    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 } ;
    const float* histRange = { range };
    bool uniform = true; bool accumulate = false;
    /// Compute the histograms:
    calcHist( &frame, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
    hist /= frame.total();
    hist += 1e-4; //prevent 0

    Mat logP;
    cv::log(hist,logP);

    float entropy = -1*sum(hist.mul(logP)).val[0];

    cout << entropy << endl;

答案 1 :(得分:3)

这是我正在使用的,希望它有所帮助; https://github.com/samidalati/OpenCV-Entropy你可以找到几种使用OpenCV来计算彩色和灰度图像熵的方法

 float entropy(Mat seq, Size size, int index)
{
  int cnt = 0;
  float entr = 0;
  float total_size = size.height * size.width; //total size of all symbols in an image

  for(int i=0;i<index;i++)
  {
    float sym_occur = seq.at<float>(0, i); //the number of times a sybmol has occured
    if(sym_occur>0) //log of zero goes to infinity
      {
        cnt++;
        entr += (sym_occur/total_size)*(log2(total_size/sym_occur));
      }
  }
  cout<<"cnt: "<<cnt<<endl;
  return entr;

}

// myEntropy calculates relative occurrence of different symbols within given input                sequence using histogram
Mat myEntropy(Mat seq, int histSize)
{ 

  float range[] = { 0, 256 } ;
  const float* histRange = { range };

  bool uniform = true; bool accumulate = false;

  Mat hist;

  /// Compute the histograms:
  calcHist( &seq, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

  return hist;
}

答案 2 :(得分:0)

enter code here

&#13;
&#13;
//Calculate Entropy of 2D histogram
double Sum_prob_1k = 0, Sum_prob_kl = 0, Sum_prob_ln_1k = 0, Sum_prob_ln_kl = 0;
for (int k = start; k < end; k++)
{
    Sum_prob_1k = 0; Sum_prob_kl = 0;
    Sum_prob_ln_1k = 0; Sum_prob_ln_kl = 0;
    //i=1 need to be start = 1
    for (int i = 1; i < k; i++)
    {
        Sum_prob_1k += HiGreyN[i];
        if (HiGreyN[i] != 0)
            Sum_prob_ln_1k += (HiGreyN[i] * System.Math.Log(HiGreyN[i]));
    }
    for (int i = k; i < end; i++)
    {
        Sum_prob_kl += HiGreyN[i];
        if (HiGreyN[i] != 0)
            Sum_prob_ln_kl += (HiGreyN[i] * System.Math.Log(HiGreyN[i]));
    }
    //Final equation of entropy for each K
    EiGrey[k] = System.Math.Log(Sum_prob_1k) + System.Math.Log(Sum_prob_kl) -
               (Sum_prob_ln_1k / Sum_prob_1k) - (Sum_prob_ln_kl / Sum_prob_kl);
    if (EiGrey[k] < 0)
        EiGrey[k] = 0;
}
//End calculating 2D Entropy
&#13;
&#13;
&#13;