Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

image processing - Gaussian filter in 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)

How I choose the parameter of sigma to make sure that my Gaussian window is 5x5 pixels?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check out the source code here: https://github.com/scipy/scipy/blob/master/scipy/ndimage/filters.py

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

So

(w - 1)/2 = int(truncate*sigma + 0.5)

For w = 5, the left side is 2. The right side is 2 if

2 <= truncate*sigma + 0.5 < 3

or

1.5 <= truncate*sigma < 2.5

If you choose truncate = 3 (overriding the default of 4), you get

0.5 <= sigma < 0.83333...

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.)

First create an input with a single 1:

In [248]: x = np.zeros(9)

In [249]: x[4] = 1

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

... and at 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...