Blurring or Smoothing Out Images — OpenCV

OpenCV python code for blurring an image using kernel or filter with the basic concepts of convolution, low pass filter, frequency of image, etc. by converting it into a gray scale image.

Akhitha Babu
The Startup
4 min readAug 15, 2020

--

Blurred images

An image can be represented as a matrix. Features like edge, contrast, etc. have to be extracted from the image for image processing.

Convolution is a fundamental operation on images in which a mathematical operation is applied to each pixel to get the desired result. Here, another matrix called kernel/filter is used which is smaller in the size of the image. On each pixel of the image, this filter is applied and the new value obtained which is the value of that pixel. The obtained image is called filtered image.
Each cell of the kernel contains some value, that kernel is kept above the pixel and corresponding values are multiplied and then summed up. This resultant value obtained is the new value of the pixel.

3X3 Kernel on 6X6 Image

What is Blurring?

Simply, it’s smoothening! In a blurred image, the image is smooth. ie, edges are not observed. A filter used for blurring is also called a low pass filter because it allows the low frequency to enter and stop high frequency. Here, frequency means the change of pixel value. Around the edge, pixel value changes rapidly as blur image is smooth so high frequency should be filtered out.
To blur the image a pixel value should be close to the neighbour value, so a filter with every call having value 1 is used.

Let’s see an example filter. Here, the filter is divided by 9 for normalization otherwise the value of a pixel will increase resulting in more contrast which is not the goal.

example filter for blurring

The size of this kernel determines the amount of blurring, with larger kernels result in averaging over a larger area producing more smoother images.

Let’s see the example code in python to blur an image using OpenCV .

Image saved in the location C:/Users/admin/Desktop/picture.png

To blur an image, each pixel is transformed to be the average value of its neighbours. This neighbour and the operation performed are mathematically represented as a kernel.

Here we blur an image by averaging the values of a 5 X 5 kernel around each pixel:

example1
example2

To highlight the effect of kernel size, here is the same blurring with a 50 X 50 kernel:

example3

Kernels are widely used in image processing to do everything from sharpening to edge detection. Let’s try applying a 5 X 5 kernel on an image. (Same as the example2 before.)

[[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]]

The centre element in the kernel is the pixel being examined, while the remaining elements are its neighbours. Since all elements have the same value (normalized to add up to 1), each has an equal say in the resulting value of the pixel of interest. We can manually apply a kernel to an image using filter2D to produce a similar blurring effect.

example4

The code preview:

Reference:

[1]Chris Albon, Machine Learning with Python Cookbook: Practical Solutions from Preprocessing to Deep Learning(2018), O’Reilly Media

--

--