What is a bilateral filter?

The bilateral filter is an advanced image-filtering technique that surpasses the capabilities of conventional pixel intensity-based filters. It serves as an edge-preserving and noise-reducing smoothing filter tailored for images, distinguished by its nonlinear characteristics. This unique methodology enables the bilateral filter to diminish noise while safeguarding edges and intricate details effectively.

From a mathematical perspective, the bilateral filter calculates a weighted average of neighboring pixels. These weights are determined by two key factors: the spatial distance between the central pixel and its neighboring pixels, and the disparity in pixel intensities. This dual consideration propels the bilateral filter into the realm of exceptional noise reduction, minimizing the risk of blurring. Its mathematical representation is as follows:

  • B(p)B(p) is the filtered value of the central pixel pp in the output image.

  • W(p)W(p) is a normalization factor that ensures the weighted average is properly normalized. NN represents the neighborhood of pixels around pp within a certain diameter dd.

  • NN represents the neighborhood of pixels around pp within a certain diameter dd.

  • The qq value iterates over the pixels in the neighborhood N.N.

  • I(Q)I(Q)is the intensity value of the pixel at the location qq in the input image.

  • Gs(pq,σs)G_s (||p-q||, \sigma_s) is the spatial Gaussian function that determines the spatial weight based on the Euclidean distance between pp and qq, with σs\sigma_s controlling the spread of the spatial filter.

  • Gr(I(p)I(q),σr)G_r (|I(p)-I(q)|,\sigma_r) is the range Gaussian function that calculates the intensity weight based on the absolute intensity difference between the central pixel pp and its neighbor qq, with σr\sigma_r controlling the strength of the intensity filter.

Use cases and applications

The versatility of the bilateral filter has led to its widespread use across various applications:

  • Image denoising: When images are captured under challenging conditions, noise can often creep in. A bilateral filter is a reliable tool for removing noise while ensuring that critical features are untouched.

  • Tone mapping: In the realm of high dynamic range (HDR) imaging, maintaining the balance between bright and dark areas is crucial. The bilateral filter aids in tone mapping by enhancing contrast and retaining the nuances in both high and low-intensity regions.

  • Texture enhancement: Images containing intricate textures, such as fabrics, landscapes, or artworks, benefit greatly from the bilateral filter’s ability to highlight and refine these textures without sacrificing other details.

Coding example

Let’s see an example below:

import cv2 as cv
from matplotlib import pyplot as plt
# Load an image
img = cv.imread('/Lena_.png')
blur = cv.bilateralFilter(img,9,75,75)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

Explanation

  • Line 4: This line reads the image Lena_.png from the root directory and stores it in the variable img.

  • Line 5: This line applies a bilateral filter to the loaded image. The bilateral filter is a non-linear filter that preserves edges while reducing noise. The parameters are as follows:

    • img: This is the input image to be filtered.

    • 9: The first argument is the diameter of the pixel neighborhood. In this case, it’s set to 9. It defines the size of the area around each pixel that will be considered when performing the filtering operation. A larger value will consider a larger neighborhood, which can result in a stronger smoothing effect but might blur the image more.

    • 75: The second argument, sigmaColor, represents the standard deviation for the color space. It controls how different colors are considered within the neighborhood. A smaller value will make the filter more sensitive to color differences, while a larger value will make it less sensitive. A value of 75 indicates a relatively large standard deviation, meaning that the filter will take into account a wider range of color values.

    • 75: The third argument, sigmaSpace, is the standard deviation for the coordinate space. It controls how different pixel positions are considered within the neighborhood. Similar to sigmaColor, a smaller value will make the filter more sensitive to spatial differences, while a larger value will make it less sensitive. Again, a value of 75 indicates a relatively large standard deviation, meaning that the filter will consider a wider spatial neighborhood.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved