【问题标题】:Local mean filter of a numpy array with missing data缺少数据的 numpy 数组的局部均值滤波器
【发布时间】:2017-11-01 18:30:57
【问题描述】:

我想对存储为 numpy 数组的图像进行局部均值过滤。图像边缘附近有一些缺失的像素,用有效的掩码(布尔数组)表示。

我可以使用skimage.filters.rank,但我的图像超出了[-1, 1] 范围,出于某种原因,scikit-image 有此要求。

还有astropy.convolution,但它会插入缺失的数据。对于一个简单的平均值,不需要插值。仅平均有效像素。输入输出有效掩码相同。

简单地将无效像素设置为零不是一种选择,因为它会污染附近的有效像素平均值。

还有this question,但它不是重复的,因为它询问更通用的卷积(这只是平均)。

【问题讨论】:

  • 不确定您的方法,但-1,1 的要求应该不是问题。只需将您的图像缩放到这种格式。平均计算在概念上具有浮点数学性质,在某些时候你肯定需要铸件(如果你害怕丢失信息)。你特别要求这个简单的操作,但是inpaint_biharmonic 有什么问题?
  • scikit-image 和 scipy.ndimage 都不支持过滤器中的缺失值。也就是说,可以教 scipy.ndimage.generic_filter 执行此操作,并且可以快速完成,如下所示:ilovesymposia.com/2017/03/15/…

标签: python numpy scipy convolution scikit-image


【解决方案1】:

@stefan-van-der-walt 所指的方法,即使用scipy.ndimage.generic_filternumpy.nanmean(尚未针对速度进行优化)。

import numpy as np
from scipy.ndimage import generic_filter

def nanmean_filter(input_array, *args, **kwargs):
    """
    Arguments:
    ----------
    input_array : ndarray
        Input array to filter.
    size : scalar or tuple, optional
        See footprint, below
    footprint : array, optional
        Either `size` or `footprint` must be defined.  `size` gives
        the shape that is taken from the input array, at every element
        position, to define the input to the filter function.
        `footprint` is a boolean array that specifies (implicitly) a
        shape, but also which of the elements within this shape will get
        passed to the filter function.  Thus ``size=(n,m)`` is equivalent
        to ``footprint=np.ones((n,m))``.  We adjust `size` to the number
        of dimensions of the input array, so that, if the input array is
        shape (10,10,10), and `size` is 2, then the actual size used is
        (2,2,2).
    output : array, optional
        The `output` parameter passes an array in which to store the
        filter output. Output array should have different name as compared
        to input array to avoid aliasing errors.
    mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
        The `mode` parameter determines how the array borders are
        handled, where `cval` is the value when mode is equal to
        'constant'. Default is 'reflect'
    cval : scalar, optional
        Value to fill past edges of input if `mode` is 'constant'. Default
        is 0.0
    origin : scalar, optional
        The `origin` parameter controls the placement of the filter.
        Default 0.0.

    See also:
    ---------
    scipy.ndimage.generic_filter
    """
    return generic_filter(input_array, function=np.nanmean, *args, **kwargs)

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 2014-02-20
    • 2015-10-08
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2013-09-20
    • 2012-12-23
    相关资源
    最近更新 更多