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

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.