使用pdf的图像熵

时间:2013-12-31 12:27:31

标签: matlab entropy

对于给定的图像Img,我计算了它的熵并得到了与MATLAB的熵函数相同的结果。

hist_img = hist(Img(:),256);
pdf_img = hist_img./sum(hist_img);

H_pdf = sum(pdf_img.*log2(1./pdf_img))
H_test = entropy(input_img)

然而,当我尝试对差异图像做同样的事情时,我得不到相同的结果

dif = input_img(2:end,:) - input_img(1:end-1,:);
hist_dif = hist(dif(:),256);
pdf_dif = hist_dif./sum(hist_dif);

H_pdf = pdf_dif.*log2(1./pdf_dif);
H_pdf (isnan(H_pdf )) = 0;
H = sum(H_pdf )
H_test = entropy(dif)

有任何建议如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这是我的java代码

         public static double getShannonEntropy_Image(BufferedImage actualImage){
         List<String> values= new ArrayList<String>();
           int n = 0;
           Map<Integer, Integer> occ = new HashMap<>();
           for(int i=0;i<actualImage.getHeight();i++){
             for(int j=0;j<actualImage.getWidth();j++){
               int pixel = actualImage.getRGB(j, i);
               int alpha = (pixel >> 24) & 0xff;
               int red = (pixel >> 16) & 0xff;
               int green = (pixel >> 8) & 0xff;
               int blue = (pixel) & 0xff;
//0.2989 * R + 0.5870 * G + 0.1140 * B greyscale conversion
//System.out.println("i="+i+" j="+j+" argb: " + alpha + ", " + red + ", " + green + ", " + blue);
                int d= (int)Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue);
               if(!values.contains(String.valueOf(d)))
                   values.add(String.valueOf(d));
               if (occ.containsKey(d)) {
                   occ.put(d, occ.get(d) + 1);
              } else {
                  occ.put(d, 1);
              }
              ++n;
       }
    }
    double e = 0.0;
    for (Map.Entry<Integer, Integer> entry : occ.entrySet()) {
         int cx = entry.getKey();
         double p = (double) entry.getValue() / n;
         e += p * log2(p);
    }
 return -e;
  }