forked from scikit-image/scikit-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropy.py
41 lines (32 loc) · 1.12 KB
/
entropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from numpy import unique
from scipy.stats import entropy as scipy_entropy
def shannon_entropy(image, base=2):
"""Calculate the Shannon entropy of an image.
The Shannon entropy is defined as S = -sum(pk * log(pk)),
where pk are frequency/probability of pixels of value k.
Parameters
----------
image : (N, M) ndarray
Grayscale input image.
base : float, optional
The logarithmic base to use.
Returns
-------
entropy : float
Notes
-----
The returned value is measured in bits or shannon (Sh) for base=2, natural
unit (nat) for base=np.e and hartley (Hart) for base=10.
References
----------
.. [1] `https://en.wikipedia.org/wiki/Entropy_(information_theory) <https://en.wikipedia.org/wiki/Entropy_(information_theory)>`_
.. [2] https://en.wiktionary.org/wiki/Shannon_entropy
Examples
--------
>>> from skimage import data
>>> from skimage.measure import shannon_entropy
>>> shannon_entropy(data.camera())
7.231695011055706
"""
_, counts = unique(image, return_counts=True)
return scipy_entropy(counts, base=base)