Search code examples
pythonscipyfilteringgaussianblur

Is `sigma` expressed in the units of pixel for `gaussian_filter`?


I am looking at scipy.ndimage.gaussian_filter, and could not get what sigma means here. Is sigma= n means n is the number of pixels, thus a region within n number of pixel on all sides of a point contributes the most on the averaging?


Solution

  • Yes, it is. This isn't obvious from the convoluted (no pun intended) way in which the Gaussian kernel is computed by SciPy, but here is an empirical verification: I convolved the Gaussian with a vector a that has a single entry 1, obtaining the kernel of the convolution. Then computed the variance in the usual way E[X**2] - E[X]**2 where X is demonstrably in pixels (np.arange(len(a))).

    from scipy.ndimage.filters import gaussian_filter
    import numpy as np
    a = np.zeros((100,))
    x = np.arange(len(a))
    a[len(a)//2] = 1
    for sigma in range(3, 10):
        kernel = gaussian_filter(a, sigma)
        var = np.sum(x**2*kernel) - np.sum(x*kernel)**2
        print("Given sigma {}, empiric value {}".format(sigma, np.sqrt(var)))
    

    Output:

    Given sigma 3, empiric value 2.999207360674749
    Given sigma 4, empiric value 3.9987184940057614
    Given sigma 5, empiric value 4.998211402871647
    Given sigma 6, empiric value 5.997694984501222
    Given sigma 7, empiric value 6.997173172490447
    Given sigma 8, empiric value 7.996647965992465
    Given sigma 9, empiric value 8.99612048649375