What is bilateral filtering in OpenCV

This recipe explains what is bilateral filtering in OpenCV

Recipe Objective: What is bilateral filtering in OpenCV?

In this recipe, let us understand what image smoothing is and how it works with the Bilateral Smoothing method (also known as bilateral filtering).

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below.

Input image

import numpy as np
import cv2
from matplotlib import pyplot as plt
image = cv2.imread('edgeflower_noisy.jpg')

Step 2: Image smoothing / Image blurring using Bilateral Smoothing

Bilateral smoothing is an edge-preserving denoising technique. As we might have noticed in the other image smoothing techniques, they generally blur out the edges. That does not happen with Bilateral smoothing, and that is why it is called an 'edge-preserving smoothing technique. But how does it preserve the edges?

We already know that in the Gaussian smoothing technique, a weighted sum of all the pixel values in the kernel area is calculated, and the central element of the kernel is replaced with that value. But this is a function of space alone. It is not considered if the pixel lies on edge or not. This is why the Gaussian smoothing technique tends to blur out the boundaries also.

Bilateral filtering or Bilateral smoothing technique overcomes this disadvantage by introducing another Gaussian filter that considers the variation of intensities to preserve the edges.

Bilateral filtering can be implemented in OpenCV using the cv2.bilateralFilter() function, which takes the following parameters

  • src: Image which is to be smoothened
  • d: Dimension of the kernel
  • sigmaColor: Standard deviation that controls the influence of pixels with different intensity values
  • sigmaSpace: Standard deviation that controls the influence of distant pixels

blur = cv2.bilateralFilter(image,9,350,350)

Step 3: Displaying the output

It's time to see and understand how the noise has been eliminated from our image. Let us use matplotlib subplots to display the input and the output image and analyze them.

titles = ['Original Image',"Bilateral Smoothing"]
images = [image,blur]
plt.figure(figsize=(13,5))
for i in range(2):
    plt.subplot(1,2,i+1)
    plt.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.tight_layout()
plt.show()

Output:

Bilateral smoothening

We can see from the above output that the output has less noise and sharpness while the edges remain intact.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.