gpt4 book ai didi

python - Scipy ndimage median_filter 来源

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:44 30 4
gpt4 key购买 nike

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

origin : scalar, optional.

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

如果默认值为零,它应该将当前像素和 3 x 3 网格放在右边和底部,不是吗?默认不应该是 footprint 的中心吗?在我们的 3 x 3 示例中,哪个对应于 (1, 1) 而不是 (0, 0)

谢谢。

最佳答案

origin 说它只接受标量,但对我来说它也接受类似数组的输入,scipy.ndimage.filters.convolve 也是如此。功能。通过 0 确实是足迹的中心。 Origin 的值是相对于中心的。对于 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.]]

关于python - Scipy ndimage median_filter 来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262294/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com