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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

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.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

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.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.