Python OpenCV - Filter2D() Function
Last Updated :
28 May, 2024
In this article, we are going to see about the filter2d() function from OpenCV. In a nutshell, with this function, we can convolve an image with the kernel (typically a 2d matrix) to apply a filter on the images.
Syntax: filter2D (src, dst, ddepth, kernel)
Parameters:
- Src - The source image to apply the filter on.
- Dst – Name of the output image after applying the filter
- Ddepth – Depth of the output image [ -1 will give the output image depth as same as the input image]
- Kernel – The 2d matrix we want the image to convolve with.
Using this function, we can create a convolution between the image and the given kernel for creating filters like smoothing and blurring, sharpening, and edge detection in an image. This function will simply convolute the 2d matrix with the image at pixel level and produce an output image. To understand this concept, we shall first skim through the concept of the kernel.
Kernel: A simple 2d matrix used in convolution or Convolution Matrix or a mask used to blur, sharpen and edge detect an image.
Working of the kernel: So, how this kernel works? Let’s see, we all know that images are represented as pixel values in OpenCV. These pixels are arranged as a matrix to form an image and as we know that a kernel is a simple 2d matrix with specific values in it based on the function of the kernel like if the kernel is used for blurring and sharpening the images are different.
Let us take an example, In this image take the first 3 rows and columns like a matrix and we have a kernel of 3 by 3 matrix. Each of the pixels in the image has a pixel value (i.e. pixel intensity) as shown. Now the convolution is done by multiplying the values of each pixel value with the kernel value in the respective places and adding all the values which were just weighted by the kernel by multiplying and forming one pixel (the center pixel which in this case it is [2,2]). And this method is repeated for the rest of the pixel value matrices in the image.
Working of convolution Note: We have specific kernels [2d convolution matrices] to do specific tasks like blurring, sharpening, and edge detection. And the kernel shown in the image is just an example and not any specific kernel.
Some Common kernels are,
- Identity kernel
- Edge detection kernel
- Sharpening kernel
- Box blurring kernel
- Gaussian blurring kernel
different kernelsWith the use of these kernels, we can form the filtered image by simply convoluting the pixel values of the image and the values in the kernel.
Blur an image with a 2d convolution matrix
We are creating a kernel by NumPy which will look like this and passing the kernel as a parameter of the filter2d function.
np.ones() will create an array of the desired shape filled all with the ones
Blur kernelExample 1: Blur an image with a 2d convolution matrix
Python
# importing the modules needed
import cv2
import numpy as np
# Reading the image
image = cv2.imread('image.png')
# Creating the kernel(2d convolution matrix)
kernel1 = np.ones((5, 5), np.float32)/30
# Applying the filter2D() function
img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel1)
# Shoeing the original and output image
cv2.imshow('Original', image)
cv2.imshow('Kernel Blur', img)
cv2.waitKey()
cv2.destroyAllWindows()
Output:
Blur an image with a 2d convolution matrixExample 2: Edge detect an image with a 2d convolution matrix
We are creating a kernel by NumPy array which will look like this and passing the kernel as a parameter of the filter2d function.
edge detection kernel
Python
# importing the modules needed
import cv2
import numpy as np
# Reading the image
image = cv2.imread('image.png')
# Creating the kernel(2d convolution matrix)
kernel2 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
# Applying the filter2D() function
img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel2)
# Shoeing the original and output image
cv2.imshow('Original', image)
cv2.imshow('Kernel Blur', img)
cv2.waitKey()
cv2.destroyAllWindows()
Output:
Edge detect an image with a 2d convolution matrix
Similar Reads
Python OpenCV - BFMatcher() Function
In this article, we will be going to implement Python OpenCV - BFMatcher() Function. Prerequisites: OpenCV, matplotlib What is BFMatcher() Function? BFMatcher() function is used in feature matching and used to match features in one image with other image. BFMatcher refers to a Brute-force matcher th
3 min read
Python OpenCV - Canny() Function
Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects edges in an ima
3 min read
Python OpenCV - destroyWindow() Function
Python Opencv destroyWindow() function is used to close particular windows. This function takes one parameter that is window name which you want to close or destroy and it doesn't return anything. Syntax: cv2.destroyWindow(window_name) Parameter: window_name: name of the window which you want to des
1 min read
Python OpenCV - distanceTransform() Function
In this article, we are going to learn about the distanceTransform() function of the OpenCV library. To use this function, you will need to have the OpenCV library installed in your Python environment. What is distanceTransform() Function? The OpenCV distanceTransform() function is an image processi
3 min read
Python OpenCV - drawKeypoints() Function
In this article let's discuss the drawKeypoints() function of OpenCV. The distinguishing qualities of an image that make it stand out are referred to as key points in an image. The key points of a particular image let us recognize objects and compare images. Detecting critical spots in a picture may
4 min read
Introduction to OpenCV
OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance. In this article, to understand the basic functionalities of Python OpenCV module, we wi
4 min read
Python OpenCV - destroyAllWindows() Function
Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function. It doesn't take any parameters and doesn't return anything. It is s
2 min read
Python OpenCV - waitKey() Function
waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Examples 1: Disp
1 min read
imreadmulti() in Python Opencv
OpenCV (Open Source Computer Vision) library primarily focuses on image and video processing. To load a single image through OpenCV, imread() function is used. However, to load multiple images in a single object, imreadmulti() function is used. Images within a file are considered as a page. Although
4 min read
Python OpenCV - imencode() Function
Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary librarie
2 min read