Image Filtering Using Convolution in OpenCV
Prerequisites: Basics of OpenCV, Basics of Convolution
In this article, filtering of images using convolution in OpenCV (Open Source Computer Vision) is discussed. In order to use the OpenCV library in Python, the following libraries should be installed as a prerequisite:
- Numpy library
- Matplotlib library
- OpenCV library
To install the following libraries, run the following commands in the command prompt:
pip install opencv-python
pip install numpy
pip install matplotlib
2-D Convolution
The fundamental and the most basic operation in image processing is convolution. This can be achieved by using Kernels. Kernel is a matrix that is generally smaller than the image and the center of the kernel matrix coincides with the pixels.
In a 2D Convolution, the kernel matrix is a 2-dimensional, Square, A x B matrix, where both A and B are odd integers
The position of the output image is obtained by multiplying each value of the matrix with the corresponding value of the image matrix and then summing them up. Based on these operations performed, various effects like blurring and sharpening of the images can be performed.
Identity Kernel
Identity Kernel is the simplest and the most basic kernel operation that could be performed. The output image produced is exactly like the image that is given as the input. It does change the input image. It is a square matrix with the center element equal to 1. All the other elements of the matrix are 0. The code given below demonstrates the operation of Identity Kernel:
Image used :
Python3
import cv2
import numpy as np
img = cv2.imread( 'geeksforgeeks.png' )
id_kernel = np.array([[ 0 , 0 , 0 ],
[ 0 , 1 , 0 ],
[ 0 , 0 , 0 ]])
flt_img = cv2.filter2D(src = img, ddepth = - 1 , kernel = id_kernel)
cv2.imshow( 'Identity' , flt_img)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output :
Blurring
Blurring is defined as averaging the values of the pixel within a neighborhood. This averaging effect makes the sharp edges smooth or blur. The blurring effect is also said to have ‘Low pass filter’ effect because it allows only low frequencies (low rate of change of pixels) to enter through it.
Steps involved in Blurring an Image :
- Choose the size of the kernel over a pixel (p). Larger the size of the kernel, the greater the smoothening effect. This is because, large kernels produce large averaging values with respect to the neighboring pixels and thus, results in a high amount of smoothening.
- Multiply each value of the kernel with the corresponding value of the image matrix.
- Add the results of multiplications and compute the average and obtain the Resultant.
- Lastly, replace the value of pixel (p) with the results obtained.
Box Blur:
This is obtained by uniformly averaging the values in the neighborhood. It is a straightforward blur. It is represented as follows:
The values should be summed up to 1. This is why we divide the matrix by 9. This is called Normalization. Following below is the code to demonstrate the Box Blurring Effect :
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread( 'geeksforgeeks.png' )
plt.imshow(img)
plt.show()
box_blur_ker = np.array([[ 0.1111111 , 0.1111111 , 0.1111111 ],
[ 0.1111111 , 0.1111111 , 0.1111111 ],
[ 0.1111111 , 0.1111111 , 0.1111111 ]])
Box_blur = cv2.filter2D(src = img, ddepth = - 1 , kernel = box_blur_ker)
plt.imshow(Box_blur)
plt.show()
|
Output :
Gaussian Blur:
This filter can be obtained by finding the weighted average of the pixel. The weights of the pixels are calculated on the basis of distance from the center of the kernel. Pixels nearer to the center of the kernel influence more on the weighted average.
The Gaussian filter requires 2 specifications – standard deviation in the X-axis and standard deviation in the Y-axis, represented as sigmaX and sigmaY respectively. If they are both set to 0, the kernel size is taken for calculating the standard deviation.
The code given below demonstrates Gaussian Blur Filter:
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread( 'geeksforgeeks.png' )
plt.imshow(img)
plt.show()
gaussian_blur = cv2.GaussianBlur(src = img, ksize = ( 3 , 3 ),sigmaX = 0 , sigmaY = 0 )
plt.imshow(gaussian_blur)
plt.show()
|
Output :
Median Blur:
The Median Blur filtering effect is obtained when every pixel value is replaced with the median value of the image pixels in the neighborhood.
The code given below demonstrates Median Blur:
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread( 'geeksforgeeks.png' )
plt.imshow(img)
plt.show()
median_blur = cv2.medianBlur(src = img, ksize = 9 )
plt.imshow(median_blur)
plt.show()
|
Output :
Sharpening
Image sharpening helps in enhancing the edges and making them crisp. This filter helps in sharpening the edges and making the image look prominent. The features in the image look distinctive on using this filter.
We use a custom 2D kernel in order to apply this filtering technique. The following kernel can be used for sharpening the image:
The Code given below demonstrates the usage of sharpening filter:
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread( 'geeksforgeeks.png' )
plt.imshow(img)
plt.show()
sharp_kernel = np.array([[ 0 , - 1 , 0 ],
[ - 1 , 5 , - 1 ],
[ 0 , - 1 , 0 ]])
sharp_img = cv2.filter2D(src = img, ddepth = - 1 , kernel = sharp_kernel)
plt.imshow(sharp_img)
plt.show()
|
Output :
Emboss
Forming a 3D design that pops out of the surface is called Emboss. It replaces the pixel with a shadow or a highlight.
The following kernel matrix can be used to apply to emboss filter:
The following code demonstrates the application of the embossing filter:
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread( 'geeksforgeeks.png' )
plt.imshow(img)
plt.show()
emboss_kernel = np.array([[ - 1 , 0 , 0 ],
[ 0 , 0 , 0 ],
[ 0 , 0 , 1 ]])
emboss_img = cv2.filter2D(src = img, ddepth = - 1 , kernel = emboss_kernel)
plt.imshow(emboss_img)
plt.show()
|
Output :
Last Updated :
16 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...