create account

Smoothing or Averaging filter in Spatial Domain by biggestloser

View this thread on: hive.blogpeakd.comecency.com
· @biggestloser · (edited)
$4.45
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>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 78 others
properties (23)
authorbiggestloser
permlinksmoothing-or-averaging-filter-in-spatial-domain
categoryhive-169321
json_metadata{"tags":["programming","development","computervision","python","pob","stem","palnet"],"users":["biggestloser"],"image":["https://images.hive.blog/DQmPJ7MJVJMKGhna8ExxghYHNcPbg5urtnN1Ee7CkW9QUMM/image.png","https://images.hive.blog/DQmQpDw4xSmDx1yWb57A39meg1hm4YFMwvHYWyxf7db8x6E/image.png","https://images.hive.blog/DQmQzQh5WrGvey9YjpB46RdbMq8BYm7XifhXxnzZfeEDihW/image.png","https://images.hive.blog/DQmSrEsczmXdkR1hnV1F8hTBxTy4GcCKnsE4fExTSLxLkhh/image.png","https://images.hive.blog/DQmTyviAcgT7LbzJyz7JxesmGNdv4urmNfAncPgpxPUhWUg/image.png","https://images.hive.blog/DQmQRwHwZviAPP2uEzvqyTfPVZzEkayNwLad59CP28tCfmt/image.png","https://images.hive.blog/DQmeZTtfpkWUNLoGTXdA5BNARqjPq2WFwJv14RYrWuGpZFU/image.png","https://images.hive.blog/DQmRAH1csYLK5iHjTipzGeR788ZXFJYDQZdpdQpsNyH85ZS/image.png","https://images.hive.blog/DQmUzevwt41HZGbNqsjyypXoaz9vco5GyD12BjwSocmCM5T/image.png","https://images.hive.blog/DQmVwwin2dRFv8uRoi2gemS8HcwrQwJof33DCVLFsP2HPkp/BiggestLoser.gif"],"links":["https://en.wikipedia.org/wiki/Spatial_filter"],"app":"hiveblog/0.1","format":"markdown"}
created2021-08-15 14:00:39
last_update2021-08-15 14:44:24
depth0
children5
last_payout2021-08-22 14:00:39
cashout_time1969-12-31 23:59:59
total_payout_value2.244 HBD
curator_payout_value2.208 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,182
author_reputation705,599,888,230
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,557,149
net_rshares5,825,792,616,957
author_curate_reward""
vote details (142)
@discovery-it ·
<div class="pull-left">https://cdn.steemitimages.com/DQmTAn3c753LR7bHCLPo96g9UvRMaPFwaMYn8VQZa85xczC/discovery_logo_colore%20-%20Copia.png</div><br> This post was shared and voted inside the discord by the curators team of <a href="https://discord.gg/cMMp943"> discovery-it</a> <br>Join our community! <a href = "https://hive.blog/trending/hive-193212"> hive-193212</a><br>Discovery-it is also a Witness, vote for us <a href = "https://hivesigner.com/sign/account-witness-vote?witness=discovery-it&approve=true"> here</a>  <br>Delegate to us for passive income. Check our <a href = "https://hive.blog/hive-193212/@discovery-it/delegations-program-80-fee-back"> 80% fee-back Program</a> <hr>
👍  
properties (23)
authordiscovery-it
permlinkre-biggestloser-5xzyifnmh9
categoryhive-169321
json_metadata"{"app": "beem/0.24.26"}"
created2021-08-15 14:24:42
last_update2021-08-15 14:24:42
depth1
children0
last_payout2021-08-22 14:24:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length690
author_reputation67,589,807,462,014
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,557,647
net_rshares1,606,837,058
author_curate_reward""
vote details (1)
@gangstalking ·
I upvoted you, thank you for posting! 👍
👎  ,
properties (23)
authorgangstalking
permlinkre-biggestloser-smoothing-or-averaging-filter-in-spatial-domain-20210815t140046077z
categoryhive-169321
json_metadata{"app":"hive-bot/0.6.3"}
created2021-08-15 14:00:48
last_update2021-08-15 14:00:48
depth1
children0
last_payout2021-08-22 14:00:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length39
author_reputation-67,597,107,868,724
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,557,152
net_rshares-27,097,296,936
author_curate_reward""
vote details (2)
@gangstalking ·
I upvoted you, thank you for posting! 👍
👎  ,
properties (23)
authorgangstalking
permlinkre-biggestloser-smoothing-or-averaging-filter-in-spatial-domain-20210815t144432078z
categoryhive-169321
json_metadata{"app":"hive-bot/0.6.3"}
created2021-08-15 14:44:33
last_update2021-08-15 14:44:33
depth1
children0
last_payout2021-08-22 14:44:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length39
author_reputation-67,597,107,868,724
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,558,028
net_rshares-27,234,427,787
author_curate_reward""
vote details (2)
@hivebuzz ·
Congratulations @biggestloser! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@biggestloser/posts.png?202108151958"></td><td>You published more than 20 posts.<br>Your next target is to reach 30 posts.</td></tr>
<tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@biggestloser/upvotes.png?202108151958"></td><td>You distributed more than 400 upvotes.<br>Your next target is to reach 500 upvotes.</td></tr>
<tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@biggestloser/upvoted.png?202108151958"></td><td>You received more than 500 upvotes.<br>Your next target is to reach 600 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@biggestloser) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>


To support your work, I also upvoted your post!
👍  
properties (23)
authorhivebuzz
permlinkhivebuzz-notify-biggestloser-20210815t200552
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-08-15 20:05:51
last_update2021-08-15 20:05:51
depth1
children0
last_payout2021-08-22 20:05:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,100
author_reputation369,406,520,094,329
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,563,421
net_rshares1,776,037,695
author_curate_reward""
vote details (1)
@stayten ·
!discovery 15
👍  
properties (23)
authorstayten
permlinkre-biggestloser-2021815t162424133z
categoryhive-169321
json_metadata{"tags":["hive-169321","programming","development","computervision","python","pob","stem","palnet"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-08-15 14:24:18
last_update2021-08-15 14:24:18
depth1
children0
last_payout2021-08-22 14:24:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation9,397,184,917,934
root_title"Smoothing or Averaging filter in Spatial Domain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,557,637
net_rshares2,409,876,094
author_curate_reward""
vote details (1)