# calculating entropy of image or alternative?

Christian Gollwitzer auriocus at gmx.de
Sat Jul 4 14:18:24 CEST 2015

```Am 04.07.15 um 03:17 schrieb telmo bacile:
> Hi list,   I found a code that calculates entropy of images with
> python that can be used for classifying interesting images from
> uninteresting ones. Interesting images has more structured patterns
> while uninsteresting are more noisy or completely homogeneous.
>
> I was thinking this code (entropy of image) can be used for measuring
> the level of disorder of a group of points in the image.
> For example:
> Imagine that we have 3 images,  each image has 6 dots, the first one
> has very ordered dots , the second one have dots a little bit
> disordered and the third one has very dissordered dots.
> Then entropy of each image should measure the level of
> dissorganization of the dots.

Your question is not a Python question, it is about image processing.
Therefore x-post and fup to comp.dsp. Since you seem to be using the
mailing list, you can access comp.dsp via Google Groups:

> But the wierd thing is that when i experimented with this i got resuts
> without sense.
> The result i get is that  the image with intermedium dissorder has
> less entropy that the very ordered image .  Do anybody have an idea
> why im getting this result?

Your model is too simple. Entropy by itself is not defined on bytes, but
on a stream of symbols. Your program simply takes each pixel as a
symbol. Therefore, the order is not considered at all! You could
randomly shuffle all pixels, and still get the exact same result. Only
the relative frequencies of the color is respected. In order to improve
on that, and to take into account the distribution you would need to
build a model. For example, you could predict each pixel value from the
previous line and then send it to the encoder resp. the entropy
calculation. This is what PNG does, for instance. As a rough estimate,
you could compress your image using PNG and compare the file size.

> Maybe im misunderstanding something. Is it possible to use entropy of
> image to measure the level of dissorganization of a group of points in
> an image? If not , is there is another method for doing this?

In principle yes....

Christian

>
> T.
>
>
>
> here is the code:
>
> import math
> from PIL import Image
> imageFile = 'int2.jpg'
> print imageFile
> im = Image.open(imageFile)
> rgbHistogram = im.histogram()
> print 'Snannon Entropy for Red, Green, Blue:'
> for rgb in range(3):
>      totalPixels = sum(rgbHistogram[rgb * 256 : (rgb + 1) * 256])
>      ent = 0.0
>      for col in range(rgb * 256, (rgb + 1) * 256):
>          freq = float(rgbHistogram[col]) / totalPixels
>          if freq > 0:
>              ent = ent + freq * math.log(freq, 2)
>      ent = -ent
>      print ent
>

```