Skip to content

Instantly share code, notes, and snippets.

@micahwalter
Created March 25, 2013 14:56
Show Gist options
  • Save micahwalter/5237697 to your computer and use it in GitHub Desktop.
Save micahwalter/5237697 to your computer and use it in GitHub Desktop.
def shannon_entropy(img):
# calculate the shannon entropy for an image
histogram = img.histogram()
histogram_length = sum(histogram)
samples_probability = [float(h) / histogram_length for h in histogram]
return -sum([p * math.log(p, 2) for p in samples_probability if p != 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment