0

For a given image Img, I calculated its entropy and got the same result as MATLAB's entropy function.

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)

However, when I try to do the same for difference image I don't get the same result

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)

Is there any suggestions how to fix this?

bezero
  • 1
  • 1
  • Consider the data type. Do you still have 256 intensity values in the diff image? Would you expect to? Food for thought. – chappjc Dec 31 '13 at 16:33

1 Answers1

0

Here is my java code

         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;
  }
Jithu R Jacob
  • 368
  • 2
  • 17