Noise Removal From Image Using Python OpenCV¶
In this blog we will learn if we are having some image with noise content in it, then how we can use Python OpenCV to remove the noise from the image.
## Basic Concept of Noise Removal
This kind of operation in image processing terminology is called filtering.
In this blog we will learn how to apply the most basic filter known as mean filtering. There ae many advanced filtering algorithms but to start with we will get learner to begin with most basoc filter for better understanding of basic concept.
# Mean Filtering
Mean filtering algorithm is based on sliding window concept. For each of the image pixel there associated is a two dimensional window which would have the adjacent pixels surounding the current pixel in consideration.
To understand let us consider the window size to be 3 X 3.
So, for instance the current pixel in consideration has value 100 and the sliding window of size 3 X 3 having surrounding pixel values as follows:
[ [ 105 , 110 , 108] ,
[ 115 , 100 , 98] ,
[ 109 , 110 , 117] ]
In the above sliding window the middle value 100 is the value of pixel in consideration.
So, the mean filtering algorithm will replace the value 100 by sliding window mean which is as follows :
Mean = ( 105 + 110 + 108 + 115 + 100 + 98 + 109 + 110 + 117 ) / 9 = 108
So, we will replace the value 100 by 108. So, new window would now looks like as follows:
[ [ 105 , 110 , 108] ,
[ 115 , 108 , 98] ,
[ 109 , 110 , 117] ]
This process is repeated fro each pixel in the Image.
# OpenCV Python Code
Below are the initial steps to write Python OpenCV code:
(1) Read the colored File in a varibale
(2) Convert teh colored Image in to Grayscale Image so that mena filtering can be applied to the same
(3) Define the size of sliding window in two variables. Since the size of sliding window is two dimensioanl so we will take two varibales to store the size.
import cv2
import numpy as np
from matplotlib import pyplot as plt
color_image = cv2.imread('C:\\My\\Video\\lena_color_512_noisy.bmp')
gray_img = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
sliding_window_size_x = 5
sliding_window_size_y = 5
Next steps in the code is to declare a marix using numpy of size of the sliding window taken in consideration. This matrix would serve as an input for the mean filtering function of OpenCV.
Mean filtering function “filter2D” will return the noise filtered cleaned grayscale image.
mean_filter_kernel = np.ones((sliding_window_size_x,sliding_window_size_y),np.float32)/(sliding_window_size_x*sliding_window_size_y)
filtered_image = cv2.filter2D(gray_img,-1,mean_filter_kernel)
Next step is to view both Original Image with Noise and Noise removed Filtered Image side by side by using Matplotlib python library
plt.subplot(121),plt.imshow(gray_img),plt.title('Original Noisy Image')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(filtered_image),plt.title('Filtered Image')
plt.xticks([]), plt.yticks([])
plt.show()
For Corporate Training & Consulting please contact at info@instrovate.com or call / whatsapp at +91 74289 52788