RGB 像素的熵 [英] Entropy of RGB pixel
本文介绍了RGB 像素的熵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Python 中正确地获取单个 RGB 像素的熵,或如下图所示.
How to get corectly in Python the entropy for a single RGB pixel, or something like in the below image.
试过这个:
def entropy(*X):
return np.sum(-p * np.log2(p) if p > 0 else 0 for p in
(np.mean(functools.reduce(np.logical_and, (predictions == c for predictions, c in zip(X, classes))))
for classes in itertools.product(*[set(x) for x in X])))
print('Entropy:',entropy(np.array([255,2,2]),np.array([255,0,1]),np.array([255,0,0]),np.array([255,1,0]),np.array([255,2,0])))
print('Entropy:',entropy(np.array([255,2,2])))
print('Entropy:',entropy(np.array([255,0,0])))
print('Entropy:',entropy(np.array([0,255,0])))
print('Entropy:',entropy(np.array([255,100,200])))
结果:
Entropy: 1.584962500721156
Entropy: 0.9182958340544896
Entropy: 0.9182958340544896
Entropy: 0.9182958340544896
Entropy: 1.584962500721156
但是红色和绿色像素的熵相同,似乎无法区分,并且看起来与下表不同.我做错了什么以及如何正确地做到这一点?
However entropy is same for red and green pixel, seems it cannot distinguish, and looks different from the below table. What I'm doing wrong and how to do this correctly?
推荐答案
如果有帮助,试试这个
def entropy(labels, base=None):
value,counts = np.unique(labels, return_counts=True)
norm_counts = counts / counts.sum()
print(norm_counts)
e = 2.718281828
base = e if base is None else base
return -(norm_counts * np.log(norm_counts)/np.log(base)).sum()
f = np.array([[[[255,10,2],
[255,0,1],
[255,0,0],
[255,1,0],
[255,2,0]]]])
h = entropy(f, 2)
print('h=',h)
这篇关于RGB 像素的熵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文