scipy中的高斯滤波器 [英] Gaussian filter in scipy
问题描述
我想在512x512像素的图像上应用尺寸为5x5像素的高斯滤镜。我发现了一个scipy函数:
I want to apply a Gaussian filter of dimension 5x5 pixels on an image of 512x512 pixels. I found a scipy function to do that:
scipy.ndimage.filters.gaussian_filter(input, sigma, truncate=3.0)
我如何选择西格玛参数以确保我的高斯窗口为5x5像素?
How I choose the parameter of sigma to make sure that my Gaussian window is 5x5 pixels?
推荐答案
在这里查看源代码:https://github.com/scipy/scipy/blob/master/scipy/ndimage/filters.py
你会看到 gaussian_filter
为每个轴调用 gaussian_filter1d
。在 gaussian_filter1d
中,过滤器的宽度由 sigma
和 truncate的值隐式确定
。实际上,宽度 w
是
You'll see that gaussian_filter
calls gaussian_filter1d
for each axis. In gaussian_filter1d
, the width of the filter is determined implicitly by the values of sigma
and truncate
. In effect, the width w
is
w = 2*int(truncate*sigma + 0.5) + 1
所以
(w - 1)/2 = int(truncate*sigma + 0.5)
对于w = 5,左边是2.右边是2,如果
For w = 5, the left side is 2. The right side is 2 if
2 <= truncate*sigma + 0.5 < 3
或
1.5 <= truncate*sigma < 2.5
如果选择 truncate = 3
(覆盖默认值4),你得到
If you choose truncate = 3
(overriding the default of 4), you get
0.5 <= sigma < 0.83333...
我们可以通过过滤一个全0的输入来检查这个,除了一个1 (即找到滤波器的脉冲响应)并计算滤波输出中的非零值的数量。 (在下面, np
是 numpy
。)
We can check this by filtering an input that is all 0 except for a single 1 (i.e. find the impulse response of the filter) and counting the number of nonzero values in the filtered output. (In the following, np
is numpy
.)
首先使用单个1创建输入:
First create an input with a single 1:
In [248]: x = np.zeros(9)
In [249]: x[4] = 1
检查中的变化大小 sigma = 0.5
...
Check the change in the size at sigma = 0.5
...
In [250]: np.count_nonzero(gaussian_filter1d(x, 0.49, truncate=3))
Out[250]: 3
In [251]: np.count_nonzero(gaussian_filter1d(x, 0.5, truncate=3))
Out[251]: 5
...和 sigma = 0.8333 ...
:
In [252]: np.count_nonzero(gaussian_filter1d(x, 0.8333, truncate=3))
Out[252]: 5
In [253]: np.count_nonzero(gaussian_filter1d(x, 0.8334, truncate=3))
Out[253]: 7
这篇关于scipy中的高斯滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!