Bilateral Filtering in Python

Bilateral Filtering in Python

  1. What Is Bilateral Filtering
  2. Steps to Perform Bilateral Filtering in Python
  3. the bilateralFilter() Function in Python
  4. Use the bilateralFilter() Function to Perform Bilateral Filtering in Python

Filtering is used to process images in Computer Vision applications. This article will discuss the implementation of bilateral filtering in Python using the OpenCV module.

What Is Bilateral Filtering

Bilateral filtering is a smoothing filtering technique. It is a non-linear and noise-reducing filter that replaces each pixel value with the weighted average pixel value of the neighbors.

Bilateral filtering is also called edge-preserving filtering as it doesn’t average the pixel across edges.

Steps to Perform Bilateral Filtering in Python

To perform bilateral filtering, we mainly perform four tasks.

  1. We replace each pixel in the image with the weighted average of its neighbors.
  2. Each neighbor’s weightage is decided by its distance from the current pixel. We assign each pixel a weight where the nearest pixels get the highest weightage, and distant pixels are assigned the lowest weight.

To perform this task, we use a spatial parameter.

  1. The neighbor’s weightage also depends on the difference in intensity of the pixels. Pixels with similar intensity to the current pixel are assigned more weight, whereas pixels with large intensity differences are assigned lesser weights.

To perform this task, we use a range parameter.

  1. By increasing the spatial parameter, you can smoothen the larger features of the image. On the other hand, if you increase the range parameter, bilateral filtering behaves as Gaussian filtering.

the bilateralFilter() Function in Python

We can perform bilateral filtering in Python using the OpenCV module using the bilateralFilter() function. The syntax for bilateralFilter() function is as follows.

bilateralFilter(src, d, sigmaColor, sigmaSpace, borderType)

Here,

  • The parameter src takes the source image that has to be processed as an input argument.
  • The parameter d takes the diameter of the neighborhood in which the pixels are to be considered while filtering.
  • The parameter sigmaColor is the value of the filter sigma in the colorspace. Having a higher value of sigmaColor means that the colors farther apart in the color space are considered while filtering.

The parameter sigmaColor should contain a value in the range of sigmaSpace.

  • The parameter sigmaSpace denotes the value of sigma in the spatial domain. A higher value of sigmaSpace means that the pixels farther away from the current pixel are considered while filtering.

The parameter sigmaSpace should contain a value in the range of sigmaColor.

  • The parameter borderType is used to define a mode for extrapolating the pixels outside the image while filtering the pixels in image boundaries.

Use the bilateralFilter() Function to Perform Bilateral Filtering in Python

The following are the steps to perform bilateral filtering in Python.

  • First, we will import cv2.
  • Next, we will open an image using the imread() function, which takes the file path of an image as its input argument and returns an array representing the image.
  • We will store the array in a variable img.
  • After loading the image, we will use the bilateralFilter() function to perform bilateral functioning in Python. After execution, the bilateralFilter() function returns an array containing the processed image.
  • After obtaining the processed image, we will save it to the file system using the imwrite() function, which takes a string containing the filename of the output file as its first input argument and the array containing the processed image as its second input argument. After executing the function, the file is saved to the file system.

Below is the image we will use to perform bilateral filtering in Python.

pattern

The following is the code to perform bilateral filtering in Python.

import cv2

img = cv2.imread("pattern.jpg")
output_image = cv2.bilateralFilter(img, 15, 100, 100)
cv2.imwrite("processed_image.jpg", output_image)

Here is the output image after performing bilateral filtering on the input image:

bilateral filter python processed image

In the given image, you can observe that the features of the strips have been blurred in the output image. This is because the neighboring pixels of each pixel are considered while creating the output pixel.

Averaging of the pixels gives a blurring effect, and the features are blurred.

When compared to Gaussian filtering, bilateral filtering preserves the edges. Therefore, while performing smoothing operations, you can always use bilateral filtering if you need to preserve the edges in your image.