Blurring Images (Smoothing)
Blurring is also referred to as smoothing. Blurring an image will produce an image with low noise. It actually removes some high frequency contents such as edges, noise etc. from the image. It can be used as a preprocessing step for various image processing tasks. OpenCV provides four main types of blurring (smoothing) techniques which are described in details in following sections.
Blurring with Averaging
The averaging is a simplest blurring technique. It takes the average of all the pixels in the neighborhood and replace the central pixel with this value. This helps overcome noise and also hide some low level details to give an impression of blurring.
In order to apply the average filter, we need to select a kernel of certain size n x m. We generally use an odd number as values for with and height for the kernel. Moreover, the kernel needs to be normalized before applying to the image to make sure that the pixel values do not exceed the input image’s range of possible value. For example, we normalized the following 3 x 3 kernel by dividing it with the sum of all values in the kernel (K).
\[K = { 1 \over 9} \left[\begin{array}{rrr}
1 & 1 & 1 \\
1 & 1 & 1 \\
1 & 1 & 1
\end{array}\right] \]
In OpenCV simple averagine is implemented using blur() function. The signature of this function is as shown below:
cv2.blur(src, ksize[, anchor[, borderType]]])
Parameter |
Description |
src
|
input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
|
ksize
|
Neighborhood size. It means how many pixels around the central pixels will be used to perform blurring of central pixel.
|
anchor
|
Anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
|
borderType
|
Border mode used to extrapolate pixels outside of the image.
|
It returns the blurred image of the same size and type as src. Code 1-1 shows how to use blur() function to smooth (blur) and image.
Code 1-1
1
2
3
4
5
6
7
8
9
10
11
12
|
import cv2
import numpy as np
try:
path = r'F:\img\lena.jpg'
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
imgBlur = cv2.blur(img,(5,5))
cv2.imshow('Original Image',(img))
cv2.imshow('Blurred Image',imgBlur)
cv2.waitKey(0)
cv2.destroyAllWindows()
except Exception as e:
print(str(e))
|
Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 3: This lines defines a try block. The exception block (Line 11-12) is associated with this block. If any exception occurs, it throws it and code in exception block is executed.
Line 4: Define path of the image to read;
Line 5: The image is read. Note that we are reading a grayscale image (see 2nd parameter).
Line 6: Using OpenCV’s blur() function to blur the input image. We used the kernel size of (5,5). For the rest of the parameters, their default values are used as they are optional.
Line 7-8: It displays both blurred and original image in a separate window.
Line 9: waitKey(0) function makes the window wait until user presses any keyboard button.
Line 10: Object of all the windows opened will be destroyed and closed.
The output obtained for the code shown above is shown in Figure 1-1.
Figure 1-1: Original image(left), blurred image (right).