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

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

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

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.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

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.