Image Filtering Using Convolution in OpenCV
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
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 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 :
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.
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 :
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:
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:
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:
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: