计算图像熵时如何处理直方图零计数?

How to deal with histogram zero count when computing image entropy?

我正在尝试在 Python 中实现 Matlab 函数 entropy()

Entropy

Entropy is a statistical measure of randomness that can be used to characterize the texture of the input image.

Entropy is defined as -sum(p.*log2(p)), where p contains the normalized histogram counts returned from imhist.

我使用 openCV 来获取归一化直方图计数。直方图计数出现零怎么办?

据我所知,实践中通常使用两种方法:

  • 第一个是删除这些计数(即设置“0*log(0) = 0”)

  • 第二个是为每个计数添加一些小值 ep -> p+e 并重新归一化。

由于您正在将该函数移植到另一个编程框架中,因此在我看来,对您的问题的一个很好的回答是:"the same thing that Matlab does".

Matlab 在归一化直方图之前丢弃等于 0 的计数。如果你用命令open entropy打开原来的函数,你会在它的代码中找到你要找的东西:

% calculate histogram counts
p = imhist(I(:));

% remove zero entries in p 
p(p==0) = [];

% normalize p so that sum(p) is one.
p = p ./ numel(I);

E = -sum(p.*log2(p));