【问题标题】:Calculating n-dimensional Image Entropy Python计算n维图像熵Python
【发布时间】:2017-06-30 09:47:27
【问题描述】:

我正在尝试计算高维“图像”的熵。显而易见的方法:

from scipy.stats import entropy

kernel_radius=2
entropy_stack = np.zeros(Stack.shape)
for ii in range(kernel_radius,entropy_stack.shape[0]-kernel_radius):
    for jj in range(kernel_radius,entropy_stack.shape[1]-kernel_radius):
        for kk in range(kernel_radius,entropy_stack.shape[2]-kernel_radius):    
            entropy_stack[ii,jj,kk]=entropy(Stack[ii-kernel_radius:ii+kernel_radius,jj-kernel_radius:jj+kernel_radius,kk-kernel_radius:kk+kernel_radius].flatten())

有效,但速度非常慢。

在高维图像中计算熵是否有任何实现技巧?更好的是:是否有任何具有此功能优化版本的软件包?

我知道 scikit-image 的 entropy 做得很好,但仅限于 2D。同样,我知道 matlab 的 entropyfilt,它的执行速度比我当前的实现快几百倍。

【问题讨论】:

  • 仅供参考,scipy.stats 中的entropy 期望概率,而不是值。
  • 另外,entropy 实现了符号流的熵计算,而不是序数流。我建议您研究无二进制熵估计器,或者您 - 至少 - 将图像堆栈中的值分成少于 256 个不同的类/符号(假设您将图像保存在 uint8 数组中)。

标签: python image-processing scipy entropy


【解决方案1】:

不幸的是,这是一个同样缓慢的解决方案(尽管代码更简洁并且可以正确处理边界)。它在scipy.ndimage 中使用generic_filter

import numpy as np
from scipy.ndimage import generic_filter
from scipy.stats import entropy

def _entropy(values):
    probabilities = np.bincount(values.astype(np.int)) / float(len(values))
    return entropy(probabilities)

def local_entropy(img, kernel_radius=2):
    """
    Compute the local entropy for each pixel in an image or image stack using the neighbourhood specified by the kernel.

    Arguments:
    ----------
    img           -- 2D or 3D uint8 array with dimensions MxN or TxMxN, respectively.
                     Input image.
    kernel_radius -- int
                     Neighbourhood over which to compute the local entropy.

    Returns:
    --------
    h -- 2D or 3D uint8 array with dimensions MxN or TxMxN, respectively.
         Local entropy.

    """
    return generic_filter(img.astype(np.float), _entropy, size=2*kernel_radius)

【讨论】:

  • 谢谢。这个版本肯定更干净。我将不得不看看输入值和概率之间的区别 'entropy()'
猜你喜欢
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多