Skip to content

Commit

Permalink
some entropy code
Browse files Browse the repository at this point in the history
  • Loading branch information
nebogeo committed Nov 23, 2015
1 parent 0a4ec0b commit c95ceba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions entropy/entropy.py
Original file line number Diff line number Diff line change
@@ -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)
47 changes: 47 additions & 0 deletions entropy/image-entropy.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c95ceba

Please sign in to comment.