熵
本文是Jesse Chen的原创文章,转载请注明出处。
熵的定义
假定一个信源的信息中包含
M
个符号,每个符号在信息中出现的概率为
H=−∑i=1Mpilog2pi(bits/symbol)
熵的计算
显然,从上述公式不难发现,计算熵,首先要计算每个符号出现的概率。对于图像而言,我们首先要统计直方图。这里给出计算一张灰度图像熵的Matlab代码。
function H = imEntropy(x)
% H = imEntropy(x)
% Computes the entropy of the input image x which is assumed to be of type
% uint8.
%%
[Height, Width] = size(x);
[p, Bins] = imhist(x);
%%
p = p/(Width*Height);
figure; plot(Bins, p, 'k');
xlabel('Pixel Value'); ylabel('Relative Count');
%%
H = sum(-p.*log2(max(p, eps)));
测试结果如下:
H=7.5272