Spatial Filters - Averaging filter and Median filter in Image Processing
Last Updated :
03 Jan, 2023
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered to be added in size so that it has a specific center pixel. This mask is moved on the image such that the center of the mask traverses all image pixels.
In this article, we are going to cover the following topics -
- To write a program in Python to implement spatial domain averaging filter and to observe its blurring effect on the image without using inbuilt functions
- To write a program in Python to implement spatial domain median filter to remove salt and pepper noise without using inbuilt functions
Theory
- Neighborhood processing in spatial domain: Here, to modify one pixel, we consider values of the immediate neighboring pixels also. For this purpose, 3X3, 5X5, or 7X7 neighborhood mask can be considered. An example of a 3X3 mask is shown below.
f(x-1, y-1) f(x-1, y) f(x-1, y+1)
f(x, y-1) f(x, y) f(x, y+1)
f(x+1, y-1) f(x+1, y) f(x+1, y+1)
- Low Pass filtering: It is also known as the smoothing filter. It removes the high-frequency content from the image. It is also used to blur an image. A low pass averaging filter mask is as shown.
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9
- High Pass Filtering: It eliminates low-frequency regions while retaining or enhancing the high-frequency components. A high pass filtering mask is as shown.
-1/9 -1/9 -1/9
-1/9 8/9 -1/9
-1/9 -1/9 -1/9
- Median Filtering: It is also known as nonlinear filtering. It is used to eliminate salt and pepper noise. Here the pixel value is replaced by the median value of the neighboring pixel.
Below is the implementation.
Input Image:
Averaging Filter:
Python3
# Low Pass SPatial Domain Filtering
# to observe the blurring effect
import cv2
import numpy as np
# Read the image
img = cv2.imread('sample.png', 0)
# Obtain number of rows and columns
# of the image
m, n = img.shape
# Develop Averaging filter(3, 3) mask
mask = np.ones([3, 3], dtype = int)
mask = mask / 9
# Convolve the 3X3 mask over the image
img_new = np.zeros([m, n])
for i in range(1, m-1):
for j in range(1, n-1):
temp = img[i-1, j-1]*mask[0, 0]+img[i-1, j]*mask[0, 1]+img[i-1, j + 1]*mask[0, 2]+img[i, j-1]*mask[1, 0]+ img[i, j]*mask[1, 1]+img[i, j + 1]*mask[1, 2]+img[i + 1, j-1]*mask[2, 0]+img[i + 1, j]*mask[2, 1]+img[i + 1, j + 1]*mask[2, 2]
img_new[i, j]= temp
img_new = img_new.astype(np.uint8)
cv2.imwrite('blurred.tif', img_new)
Output:
In the above example, it is observed that the filtered image is slightly blurred. If we increase the size of the averaging mask, more blurring can be obtained.
Median Filtering:
Python3
# Median Spatial Domain Filtering
import cv2
import numpy as np
# Read the image
img_noisy1 = cv2.imread('sample.png', 0)
# Obtain the number of rows and columns
# of the image
m, n = img_noisy1.shape
# Traverse the image. For every 3X3 area,
# find the median of the pixels and
# replace the center pixel by the median
img_new1 = np.zeros([m, n])
for i in range(1, m-1):
for j in range(1, n-1):
temp = [img_noisy1[i-1, j-1],
img_noisy1[i-1, j],
img_noisy1[i-1, j + 1],
img_noisy1[i, j-1],
img_noisy1[i, j],
img_noisy1[i, j + 1],
img_noisy1[i + 1, j-1],
img_noisy1[i + 1, j],
img_noisy1[i + 1, j + 1]]
temp = sorted(temp)
img_new1[i, j]= temp[4]
img_new1 = img_new1.astype(np.uint8)
cv2.imwrite('new_median_filtered.png', img_new1)
Output:
In the above example, we can see that the median filtered image is considerably enhanced with hardly any salt and pepper noise in it.
Similar Reads
Feature Descriptor in Image Processing In image processing, a feature descriptor is a representation of an image region or key point that captures relevant information about the image content. In this article, we are going to discuss one of the image processing algorithms i.e. Feature Descriptor Image processingImage processing is a comp
5 min read
Denoising techniques in digital image processing using MATLAB Denoising is the process of removing or reducing the noise or artifacts from the image. Denoising makes the image more clear and enables us to see finer details in the image clearly. It does not change the brightness or contrast of the image directly, but due to the removal of artifacts, the final i
6 min read
Image Edge Detection Operators in Digital Image Processing In digital image processing edges are places where the brightness or color in an image changes a lot. These changes usually happen at borders of objects. Detecting edges helps us understand the shape, size and location of different parts in an image. Edge detection is used to recognize patterns, und
5 min read
Statistical Functions in Python | Set 1 (Averages and Measure of Central Location) Python has the ability to manipulate some statistical data and calculate results of various statistical operations using the file "statistics", useful in domain of mathematics. Important Average and measure of central location functions : 1. mean() :- This function returns the mean or average of the
3 min read
Finding Mean, Median, Mode in Python without libraries In this article, we will learn how to calculate Mean, Median, and Mode with Python without using external libraries. 1. Mean: The mean is the average of all numbers and is sometimes called the arithmetic mean. This code calculates Mean or Average of a list containing numbers: We define a list of num
6 min read