-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |