How Image Blurring Works

Akeyla Pradia Naufal
The Startup
Published in
7 min readJul 28, 2020
Photo by Carson Vara on Unsplash

Definition

Roughly speaking, blurring an image is make the image less sharp. This can be done by smoothing the color transition between the pixels.

To accomplish this target, we need to apply a convolution operation of a specialized matrix, called kernel, to the image’s matrix.

What is convolution?

Mathematically speaking, a convolution of two matrix, A with size m x n, and B with size p x q, is a (m + p -1) x (n+q-1) matrix C with entries:

Convolution in matrices

Casually speaking, convolution is just forming a new matrix in which the entries are the sums of product of the entries of one matrix with the corresponding entry of another matrix. All of these products could be calculated along the rows and columns.

What is kernel?

Kernel is a matrix that has purpose to transform an image. It is not exclusive to image blurring. It can also be used to detect edges, sharpening edges, and others kind of image transformation. Kernel that used in blurring image is a low-pass filter kernel. It allows low frequency to enter and stop the higher frequency.

Application

Why do we even need to blur an image in the first place? After all, doesn’t it make the image become less visible?

It turns out that blurring an image has several purpose.

Firstly, it reduce the noises in the image. A random brightness spot or incorrect color spot, depends on the type of noise, could be reduced by blurring the image with suitable type of blur.

Blurring an image also reduces the size of image. With appropriate blurring function, we can deblurring the blurred image into the original image. This can be very helpful in transferring a vast size of images.

Blurring also used in media. For example, when the news’ picture is not appropriate or explicit. Another use is to hide the face, name, and all private data of people that happens to be included in the image accidentally.

Lastly, for entertainment purpose. For instance, in movies and digital artworks. The blurring effect may enhance the feels of lovely citylights scene. Or it may helps movie audience knows this particular scene occurs in past.

Types of Blurring

There are a number of blurring filter type that can be used. All has its own characteristics. In this section, I explain two of them: Mean/Box/Average filter and Gaussian filter. In Python, these blur filter is contained in OpenCV package. In all of these section, the module I used is

# For blur and convolution
import cv2
# For creating matrix
import numpy as np
# From showing images
from matplotlib import pyplot as plt

1. Mean Filter (Average Filter/Box Filter)

This filter takes the average of pixels in kernel and replace the central pixel with this average. This kernel has all of its elements same and sums up to 1. The kernel must be odd-sized. Hence, if the size of the kernel is a x b, the mean filter kernel is

The general form of mean filter kernel

For example, when a = 3 and b = 3, the kernel is

The greater value of kernel size, the greater blurring effect because the number of pixels involved is greater and the transition of colors become smoother.

# Import the image
img = cv2.imread('103057.jpg')
# Show the original image
plt.figure()
plt.imshow(img)
plt.show()
# Creating a mean filter kernel
def meankernel(size):
mk = np.ones((size, size), np.float32)/(size ** 2)
return mk
# Convolute the kernel with image
for size in range(3, 14, 2):
blurImg = cv2.filter2D(img, -1, meankernel(size))
plt.figure()
plt.imshow(blurImg)
plt.show()

The OpenCV module has given a function for mean filter: cv2.blur(). Here is the sample code for mean filter with different size of kernel:

# Import the image
img = cv2.imread('103057.jpg')
# Show the original image
plt.figure()
plt.imshow(img)
plt.show()
# Show the blurred image with different size of kernel
for size in range(3, 14, 2):
blurImg = cv2.blur(img,(size,size))
plt.figure()
plt.imshow(blurImg)
plt.show()

The original image is:

Original image.

And the blurred images are

3 x 3 mean filter kernel — 5 x 5 mean filter kernel
7 x 7 mean filter kernel — 9 x 9 mean filter kernel
11 x 11 mean filter kernel — 13 x 13 mean filter kernel

2. Gaussian Filter

This filter gives different weight to each entries in matrix as entries in kernel. The closer pixel to the selected pixel has greater weight while the further pixel has lower weight. In theory, all pixel in matrix contributes to the value of the entry in final matrix. In fact, the complete (or theoretical) formula for this filter is

Gaussian function

where x and y is the horizontal and vertical distance. σ stands for the standard deviation. The higher value of σ, the greater blurring effect.

In practice, we estimate the Gaussian function by an odd-sized kernel whose entries are the estimation of the Gaussian function at that pixel. Moreover, this kernel cannot be sufficiently large because the further pixel give smaller contribution to the value of kernel. Often, we ignore the σ and just give it to the program to determine the suitable value for the given size kernel. For example, the approximation of 3 x 3 Gaussian kernel is

Approximation for 3 x 3 Gaussian kernel. Source: Wikipedia

To calculate the Gaussian kernel, we can use the OpenCV function of cv2.GaussianBlur(). Here is the sample code for Gaussian filter with different size of kernel:

# Import the image
img = cv2.imread('103057.jpg')
# Show the original image
plt.figure()
plt.imshow(img)
plt.show()
# Show the blurred image with different size of kernel
for size in range(3, 14, 2):
blurImg = cv2.GaussianBlur(img,(size,size), 0)
plt.figure()
plt.imshow(blurImg)
plt.show()

The original image is:

Original image.

And the blurred images are

3 x 3 Gaussian kernel — 5 x 5 Gaussian kernel
7 x 7 Gaussian kernel — 9 x 9 Gaussian kernel
11 x 11 Gaussian kernel — 13 x 13 Gaussian kernel

Comparison

The Gaussian kernel gives better result in separating frequencies. But, it is slow because of all the calculation involved. On the other hand, mean kernel works in reducing random noise in image space and it is fast. But, it gives worse performance in separating frequency.

We can compromise both kernel by apply mean kernel 4 times on the image. The blurred image would look like the Gaussian kernel.

Here is some blurred images with different kernel size

Original — 3 x 3 mean kernel — 3 x 3 Gaussian kernel
Original — 11 x 11 mean kernel — 11 x 11 Gaussian kernel
Original — 21 x 21 mean kernel — 21 x 21 Gaussian kernel
Original — 51 x 51 mean kernel — 51 x 51 Gaussian kernel
11 x 11 Gaussian kernel for σ = 0 (default), 10, and 25

--

--