-
Notifications
You must be signed in to change notification settings - Fork 21
/
MedianFilter.py
40 lines (31 loc) · 1.07 KB
/
MedianFilter.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
import numpy
from PIL import Image
def median_filter(data, filter_size):
temp = []
indexer = filter_size // 2
data_final = []
data_final = numpy.zeros((len(data),len(data[0])))
for i in range(len(data)):
for j in range(len(data[0])):
for z in range(filter_size):
if i + z - indexer < 0 or i + z - indexer > len(data) - 1:
for c in range(filter_size):
temp.append(0)
else:
if j + z - indexer < 0 or j + indexer > len(data[0]) - 1:
temp.append(0)
else:
for k in range(filter_size):
temp.append(data[i + z - indexer][j + k - indexer])
temp.sort()
data_final[i][j] = temp[len(temp) // 2]
temp = []
return data_final
def main():
img = Image.open("noisyimg.png").convert(
"L")
arr = numpy.array(img)
removed_noise = median_filter(arr, 3)
img = Image.fromarray(removed_noise)
img.show()
main()