计算n维图像熵Python

时间:2017-06-30 09:47:28

标签: python image-processing scipy entropy

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

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比我目前的实现快了几百倍。

1 个答案:

答案 0 :(得分:1)

这是一个解决方案,不幸的是结果同样缓慢(虽然代码更清晰,并正确处理边界)。它使用generic_filter中的scipy.ndimage

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)