Working image smoothing with the averaging method in OpenCV

This recipe explains how does image smoothing works with the averaging method. Average smoothing calculates the average of all the pixels in the kernel area and replaces the central element with the average.

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

In this recipe, let us understand what image smoothing is and how does it work with averaging method.

Explore the Real-World Applications of Recommender Systems

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 the Average method

If we observe the input image, we can see that the image is noisy with a lot of unwanted dots and disturbances. Image smoothing is generally used to remove this noise from the image. There are various methods available to perform image smoothing, and smoothing by average is the simplest among them.

Average smoothing calculates the average of all the pixels in the kernel area and replaces the central element with the average.  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. This method is sometimes called homogenous smoothing since it assigns equal weights to all the pixels in the kernel area. Let us understand how it works by implementing it using OpenCV.

The cv2.blur() function does the job of average smoothing. It takes two mandatory parameters, and they are

  • src: The image which is to be smoothened
  • ksize: The dimension of the kernel

Conventionally, the kernel dimension is chosen to be 3 x 3 or 5 x 5, but it is totally up to the programmer to select the optimal kernel dimension.

blur = cv2.blur(image,(3,3))

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',"Average 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:

average_smoothing.png

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

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

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.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

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 .

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.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.