-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatial median filtering.py
43 lines (37 loc) · 1.48 KB
/
spatial median filtering.py
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
37
38
39
40
41
42
43
import cv2
import numpy as np
def convolve_mean(img,mask):
m, n = img.shape
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)
return img_new
def convolve_median(img):
m, n = img.shape
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],
img[i-1, j],
img[i-1, j + 1],
img[i, j-1],
img[i, j],
img[i, j + 1],
img[i + 1, j-1],
img[i + 1, j],
img[i + 1, j + 1]]
temp = sorted(temp)
img_new[i, j]= temp[4]
img_new = img_new.astype(np.uint8)
return img_new
mask = np.ones((3, 3), dtype="float") * (1.0 / (3*3))
image = cv2.imread('pokemon.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("original", gray)
convoleMeanOutput = convolve_mean(gray,mask)
cv2.imshow("Mean Filter Image", convoleMeanOutput)
convoleMedianOutput = convolve_median(gray)
cv2.imshow("Median Filter Image", convoleMedianOutput)