What is image blurring?

Blurring an image is a process that makes the image less sharp and reduces its level of detail. It distorts the detail of an image which makes it less clear. The most common use of image blurriness is to remove noise from the image; the other is to get the most detailed part of the image and smooth out the less detailed ones. Image blur is also called image smoothing.

How to blur an image

We can blur an image using different low-pass blurring filters. These low-pass filters decrease the unevenness between the pixels by averaging nearby pixels. They tend to retain low-frequency information while decreasing high-frequency information in an image.

Let’s see an illustration below of applying a mean filter on the image matrix.

A 3x3 Mean filter
1 of 6

Types of filters

There are several types of low-pass filter used for smoothing the image. Let’s discuss the two most used filters.

  • Mean filter
  • Gaussian filter

Mean filter

The mean filter replaces each pixel value with the average value of its neighbors, including itself. It reduces the amount of intensity variation between one pixel and the next. It is based on the kernel, which represents the shape and size of the filter while calculating the mean.

Gaussian filter

Gaussian filters work by using a 2D distribution as the point spread function, which can be achieved by convolving the 2D Gaussian distribution function with the image. For this, a discrete approximation for the Gaussian function is needed. Let’s see the equation below.

G(x,y)=12πσ2e(x2+y2)(2σ2)G(x,y)=\dfrac{1}{2\pi\sigma^2}e^{-\dfrac{(x^2+y^2)}{(2\sigma^2)}}

Let’s see an example of blurring the image using the gaussian 3x3 filter below.

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('/image_1.jpg', 1)
blur = cv2.GaussianBlur(img,(3,3),0)
plt.subplot(121),plt.imshow(img),plt.title('Original image')
plt.subplot(122),plt.imshow(blur),plt.title('Blurred image')

As we can see, the gaussian filter blurred the image, but if we increase the size of the filter, the effect would be more substantial. So let’s see an example below.

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('/image_1.jpg', 1)
blur = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(121),plt.imshow(img),plt.title('Original image')
plt.subplot(122),plt.imshow(blur),plt.title('Blurred image')

Explanation

  • Line 1: import cv2 imports the OpenCV library into the python file.
  • Line 2: import matplotlib.pyplot as plt imports the matplotlib library into the python file.
  • Line 4: The cv2.imread() reads the image and returns the image data in img.
  • Line 5: Applying a 9x9 gaussian filter on the image by using the built-in function cv2.GaussianBlur().
  • Line 6–7: Plotting the original and blurred image.

Let us now use a Mean filter:

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('/image_1.jpg', 1)
blur = cv2.blur(img,(5,5))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')

Explanation

  • Line 1: import cv2 imports the OpenCV library into the python file.
  • Line 2: import matplotlib.pyplot as plt imports the matplotlib library into the python file.
  • Line 4: The cv2.imread() reads the image and returns the image data in img.
  • Line 5: Applying a 5x5 mean filter on the image by using the built-in function cv2.blur().
  • Line 6–7: Plotting the original and blurred image.
Copyright ©2024 Educative, Inc. All rights reserved