How to do bilateral filtering in OpenCV

share link

by Dejaswarooba dot icon Updated: Oct 25, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Bilateral filtering helps in image processing tasks to achieve image smoothing. It will preserve edges and details. It is quite distinct from traditional image blurring methods like Gaussian Blurring.


Standard blurring techniques create a uniform smoothing effect. We can do this by applying a low-pass filter kernel to the entire image. Bilateral filtering takes into account both spatial and color information. It is when applying the smoothing filter.    


We can employ the `bilateralFilter` function from the OpenCV library for bilateral filtering. This function operates on an input image and a destination (output) image. Bilateral filtering computes a weighted average for each pixel.

It considers two key factors:   

1. Color Similarity (sigmaColor - Filter sigma):   

It looks at how similar the colors are between the present pixel and its neighboring pixels.   


2. Spatial Proximity (sigmaSpace - Filter sigma):    

It considers the closeness of neighboring pixels. i.e., how close they are in their spatial distance from the current pixel.   


Bilateral filtering combines these two factors. This ensures that we apply the smoothing effect. It helps preserve edges and details in the image. Median blurring computes the median value of pixel intensities within a kernel area. Unlike that, bilateral filtering is an advanced filter. It takes into account pixel differences and values in the current pixel's vicinity. It is useful in scenarios where there is a large intensity variation in the image. It is also useful when we need to retain similar intensities.   


The bilateral smoothing filter uses a normalization factor. It considers the pixel values within a kernel. This works in contrast to a simple average filter like a box filter or a normalized box filter. This function helps with noise removal in real-time applications. It helps ensure unwanted noise doesn't interfere with the task at hand. It employs for edge-preserving smoothing and enhancing features. It helps keep important details intact.  

 

The cv2.bilateralFilter function is a tool for performing bilateral filtering on images. It is particularly effective in reducing noise while retaining important image structures. Here's an overview of the cv2.bilateralFilter function:   

Syntax:   

cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace, dst=None, borderType=None)   

Parameters:   

  • src: This is the source image that you want to filter.   
  • d: The diameter of each pixel neighborhood. It defines the range over which the filter operates. A larger d value includes pixels that are farther apart in the filtering process.   
  • sigmaColor: This parameter controls the influence of color similarity on the filtering. A larger sigmaColor value means that we consider pixels with similar color values.   
  • sigmaSpace: This parameter controls the influence of spatial proximity on the filtering. A larger sigmaSpace value means pixels closer to the center contribute to filtering.   
  • dst (optional): This is the output image where we store the filtered result. If not specified, the function creates a new image for the result.   
  • borderType (optional): It specifies how to handle border pixels during filtering. Common values include cv2.BORDER_DEFAULT, cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, and others.