Hi , first i would like to thank to the forum for the help, ive made this code with the help of this forum.
Now I would like to ask for advices or recomendations.
The idea is to calculate the shannons entropy of an image.
I ve found a code in java that calculates the entropy of a string so i adapted that code:
what im doing is to load an image and then iterate over the pixels to get the hex colors values and then i join these values in one string.
At the end i end up with something like this:
"C0C1B4BABAACBABBADBBBCAEBEBFB1BCBDAFBEBEB0C1C0B1BFBEAFC0C0AFC2C1B1BFBEAEC3C2B2C4C2B2BFC0B3C0C1B3BDBCAEBBB9ABBDBBADBFBFB0BCBBACBFBEAEBDBCACBBBAAABAB9A9BDBCACC4C3B3C2BFB0BEBFB1C0C1B3C5C4B6C0BDB0BEBCAEC2C1B2BAB9A9BEBDADC1BFAFC1BFB1BDBBABBFBEAEC0BDAEBFBCADC5C4B6C0C1B3C2C2B4C3C2B3BEBDAEBFBDAEC0BFAFBFBDADBFBDADBDBAABBFBCADC0BDAEC0BDADC0BDACC1BFB1BEBDAFBFBFB0C4C3B3C1C0B0BBB9A9BCBAABBDBCACBDBBACBAB7A8BDBAABBEBBACC1BEAEC1BEAEBFBEAFB8B7A7BCBBABC2C1B1C3C1B2C2C0B0BDBBABBEBDADBFBDAEBDBAABBCB9AABCB9AABFBCADBFBCADC1C0B0B8B7A7BFBEAEC2C1B1BDBBACBEBCADBEBCACC3C3B3C3C1B2BDBAABBDBBABBEBBACBFBCADBBB8A9C0BFAFC0BEAEC2C1B1C6C4B5C0BFAFBDBBABC2C0B0C5C5B5C0BFAFBBB9AABEBDADC0BDADBDB9A9B8B2A3C2C1B1C3C2B2C0BFAFC3C1B2C5C4B4C2C1B1C1BFB0C3C1B1C0BDAEBFBCADBFBCADBEBAA9BAB5A5BAB4A6C6C5B5C4C3B3C0BFAFBFBEAEC4C3B3C0C0AFBFBDADC1BEAFC4C1B2C6C1B3C1BCACBEB9A9BAB4A6BFBAACC3C2B2C0BEAEBFBEAEBFBEAEC0BEAFC2BFB0BFBCAEC1BEAEC6C1B1C2BCAFC0BAACBFBAA9BCB6A7BEB8AAC1C0B0BFBCADBDBAABBEBCACC2BFB0C3C0B1BFBCACC1BDACC2BEAEBEB9ABBDB9ABBFBBAAC1BCACC2BDADBFBEAEBFBDAEBAB7A8B7B4A5BEBBACBEBBABBFBBAAC5BFAFC0BCADBDB9ABB9B6A7C6C2B3CAC4B6C8C3B"
then im calculating the entropy of that big string.
It seems it works ok, but i was thinking that is this a real way to calculate the entropy of an image?
is this in reality the entropy of an image?
Other thing i was thinking is that instead of using the hex values of each pixel , convert each hex value in just one character , for example convert "B7B4A5" into "a". What do you think? do you think that would be better?
Do you think this is a good way to calculating the entropy of an image? or can you think an improved or better way?
here is the code:
- import java.util.*;
- public double calculateShannonEntropy(String[] values) {
- Map<String, Integer> map = new HashMap<String, Integer>();
- // count the occurrences of each value
- for (String sequence : values) {
- if (!map.containsKey(sequence)) {
- map.put(sequence, 0);
- }
- map.put(sequence, map.get(sequence) + 1);
- }
- // calculate the entropy
- double result = 0.0d;
- for (String sequence : map.keySet()) {
- double frequency = (double) map.get(sequence) / values.length;
- result -= frequency * (Math.log(frequency) / Math.log(2));
- }
- return result;
- }
- //Como convertir este codigo de arriba solo en processing?
- void setup() {
- /****************************************************************/
- size(500, 400);
- imageMode(CENTER);
- /****************************************************************/
- final int ww = width>>1, hh = height>>1;
- PImage img;
- img = loadImage("imagen.png");
- final int imgW = img.width, imgH = img.height;
- final int imgLen = img.pixels.length;
- println(imgLen);
- image(img, ww, hh);
- /****************************************************************/
- int col = imgW>>2, row = imgH>>1;
- color colour = img.pixels[row*imgW + col];
- println( hex(colour) );
- /****************************************************************/
- String[] pixelsStr = new String[imgLen];
- for ( int i=imgLen; i!=0; pixelsStr[--i] = hex(img.pixels[i], 6) );
- String colStr = pixelsStr[row*imgW + col];
- println( colStr );
- println( pixelsStr );
- /****************************************************************/
- String strings = join(pixelsStr, "");
- println(strings);
- String [] sa = new String[strings.length() - 1];
- //println("THe collection of values we want to");
- //println("calculate the entropy for");
- for (int i = 0; i < sa.length; i++) {
- sa[i] = strings.substring(i, i+2);
- // println(sa[i]);
- }
- // The method returns a double need to cast to a float
- float entropy = (float) calculateShannonEntropy(sa);
- println("Entropy = " + entropy);
- }
1