You are reading the article Learn The Concept Of Filtering In Opencv updated in September 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn The Concept Of Filtering In Opencv
Introduction to OpenCV filter2dWhile 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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 filter2dHere are the following examples mention below
Example #1OpenCV 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 #2Code:
#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 #3OpenCV 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 #4OpenCV 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 ArticlesWe hope that this EDUCBA information on “OpenCV filter2d” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Learn The Concept Of Filtering In Opencv
Update the detailed information about Learn The Concept Of Filtering In Opencv on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!