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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Data Analysis of Working Capital Management using Tableau
In this Data Analysis Project using Tableau, you will focus on optimizing working capital by analyzing receivables and payables data using Tableau and build actionable dashboards.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

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.

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.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

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.

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.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

OSZAR »