Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the entropy of an image and how is it calculated?

I have learnt that it is the randomness of the pixels. But please help with how this randomness is being calculated mathematically. And also how different images will have different entropy.

like image 860
gag123 Avatar asked May 13 '18 05:05

gag123


1 Answers

You may as well calculate the Shannon entropy straight from your img. Just do:

import skimage.measure    
entropy = skimage.measure.shannon_entropy(img)

If you want to see the maths behind:

import numpy as np
marg = np.histogramdd(np.ravel(img), bins = 256)[0]/img.size
marg = list(filter(lambda p: p > 0, np.ravel(marg)))
entropy = -np.sum(np.multiply(marg, np.log2(marg)))

First, marg is the marginal distribution of the two dimensional grayscale image img. bins is set to 256 for an 8-bit image. Then you need to filter out the probabilities that are equal to zero and finally sum over the remaining elements np.multiply(marg, np.log2(marg)), as defined by Shannon's entropy.

like image 177
Alejandro G. Avatar answered Sep 23 '22 18:09

Alejandro G.