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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

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

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.