Gaussian Blur Algorithm from scratch in Python

Rohit Krishna
5 min readJan 4, 2023

--

Image by Dim Hou on Unsplash [Edited]

Here we will be discussing about image filters, convolution, etc.. along with the Python implementation, as well as, learn to use OpenCV for the same task for real-world applications.

Gaussian Filter

What is a gaussian filter and why do we use it?

In digital signal processing, a Gaussian filter is a filter whose impulse response is a Gaussian function.

If that definition is quite a mouthful, let me clear it that for you.

Do you ever come across with the term Normal distribution, bell-shaped curve, or standard distribution these all are the same as Gaussian distribution, So i hope yo get the point that, it is that much important in the STEM field.

The equation for the gaussian distribution is,

The equation for the gaussian distribution in 2D (has two variables x and y) is,

You can use the below dipiction to visualize the 1D and 2D Gaussian distribution/kernel.

depiction of the steps of getting gaussian kernel matrix from it’s 1D plot | Image by author

There are many filters such as box filter, mean filter, median filter, etc… but in all filters available for this operation gaussian filter stands out, that is because it preserves a bit more details when you compare for example the box filter.

Photo by Christopher Campbell on Unsplash [Edited]

There is also another filter called Bilateral Filter which performs better than gaussian, but it’s computationaly a bit expensive, perhaps we will cover that in another article.

So now you know all about gaussian kernel,

before getting into the code, you have to also understand, The Convolution Operation

Convolution

Convolution is a matrix operation, which is used to transform a matrix with a given kernel. It is the heart of the Convolutional Neural Networks (CNNs)

Animation of how the convolution operation is performed

In the above depiction,

  • Left most matrix is the input data
  • The matrix in the center is a kernel
  • In the rightmost matrix is the output of the convolution operation performed on the input data with the given filter/kernel

Each cell in the output is been calculated by taking the element-wise multiplication (Hadamard product) of the kernel/filter with its corresponding sub-matrix of the input data, and taking the total summation of the product. The sub-matrix will change in each iteration with the given stride.

Some terms related to this operation,

Padding

As the name implies it is the level of empty/zero data point added in the border of the input data, Which is used to give the corner data points more role in the computation. In the above depiction padding was zero.

Strides

It is the length of the jump in taking the next sub-matrix of the input data. In the above depiction, stride was one.

Cross-Correlation

The operation that I explained yet is Cross-Correlation “not Convolution”, but before putting the full stop, let me make it clear,

if you rotate the given kernel 180° and perform the explained operation, that is Convolution, but in our context, the rotation of the kernel doesn’t matter so both can be considered as the same operation (but not really)

I’m using the term convolution because, it is a bit fancier and more famous than Cross-Correlation (i think).

In our context, the input data is the image that we are giving and kernel/filter is the gaussian kernel (obviously).

The procedure is to perform convolution operation on an image with the gaussian kernel matrix, which results in a blurred image of the corresponding given image.

Ta daa… rool the drums 🥁, so now we can get into the code.

Code

Gaussian Filter

code to generate the gaussian kernel matrix

Convolution Operation

convolution algorithm implementation

The above code is not in anyway optimized or anyting, it is just for teaching purposes, i prefer using cv2.filter2D, scipy.signal.correlate2d or scipy.signal.convolve2d

For the complete code refer to my Github Repo below,

Conclusion

I hope you understood how the image blur operation is actually perform under the hood. Its not a bad practice to code this from scratch, It actually helps you understand things far better than just an explanation. But when you work on real-world projects you don’t have to code from scratch, then you can use libraries such as OpenCV for your help.

In Python using OpenCV, you can generate a gaussian blurred image as below,

import cv2
img = cv2.imread('<path_to_image>')
imgBlur = cv2.GaussianBlur(img, (9, 9), 3) # img, kernel_size, sigma

If this article helps you in any way, i really appreciate if you could support me through BuyMeACoffee, it really motivates me to produce amazing articles for you guys 🥰.

References

--

--