缺失d的numpy数组的局部均值滤波

2024-04-23 11:56:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我可以使用^{},但我的图像超出了[-1, 1]的范围,出于某种原因,scikit image要求这样做。在

还有一个^{},但它插入缺失的数据。对于简单的平均值,不需要插值。只是平均有效像素。输入和输出的有效掩码是相同的。在

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

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


Tags: 数据图像imagenumpy局部像素数组scikit
1条回答
网友
1楼 · 发布于 2024-04-23 11:56:17

@stefan van der walt所指的方法,即使用scipy.ndimage.generic_filter和{}(尚未对速度进行优化)。在

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)

相关问题 更多 >