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.
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.
There are several types of low-pass filter used for smoothing the image. Let’s discuss the two most used filters.
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 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.
Let’s see an example of blurring the image using the gaussian 3x3 filter below.
import cv2import matplotlib.pyplot as pltimg = 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 cv2import matplotlib.pyplot as pltimg = 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')
import cv2
imports the OpenCV library into the python file.import matplotlib.pyplot as plt
imports the matplotlib library into the python file.cv2.imread()
reads the image and returns the image data in img
.cv2.GaussianBlur()
.Let us now use a Mean filter:
import cv2import matplotlib.pyplot as pltimg = 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')
import cv2
imports the OpenCV library into the python file.import matplotlib.pyplot as plt
imports the matplotlib library into the python file.cv2.imread()
reads the image and returns the image data in img
.cv2.blur()
.