【问题标题】:entropy for a gray image in opencvopencv中灰度图像的熵
【发布时间】:2014-09-15 19:08:11
【问题描述】:

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

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系统中使用它。我将灰色图像的熵设为零。请帮帮我。我哪里做错了。

【问题讨论】:

  • 不应该是0吗?低熵值意味着图像中有很多顺序。高熵意味着发生了很多事情(随机噪声比均匀图像具有更高的熵)

标签: c++ opencv information-theory


【解决方案1】:

在不知道您使用的是什么图像的情况下,我们无法知道零熵结果是否不是正确答案(正如 @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;

【讨论】:

    【解决方案2】:

    这是我正在使用的,希望对您有所帮助; 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;
    }
    

    【讨论】:

    • 为什么你不能从 Mat seq 获得尺寸?
    【解决方案3】:
    enter code here
    

    //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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    相关资源
    最近更新 更多