How to apply custom filters to images (2D convolution) using OpenCV Python?


In this tutorial, we will see how to apply two different low-pass filters to smooth (remove noise from) the image. The two filters are filter2D and boxFilter. These filters are 2D filters in space.

Applying 2D filters to images is also known as the "2D Convolution operation". These filters are commonly referred to as averaging filters.

The main disadvantage of these filters is that they also smooth the edges in the image. If you don't want to smooth the edges, you can apply a "bilateral filter". A bilateral filter operation preserves the edges.

Syntax

Following are the syntaxes of Filter2D and BoxFilter

cv2.filter2D(img, ddepth, kernel)
cv2.boxFilter(img, ddepth, ksize)

Parameters

  • img − The input image on which the filter operation to be applied.

  • ddepth − The desired depth of the output image. If "depth = -1", it returns the output image with the same depth as the input image.

  • kernel − Convolution kernel. In filter2D, we pass the kernel as a numpy array.

  • ksize − The kernel size. In boxFilter, we pass only the kernel size as a tuple.

Steps

To perform bilateral filter operation, you can follow the steps given below −

Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.

import cv2

Read the input image and convert it to a grayscale image

img = cv2.imread('vehicle.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Define kernel in case to apply filter2D.

Define kernel in case to apply filter2D.
kernel = np.ones((5,5),np.float32)/25

Apply filter2D or boxFilter filtering on the input image. We pass ddepth, kernel, or ksize as arguments to the filter function.

result = cv2.filter2D(img,-1,kernel)
result = cv2.boxFilter(img,-1,(5,5))

Display the filtered image.

cv2.imshow('Filter 2D Image', result)
cv2.waitKey(0)
cv2.destroyAllWindows())

Let's see the examples to perform filter2D and boxFilter operation on the input image.

We will use the following image as the Input file in all the examples below.

Example 1

In this Python program, we apply filter2D to the input image using a 5×5 kernel.

# import required libraries import cv2 import numpy as np # read the input image img = cv2.imread('vehicle.jpg') # convert the image to grayscale img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # define a 5x5 kernel kernel = np.ones((5,5),np.float32)/25 # Apply the averaging filter result = cv2.filter2D(img,-1,kernel) # Display the output image cv2.imshow("Filter 2D Image ", result) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When we execute the above code, it will produce the following output window −

The above output image is obtained after applying an averaging filter using cv2.filter2D().

Example 2

In the Python example below, we apply boxFilter operation to the input image using a 5×5 kernel.

# Import required library import cv2 # Read the input image img = cv2.imread('vehicle.jpg') # convert the image to grayscale img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply boxFilter to the input image result = cv2.boxFilter(img,-1,(5,5)) # display the image after filter operation cv2.imshow("Box Filter Image", result) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When we execute the above code, it will produce the following output window −

The above output image is obtained after applying an averaging filter using cv2.boxFilter().

Updated on: 27-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements