Jithu R Jacob
Jithu R Jacob

Reputation: 368

Calculation of entropy of an Image using JAVA

I'm working on an Image Encryption program as a part of my academic project in Java.The output of the project would be an encrypted image.Now I need to calculate the entropy of original image and encrypted image for analysis based on this formula Image Entropy Description

Sample Data

How can I find find the entropy of an image according to this equation using Java

Upvotes: 0

Views: 3777

Answers (1)

Jithu R Jacob
Jithu R Jacob

Reputation: 368

Here is the code I developed , the entropy value calculated using this is consistent with the entropy() in Matlab thus I'm assuming it to be right please tell me otherwise

      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;
}

Upvotes: 1

Related Questions