代码之家  ›  专栏  ›  技术社区  ›  rakesh a

Scipy ndimage median_filter原点

  •  4
  • rakesh a  · 技术社区  · 9 年前

    我有一个二进制数组, a = np.random.binomial(n=1, p=1/2, size=(9, 9)) 。我使用 3 x 3 比如说, b = nd.median_filter(a, 3) 我希望这应该基于像素及其八个相邻像素执行中值滤波。然而,我不确定内核的位置。这个 documentation 说,

    原点:标量,可选。

    The origin parameter controls the placement of the filter. Default 0.0.
    

    如果默认值为零,则应使用当前像素和 3 x 3 右边和底部的网格,不是吗?默认值不应该是 footprint ? 在我们的 3 x 3 示例将对应于 (1, 1) 与…相反 (0, 0) ?

    谢谢

    1 回复  |  直到 9 年前
        1
  •  3
  •   Zack Graber    9 年前

    origin表示它只接受标量,但对我来说,它也接受类似数组的输入 scipy.ndimage.filters.convolve 作用通过0确实是足迹的中心。原点的值相对于中心。对于3x3封装,您可以指定值-1.0到1.0。以下是一些示例。请注意,在未指定原点的示例中,过滤器按预期居中。

    import numpy as np
    import scipy.ndimage
    
    a= np.zeros((5, 5))
    a[1:4, 1:4] = np.arange(3*3).reshape((3, 3))
    
    default_out = scipy.ndimage.median_filter(a, size=(3, 3))
    shift_pos_x = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, 1))
    shift_neg_x = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, -1))
    
    print(a)
    print(default_out)
    print(shift_pos_x)
    print(shift_neg_x)
    

    输出:

    输入数组:

    [[ 0.  0.  0.  0.  0.]
     [ 0.  0.  1.  2.  0.]
     [ 0.  3.  4.  5.  0.]
     [ 0.  6.  7.  8.  0.]
     [ 0.  0.  0.  0.  0.]]
    

    居中输出:

    [[ 0.  0.  0.  0.  0.]
     [ 0.  0.  1.  0.  0.]
     [ 0.  1.  4.  2.  0.]
     [ 0.  0.  4.  0.  0.]
     [ 0.  0.  0.  0.  0.]]
    

    向右移位输出:

    [[ 0.  0.  0.  0.  0.]
     [ 0.  0.  0.  1.  0.]
     [ 0.  0.  1.  4.  2.]
     [ 0.  0.  0.  4.  0.]
     [ 0.  0.  0.  0.  0.]]
    

    左移输出:

    [[ 0.  0.  0.  0.  0.]
     [ 0.  1.  0.  0.  0.]
     [ 1.  4.  2.  0.  0.]
     [ 0.  4.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.]]