close

How to gauss-filter (blur) a floating point numpy array

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to gauss-filter (blur) a floating point numpy array in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to gauss-filter (blur) a floating point numpy array?

  1. How to gauss-filter (blur) a floating point numpy array?

    Here is my approach using only numpy. It is prepared with a simple 3×3 kernel, minor changes could make it work with custom sized kernels.

  2. gauss-filter (blur) a floating point numpy array

    Here is my approach using only numpy. It is prepared with a simple 3×3 kernel, minor changes could make it work with custom sized kernels.

Method 1

If you have a two-dimensional numpy array a, you can use a Gaussian filter on it directly without using Pillow to convert it to an image first. scipy has a function gaussian_filter that does the same.

from scipy.ndimage.filters import gaussian_filter

blurred = gaussian_filter(a, sigma=7)

Method 2

Here is my approach using only numpy. It is prepared with a simple 3×3 kernel, minor changes could make it work with custom sized kernels.

def blur(a):
    kernel = np.array([[1.0,2.0,1.0], [2.0,4.0,2.0], [1.0,2.0,1.0]])
    kernel = kernel / np.sum(kernel)
    arraylist = []
    for y in range(3):
        temparray = np.copy(a)
        temparray = np.roll(temparray, y - 1, axis=0)
        for x in range(3):
            temparray_X = np.copy(temparray)
            temparray_X = np.roll(temparray_X, x - 1, axis=1)*kernel[y,x]
            arraylist.append(temparray_X)

    arraylist = np.array(arraylist)
    arraylist_sum = np.sum(arraylist, axis=0)
    return arraylist_sum

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read