Python RGB像素的熵
如何在Python中正确获得单个RGB像素的熵,或下图中的类似内容 我试过这个:Python RGB像素的熵,python,image-processing,statistics,entropy,Python,Image Processing,Statistics,Entropy,如何在Python中正确获得单个RGB像素的熵,或下图中的类似内容 我试过这个: 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 clas
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
然而,对于红色和绿色像素,熵是相同的,似乎无法区分,并且看起来与下表不同。我做错了什么,如何正确地做
来源:如果有帮助,试试这个
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)
这毫无意义。这张桌子是从哪里来的?他们是如何定义“熵”的?但它给出了不同的熵,如果我想计算单个像素的熵,最重要的是什么,而不是它会返回不同像素的相同熵,为什么会这样?这是香农熵,它只显示变换系数,如果你有更多的数据,它很有用,单个像素可能无法工作。试试其他的熵,这只是其中之一。