From c95ceba94151117e0fb185a72981acef0d4c69b2 Mon Sep 17 00:00:00 2001 From: dave griffiths Date: Mon, 23 Nov 2015 11:06:40 +0000 Subject: [PATCH] some entropy code --- entropy/entropy.py | 24 ++++++++++++++++++++ entropy/image-entropy.py | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 entropy/entropy.py create mode 100644 entropy/image-entropy.py diff --git a/entropy/entropy.py b/entropy/entropy.py new file mode 100644 index 0000000..40b2553 --- /dev/null +++ b/entropy/entropy.py @@ -0,0 +1,24 @@ +from __future__ import division +import math + +## simple measurement of entropy + +def hist(source): + hist = {}; l = 0; + for e in source: + l += 1 + if e not in hist: + hist[e] = 0 + hist[e] += 1 + return (l,hist) + +def entropy(hist,l): + elist = [] + for v in hist.values(): + c = v / l + elist.append(-c * math.log(c ,2)) + return sum(elist) + +source = "222222222222222222122" +(l,h) = hist(source); +print 'Entropy:', entropy(h, l) diff --git a/entropy/image-entropy.py b/entropy/image-entropy.py new file mode 100644 index 0000000..168e139 --- /dev/null +++ b/entropy/image-entropy.py @@ -0,0 +1,47 @@ +from PIL import Image +import numpy as np +from matplotlib import pyplot as plt + +def entropy(signal): + ''' + function returns entropy of a signal + signal must be a 1-D numpy array + ''' + lensig=signal.size + symset=list(set(signal)) + numsym=len(symset) + propab=[np.size(signal[signal==i])/(1.0*lensig) for i in symset] + ent=np.sum([p*np.log2(1.0/p) for p in propab]) + return ent + + +colorIm=Image.open('mo.JPG') +greyIm=colorIm.convert('L') +colorIm=np.array(colorIm) +greyIm=np.array(greyIm) + +N=5 +S=greyIm.shape +E=np.array(greyIm) +for row in range(S[0]): + for col in range(S[1]): + Lx=np.max([0,col-N]) + Ux=np.min([S[1],col+N]) + Ly=np.max([0,row-N]) + Uy=np.min([S[0],row+N]) + region=greyIm[Ly:Uy,Lx:Ux].flatten() + E[row,col]=entropy(region) + + +plt.subplot(1,3,1) +plt.imshow(colorIm) + +plt.subplot(1,3,2) +plt.imshow(greyIm, cmap=plt.cm.gray) + +plt.subplot(1,3,3) +plt.imshow(E, cmap=plt.cm.jet) +plt.xlabel('Entropy in 10x10 neighbourhood') +plt.colorbar() + +plt.show()