How to Apply Custom Filters to Images (2D Convolution) Using OpenCV Python?
Introduction
OpenCV is an open-source computer vision and machine learning software library. It is used for analyzing real-time computer vision and image processing. OpenCV can be used to manipulate images in Python by using various image processing techniques, including custom filters.
Custom filters are used for smoothing, sharpening, and edge detection of images. In this tutorial, we will discuss how to implement custom filters using OpenCV in Python.
What is Image Filtering?
Image filtering is the process of modifying the pixels of an image to highlight or suppress specific image features. Filtering can be done in the spatial domain, which is the image space, or the frequency domain, which is the Fourier transform of the image.
The most commonly used filter in the spatial domain is the convolution filter. Convolution is a mathematical operation that applies a kernel (a small matrix) to an image. The kernel is used to weight the pixels of the image and create a filtered output image.
In this tutorial, we will discuss how to apply custom filters to images using 2D convolution.
2D Convolution
2D convolution is an operation that applies a kernel to an image using the convolution formula. The kernel is a small matrix that moves over the image, and at each position, it multiplies the image pixels with the corresponding kernel values. The sum of the products gives the new value for the output image pixel at that position.
The kernel size is usually small, such as 3×3 or 5×5, and determines the type of filter to be applied. For example, a 3×3 kernel that averages the pixel values is used for smoothing or blurring an image.
The 2D convolution operation can be represented as follows:
output_image[x, y] = sum(kernel[i, j] * input_image[x+i, y+j]
for i in range(kernel.shape[0])
for j in range(kernel.shape[1]))
Applying Custom Filters to Images using OpenCV
OpenCV provides the cv2.filter2D()
function to apply custom filters to images. This function accepts the input image, the data type (depth) of the output image, and the kernel matrix as input. It performs the 2D convolution operation on the input image using the kernel matrix and returns the filtered output image.
In the following example, we will create a 3×3 kernel that sharpens the edges of an image and apply it to a sample image.
import cv2
import numpy as np
# load the sample image
img = cv2.imread('lenna.png')
# create a 3x3 sharpening kernel
kernel = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
# apply the custom filter to the image
output = cv2.filter2D(img, -1, kernel)
# show the input and output images
cv2.imshow('Input Image', img)
cv2.imshow('Output Image', output)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above code, we first load a sample image using the cv2.imread()
function. We then create a 3×3 kernel that sharpens the edges of the image by keeping the central pixel weight more than the surrounding pixels. Finally, we apply the custom filter to the image using the cv2.filter2D()
function and display the input and output images using the cv2.imshow()
function.
Conclusion
Custom filters are used for smoothing, sharpening, or edge detection of images. In this tutorial, we have discussed how to implement custom filters using 2D convolution in OpenCV Python. We use the cv2.filter2D()
function to perform 2D convolution on an image using a kernel matrix and obtain the filtered output image. Image filtering has various applications in computer vision and image processing, and OpenCV provides a vast range of functions to perform such operations on images.