Updated April 10, 2023
Introduction to OpenCV filter2d
While dealing with images in image processing, very often there arises a necessity to modify or enhance the images, in such cases, we make use of filters using which certain features of the image can be improved or can be removed and there are various types of filters like low pass filters, high pass filters, etc. and OpenCV provides a filtering function called filter2d() using which an image can be convolved with a kernel that can cause blurring of images, sharpening of images, detection of edges in the images, etc. and the filter2d() function returns an image after blurring it or removing the noise.
The syntax to define filter2d() function in OpenCV is as follows:
filter2d(source_image, depth, kernel)
where source_image is the image on which the filtering operation must be performed,
depth is an integer variable that specifies the depth in an output image,
The kernel is a matrix that represents the convolution kernel.
Working of filter2d() function in OpenCV
- The technique of improving certain features in an image or removing certain features from an image is called filtering in OpenCV.
- The process of filtering an image enables blurring of images, sharpening of images, detection of edges in the images, etc.
- OpenCV provides a function called filter2d() function to perform filtering of images.
- The filter2d() function convolves a kernel with an image.
- Convolution is multiplying two arrays consisting of numbers having the same dimensions to produce a third array consisting of numbers having the same dimensions.
- A kernel is a small matrix used for the blurring of images, sharpening of images, detection of edges in the images, etc. in image processing.
- The filter2d() function works by placing the kernel on all the pixels and then finding the average of all the pixels to make it the central pixel value and the process repeats for all the pixels in the image.
- The filter2d() function returns an image after enhancing certain features in the image or after removing certain features from the image.
Examples of OpenCV filter2d
Here are the following examples mention below
Example #1
OpenCV program in python to demonstrate filter2d() function to blur the given image and display the blurred image as the output on the screen:
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be blurred using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/tree.jpg')
#defining the matrix for kernel to apply filter2d() function on the image to blur the image
kernelmatrix = np.ones((5,5),np.float32)/25
#applying filter2d() function on the image to blur the image and display it as the output on the screen
resultimage = cv.filter2D(imageread,-1,kernelmatrix)
cv.imshow('Filtered_image',resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be blurred using the imread() function. Then we are defining the matrix for the kernel to apply filter2d() function on the image to blur the image. Then we are applying the filter2d() function on the image to blur the image and display it as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate filter2d() function to blur the given image and display the blurred image as the output on the screen:
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be blurred using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/plane.jpg')
#defining the matrix for kernel to apply filter2d() function on the image to blur the image
kernelmatrix = np.ones((5,5),np.float32)/25
#applying filter2d() function on the image to blur the image and display it as the output on the screen
resultimage = cv.filter2D(imageread,-1,kernelmatrix)
cv.imshow('Filtered_image',resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be blurred using the imread() function. Then we are defining the matrix for the kernel to apply filter2d() function on the image to blur the image. Then we are applying the filter2d() function on the image to blur the image and display it as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate filter2d() function to blur the given image and display the blurred image as the output on the screen:
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be blurred using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/educba.jpg')
#defining the matrix for kernel to apply filter2d() function on the image to blur the image
kernelmatrix = np.ones((5,5),np.float32)/25
#applying filter2d() function on the image to blur the image and display it as the output on the screen
resultimage = cv.filter2D(imageread,-1,kernelmatrix)
cv.imshow('Filtered_image',resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be blurred using the imread() function. Then we are defining the matrix for the kernel to apply filter2d() function on the image to blur the image. Then we are applying the filter2d() function on the image to blur the image and display it as the output on the screen. The output is shown in the snapshot above.
Example #4
OpenCV program in python to demonstrate filter2d() function to blur the given image and display the blurred image as the output on the screen:
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be blurred using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/car.jpg')
#defining the matrix for kernel to apply filter2d() function on the image to blur the image
kernelmatrix = np.ones((5,5),np.float32)/25
#applying filter2d() function on the image to blur the image and display it as the output on the screen
resultimage = cv.filter2D(imageread,-1,kernelmatrix)
cv.imshow('Filtered_image',resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be blurred using the imread() function. Then we are defining the matrix for the kernel to apply filter2d() function on the image to blur the image. Then we are applying the filter2d() function on the image to blur the image and display it as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV filter2d” was beneficial to you. You can view EDUCBA’s recommended articles for more information.