-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian Blur
36 lines (33 loc) · 1010 Bytes
/
Median Blur
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cv2
import matplotlib.pyplot as plt
import numpy as np
import random
def main():
path ="C:\\Users\\edkni\\Downloads\\image_processing_files\\Dataset\\"
imgpath = path + '4.2.07.tiff'
img = cv2.imread(imgpath,1)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
rows,columns,channel = img.shape
p = 0.2
noisy = np.zeros(img.shape,np.uint8)
for i in range(rows):
for j in range(columns):
r = random.random()
if r < p/2:
#pepper noise
noisy[i][j] = [0,0,0]
elif r<p:
#salt noise
noisy[i][j] = [255,255,255]
else:
noisy[i][j] = img[i][j]
denoised = cv2.medianBlur(noisy,5)
output = [img,noisy,denoised]
titles = ['Original','Noisy','Denoised']
for i in range(3):
plt.subplot(1,3,i+1)
plt.imshow(output[i])
plt.title(titles[i])
plt.show()
if __name__ == "__main__":
main()