Working of the image smoothing in Gaussian Blurring OpenCV

This recipe explains how does image smoothing works with the Gaussian Blurring method. Gaussian blurring is one of the Image smoothing techniques used to remove the noise from the image

Recipe Objective: How does the image smoothing work with the Gaussian Blurring method in OpenCV?

In this recipe, let us understand what image smoothing is and how it works with the Gaussian Blurring method.

Explore Fascinating Image Processing Project Ideas With Source Code

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.

projectpro_noise_20.jpg 

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

Step 2: Image smoothing / Image blurring using Gaussian blurring

As we can see, the image that we have is boisterous with unwanted dots and disturbances. Image smoothing is generally used to remove this noise from the image. Gaussian blurring is one of the Image smoothing techniques used to remove the noise from the image. The Gaussian blurring technique uses a Gaussian filter which assigns different weights to the pixels inside each kernel area and replaces the central element with the gaussian weighted sum. A kernel is nothing but a small matrix used for sharpening, blurring, embossing, edge detection, and much more. It is also sometimes called a convolution matrix, a mask, or a filter.  In each kernel, the pixels located in the center generally tend to have a higher weight, and it reduces in both x and y directions as we keep approaching the edge of the kernel.

The Gaussian Blurring method can be implemented using the cv2.GaussianBlur() takes the following input parameters

  • src: The image to be smoothened
  • ksize: The kernel dimension.
  • sigmaX: Standard deviation in the X direction
  • sigmaY: (Optional) Standard deviation in the Y direction

The kernel dimension should be positive and odd. sigmaX specifies the standard deviation in the X-direction. If only sigmaX is set, then sigmaY also takes the same value. If sigmaX is set as 0 then, both sigmaX and sigmaY are automatically calculated from the kernel size.

gauss_blur = cv2.GaussianBlur(image,(5,5),0)

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',"Gaussian smoothing"]
images = [image,gauss_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:

gauss_smooth.png

We can see from the above output that the output has less noise and sharpness.

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

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

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.