-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedianFılter.py
195 lines (134 loc) · 4.54 KB
/
MedianFılter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from PIL import Image
import math
import sys,os
def bitMix_ordering(pixels,centerRgb,kernelSize) : # 2 tane parametre alıcak pixel_1 ve pixel_2
bitArr = pow(kernelSize,2) * [None]
p1_24bit = None
for i in range(len(pixels)) :
if pixels[i] == None :
p1_24bit = "000000000000000000000000"
else :
red_1 = format(pixels[i][0],'08b')
green_1 = format(pixels[i][1],'08b')
blue_1 = format(pixels[i][2],'08b')
p1_24bit = red_1 + green_1 + blue_1
bitArr[i] = p1_24bit
bitArr.sort()
return (int(bitArr[4][:8], 2),int(bitArr[4][8:16], 2),int(bitArr[4][16:24], 2))
def lexicographical_ordering(pixels,centerRgb,kernelSize) : # 2 tane parametre alıcak pixel_1 ve pixel_2
for i in range(len(pixels)) :
if pixels[i] == None :
pixels[i] = [0,0,0]
pixels.sort(key=lambda x : x[:])
return tuple(pixels[4])
def norm_based_ordering(pixels,centerRgb,kernelSize) : #distance fonksiyonunu kullanmadan euclideanı bul
EucArr = pow(kernelSize,2) * [None]
dic = {}
euc = None
#print(centerRgb)
for i in range(len(pixels)) :
if pixels[i] == None :
euc = math.sqrt(abs(pow(centerRgb[0],2) + pow(centerRgb[1],2) + pow(centerRgb[2],2)))
pixels[i] = centerRgb
else :
euc = math.sqrt(abs(pow((centerRgb[0] - pixels[i][0]),2) + pow((centerRgb[1] - pixels[i][1]),2) + pow((centerRgb[2] - pixels[i][2]),2) ))
EucArr[i] = euc
dic[euc] = pixels[i]
#euc1 = Math.sqrt(Math.pow(firstRed,2) + Math.pow(firstGreen,2) + Math.pow(firstBlue,2));
#euc2 = Math.sqrt(Math.pow(secRed,2) + Math.pow(secGreen,2) + Math.pow(secBlue,2));
EucArr.sort()
#print(EucArr[4])
#print(dic.get(EucArr[4]))
return tuple(dic.get(EucArr[4]))
def scalar_median_filter(image,kernelSize,f) : # marginal
width, height = image.size
indexer = kernelSize // 2
arr_Red = []
arr_Green = []
arr_Blue = []
new_image = image.copy()
pixel_access_image = new_image.load()
arr_Red = pow(kernelSize,2) * [None]
arr_Green = pow(kernelSize,2) * [None]
arr_Blue = pow(kernelSize,2) * [None]
for i in range(width) :
for j in range(height) :
y = 0
for z in range(kernelSize) :
if i + z - indexer < 0 or i + z - indexer > width - 1:
for c in range(kernelSize):
arr_Red[y] = 0
arr_Green[y] = 0
arr_Blue[y] = 0
y += 1
else:
if j + z - indexer < 0 or j + z - indexer > height - 1:
arr_Red[y] = 0
arr_Green[y] = 0
arr_Blue[y] = 0
y += 1
else:
for k in range(kernelSize):
if j + k < height and i + z < width :
red, green, blue = image.getpixel((i + z , j + k))
arr_Red[y] = red
arr_Green[y] = green
arr_Blue[y] = blue
y += 1
arr_Red.sort()
arr_Green.sort()
arr_Blue.sort()
newRed, newGreen, newBlue = arr_Red[len(arr_Red) // 2],arr_Green[len(arr_Green) // 2], arr_Blue[len(arr_Blue) // 2]
pixel_access_image[i,j] = (newRed,newGreen,newBlue)
saveName = "Results/" + f + "_scalar_.png"
new_image.save(saveName)
#new_image.show()
def vector_median_filter(image,kernelSize,func,f) : # marginal
width, height = image.size
indexer = kernelSize // 2
arr_Red = []
arr_Green = []
arr_Blue = []
new_image = image.copy()
pixel_access_image = new_image.load()
sortKernel = pow(kernelSize,2) * [None]
for i in range(width) :
for j in range(height) :
y = 0
centerRgb = image.getpixel((i, j))
for z in range(kernelSize) :
if i + z - indexer < 0 or i + z - indexer > width - 1:
for c in range(kernelSize):
sortKernel[y] = None
y += 1
else :
if j + z - indexer < 0 or j + z - indexer > height - 1:
sortKernel[y] = None
y += 1
else:
for k in range(kernelSize):
if j + k < height and i + z < width :
red, green, blue = image.getpixel((i + z , j + k))
sortKernel[y] = [red,green,blue]
y += 1
pixel_access_image[i,j] = func(sortKernel,centerRgb,kernelSize)
saveName = "Results/" + f + "_vector_" + func.__name__ + ".png"
new_image.save(saveName)
#new_image.show()
def main() :
path = 'Samples'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.png' or '.jpeg' in file:
files.append(os.path.join(r, file))
for f in files:
image = Image.open(f)
rgb_im = image.convert('RGB')
vector_median_filter(rgb_im,3,norm_based_ordering,f.split("/")[1])
vector_median_filter(rgb_im,3,bitMix_ordering,f.split("/")[1])
vector_median_filter(rgb_im,3,lexicographical_ordering,f.split("/")[1])
scalar_median_filter(rgb_im,3,f.split("/")[1])
if __name__ == "__main__" :
main()