# Entropy of an image

If I want to measure entropy of an image how should I do it?

My aim is to ignore things like tree leaves moving in the wind with my C# desktop application.

I know matlab has this functionality but I cannot see it within Emgu which is what I am using.

I got as far as finding 'Shannon Entropy' is probably the way forward but I am stuck as to how to implement it in C# speficially for images.

You have probably found out that entropy of a discrete random variable $X$ is defined by

$$H(X)=-\sum_{i=1}^N p_i\log_2 p_i\tag{1}$$

where $N$ is the size of the alphabet (i.e. the number of possible values of $X$), and $p_i$ is the probability that $X$ assumes the $i^{th}$ value of the alphabet. The entropy $H(X)$ can be interpreted as the average information obtained by observing $X$.

In the case of an image, you need to estimate the values $p_i$. You can do this by computing the histogram of the image: define a number $N$ of bins of (equal) grayscale ranges and simply count the number of pixels per bin. Dividing the number of pixels per bin by the total number of pixles gives you an estimate of $p_i$. Then you just need to plug these values into (1) and you get the entropy of the image.

• Hi, thats for your response. I did come across that formulae (1) on my searching travels. OK, I can take an image, grayscale it, compute the histogram of it which will return me 256 bins each with a number of pixel counts within each bin. Now, to understand the variables I say 'X' is the total number of pixels of that image. N is the number of bins ~ 256. So is 'i' my measuring axis? – Andrew Simpson Mar 23 '14 at 13:22
• The variable $X$ is the grayscale value, and $i$ is the bin index. So $p_i$ is the probability that a pixel of your image has a gray-scale value corresponding to bin $i$. – Matt L. Mar 23 '14 at 13:25
• Hi, thanks again for your time. It is educational and much appreciated. I 'think' i understand what you just stated. But, I need help to apply this practically to my objective which is discerning whether the motion movement i detect within an image has a high atrophy 'rating' (i.e. trees, bushes, leaves etc) as opposed to a solid object like a car/person. Would I isolate the pixels with the ROI and enumerate through each individual pixel value using that formulate to rate the 'randomness' of the pixel changes? – Andrew Simpson Mar 23 '14 at 13:31
• I think we're now discussing different questions. I tried to explain how to measure the entropy of an image, as asked in your original question. Maybe it would make sense to formulate a new question concerning the overall problem and the algorithm you have in mind. – Matt L. Mar 23 '14 at 15:02
• Hi, you did explain entropy to me and i am very grateful. I thought I had explained in my question what I was try to achieve. "My aim is to ignore things like tree leaves moving in the wind with my C# desktop application." i cannot think of another way to express that. But i will give you tick as you did explain entropy to me. Thanks :) – Andrew Simpson Mar 23 '14 at 16:16

"If I want to measure entropy of an image how should I do it?"

An excellent question and one that, as of September 2016, has a simple and directly computable answer.

The question is answered in a recent Stack Exchange correspondence:

https://stats.stackexchange.com/questions/235270/entropy-of-an-image

A detailed explanation is presented in the recent arXiv preprint:

https://arxiv.org/abs/1609.01117

Note that the preprint includes a link to GitHub experimental sourcecode.

• The author of that answer confuses entropy and information -- an image is a signal, it can have information, not entropy. Only sources have entropy (unless there's a different use of the term in image processing). – MBaz Dec 8 '16 at 2:31
• Hi both, thanks for the link and yes i have wised up now to the error of my question. – Andrew Simpson Dec 8 '16 at 6:54