Working of the image smoothing with median method in OpenCV

This recipe helps you to understand the working of image smoothing with the median method in OpenCV. Image smoothing is a noise elimination technique used to remove unwanted dots and disturbances in the image.

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

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

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_50.jpg 

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

Step 2: Image smoothing / Image blurring using Median Smoothing

Image smoothing is a noise elimination technique used to remove unwanted dots and disturbances in the image. As we can see, the image that we have is boisterous. This noise can be eliminated using the Image smoothing technique. There are various ways to perform Image Smoothing, and Median Smoothing is one of them.

In median soothing, we calculate the median of all the pixels under the kernel area and replace the central element with the median. 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. Median smoothing is highly effective in eliminating salt-and-pepper noise ( Salt-and-pepper noise, sometimes called impulse noise, is the discrepancies caused in the image due to sudden or sharp disturbances. The best example for such a noisy image is the input image that we have ) in any image. Another reason median smoothing works effectively is because it does not replace the central element with any new value. Instead it replaces the central element with one of the values already present inside the kernel area and hence it reduces noise effectively.

Median smoothing can be implemented in OpenCV using cv2.medianBlur() function which takes the following parameters

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

The kernel dimension is a positive odd integer value.

median = cv2.medianBlur(image,5)

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',"Median Smoothing"]
images = [image,median]
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:

Median smoothening

We can clearly 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

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

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.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy