Smoothing or Averaging filter in Spatial Domain

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@biggestloser·
0.000 HBD
Smoothing or Averaging filter in Spatial Domain
<center><h1>Spatial Domain</h1></center>
___
Spatial Domain can be broadly said as the image space. The image can be represented in the form of a 2D matrix where each element of the matrix represents pixel intensity. This state of 2D matrices which depict the intensity is called Spatial Domain. Manipulation of the pixel values are done here and the image is processed pixel by pixel.
___
<center><h2>Smoothing Filter</h2></center>
___
Image smoothing is a digital image processing technique that reduces and suppresses image noises. In the spatial domain, neighborhood averaging can generally be used to achieve the purpose of smoothing.
___
<center><h2>Average Filter</h2></center>
___
Average filtering is a method of **smoothing** images by reducing the amount of intensity variation between neighbouring pixels. The average filter works by moving through the image pixel by pixel, replacing each value with the average value of neighbouring pixels, including itself.
___
<center><h2>Program</h2></center>
___
### Importing python libraries
>import cv2
import numpy as np
import matplotlib.pyplot as plt
### Reading the image
>image = cv2.imread('badges.jpg')
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
plt.title('Original Image')
plt.imshow(image);


![image.png](https://images.hive.blog/DQmPJ7MJVJMKGhna8ExxghYHNcPbg5urtnN1Ee7CkW9QUMM/image.png)

### Converting the original image to grayscale
>img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.title('Grayscale image')
plt.imshow(img, cmap='gray', vmin=0, vmax=255);


![image.png](https://images.hive.blog/DQmQpDw4xSmDx1yWb57A39meg1hm4YFMwvHYWyxf7db8x6E/image.png)

### Number of rows and columns of the image
>m, n = img.shape
print(f'Rows: {m},  Columns: {n}')

![image.png](https://images.hive.blog/DQmQzQh5WrGvey9YjpB46RdbMq8BYm7XifhXxnzZfeEDihW/image.png)
### Developing Averaging filter mask of (3,3)
#####  _If the image is bigger in size then you can increase the mask to (5,5) or (7,7)._
>mask = np.ones([3,3], dtype=int)
mask = mask/9
print(mask)

![image.png](https://images.hive.blog/DQmSrEsczmXdkR1hnV1F8hTBxTy4GcCKnsE4fExTSLxLkhh/image.png)
### Convolving the 3X3 mask over the image
>newImage = np.zeros([m, n])
print(newImage)

![image.png](https://images.hive.blog/DQmTyviAcgT7LbzJyz7JxesmGNdv4urmNfAncPgpxPUhWUg/image.png)


![image.png](https://images.hive.blog/DQmQRwHwZviAPP2uEzvqyTfPVZzEkayNwLad59CP28tCfmt/image.png)



![image.png](https://images.hive.blog/DQmeZTtfpkWUNLoGTXdA5BNARqjPq2WFwJv14RYrWuGpZFU/image.png)
___
**Median Filter:** Median filtering is a nonlinear process useful in reducing impulsive, or salt-and-pepper noise.  In a median filter, a window slides along the image, and the median intensity value of the pixels within the window becomes the output intensity of the pixel being processed.
___
### Traverse the image. For every 3X3 area, The median of the pixels are found and center pixel of the median is replaced by it.


![image.png](https://images.hive.blog/DQmRAH1csYLK5iHjTipzGeR788ZXFJYDQZdpdQpsNyH85ZS/image.png)


![image.png](https://images.hive.blog/DQmUzevwt41HZGbNqsjyypXoaz9vco5GyD12BjwSocmCM5T/image.png)

___
**Tools Used: Jupyter NoteBook
Python Libraries: OpenCV, numpy, matplotlib**
___

<center><h2>Explanation</h2></center>
___
_The original image is taken into the imread function of OpenCv and is displayed in the output with the help of matplotlib. The image is converted into grayscale for further process. The number of rows and columns of the image are obtained by the shape function which comes out to be 480 rows and 640 columns. A 3x3 averaging filter is generated containing all elements as 1 and it is divided by 9. The 3x3 mask created are convolved over the image. The blurred image is created through the imwrite() function of OpenCV and is stored in the workspace of jupyter notebook. It is observed that the filtered image is slightly blurred. If we increase the size of the averaging mask, more blurring can
be obtained. For median filtering the image is traversed for every 3x3 area. The median of the pixels is found out and the center is replaced by the median. The median filtered image is considerably enhanced with hardly any salt and pepper noise in it._

**Conclusion:**
 1. It is observed that the filtered image is slightly blurred. If we increase the size of the averaging mask, more blurring can be obtained. 
2. The median filtered image is considerably enhanced with hardly any salt and pepper noise in it.
___
**References:** Wikipedia, geeksforgeeks website, personal prepared notes.
<a href='https://en.wikipedia.org/wiki/Spatial_filter'>Reference 1</a> <a href='https://en.wikipedia.org/wiki/Smoothing'>Reference 2</a> <a href='https://www.geeksforgeeks.org/spatial-filtering-and-its-types/'>Reference 3</a>
**Image Source:** https://pixabay.com 
___
### I tried to make the code simple to understand and gave explanation of the various functions used in the program. Feel free to give your suggestions. Until we meet again in some other post, this is @biggestloser signing off....
___

<center>
![BiggestLoser.gif](https://images.hive.blog/DQmVwwin2dRFv8uRoi2gemS8HcwrQwJof33DCVLFsP2HPkp/BiggestLoser.gif)
</center>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,