-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
307 lines (278 loc) · 12.3 KB
/
main.py
File metadata and controls
307 lines (278 loc) · 12.3 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from Tkinter import *
from ttk import *
from PIL import Image
from PIL import ImageTk
import tkFileDialog
import tkMessageBox
import cv2
import math
import random
import numpy as np
class Main(Frame):
img = None
originImg = None
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.menubar = Menu(self)
filemenu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open", command=self.readImage)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
editmenu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Add pepper and salt noise", command=lambda: self.addNoise(0))
editmenu.add_command(label="Add pepper noise", command=lambda: self.addNoise(1))
editmenu.add_command(label="Add salt noise", command=lambda: self.addNoise(2))
editmenu.add_command(label="Reset", command=self.reset)
toolmenu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Tool", menu=toolmenu)
toolmenu.add_command(label="Neighbor Averaging(3x3)"
, command=lambda: self.smoothing(img, 'neighborhood_averaging', 1))
toolmenu.add_command(label="Neighbor Averaging(5x5)"
, command=lambda: self.smoothing(img, 'neighborhood_averaging', 2))
toolmenu.add_command(label="median filter(3x3)", command=lambda: self.smoothing(img, 'median_filtering', 1))
toolmenu.add_command(label="median filter(5x5)", command=lambda: self.smoothing(img, 'median_filtering', 2))
toolmenu.add_command(label="Bilateral filter(3x3, sigma_c=50, sigma_s=3)"
, command=lambda: self.smoothing(img, 'Bilateral_filter', 1))
toolmenu.add_command(label="Min filtering(3x3)"
, command=lambda: self.smoothing(img, 'Min_filtering', 1))
toolmenu.add_command(label="Max filtering(3x3)"
, command=lambda: self.smoothing(img, 'Max_filtering', 1))
toolmenu.add_command(label="peak filtering(3x3)"
, command=lambda: self.smoothing(img, 'peak_filter', 1))
toolmenu.add_command(label="valley filtering(3x3)"
, command=lambda: self.smoothing(img, 'valley_filter', 1))
self.master.config(menu=self.menubar)
self.label = Label(self)
self.label["text"] = "Process: "
self.label["font"] = "Courier, 20"
self.label["width"] = 60
self.label.grid(row=0, column=0)
# read and show the image
def readImage(self):
filename = tkFileDialog.askopenfilename(title="open", filetypes=[("Image Files", '*.jpg;*.jpeg;*.png')])
global img
global originImg
img = cv2.imread(filename)
gray = self.rgb2gray(img)
img = gray
originImg = np.copy(img)
self.label["text"] = "Process: "
global panelA, panelB
image = Image.fromarray(img)
width = 350
ratio = float(width) / image.size[0]
height = int(image.size[1] * ratio)
image.thumbnail((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
if panelA is None or panelB is None:
panelA = Label(image=image)
panelA.image = image
panelA.pack(side="left", anchor=SW, padx=10, pady=10)
panelB = Label(image=image)
panelB.image = image
panelB.pack(side="right", anchor=SE, padx=10, pady=10)
else:
panelA.configure(image=image)
panelA.image = image
panelB.configure(image=image)
panelB.image = image
def reset(self):
global img
global originImg
global panelA, panelB
img = np.copy(originImg)
image = Image.fromarray(img)
width = 350
ratio = float(width) / image.size[0]
height = int(image.size[1] * ratio)
image.thumbnail((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
self.label["text"] = "Process: "
if panelA is None or panelB is None:
panelA = Label(image=image)
panelA.image = image
panelA.pack(side="left", anchor=SW, padx=10, pady=10)
panelB = Label(image=image)
panelB.image = image
panelB.pack(side="right", anchor=SE, padx=10, pady=10)
else:
panelA.configure(image=image)
panelA.image = image
panelB.configure(image=image)
panelB.image = image
def addNoise(self, type):
global img
for i in range(0, img.shape[0]):
for j in range(0, img.shape[1]):
ran = random.random()
if type == 0:
if 0.85 > ran >= 0.7:
img.itemset((i, j, 0), 0)
img.itemset((i, j, 1), 0)
img.itemset((i, j, 2), 0)
elif ran >= 0.85:
img.itemset((i, j, 0), 255)
img.itemset((i, j, 1), 255)
img.itemset((i, j, 2), 255)
elif type == 1:
if ran >= 0.7:
img.itemset((i, j, 0), 0)
img.itemset((i, j, 1), 0)
img.itemset((i, j, 2), 0)
elif type == 2:
if ran >= 0.7:
img.itemset((i, j, 0), 255)
img.itemset((i, j, 1), 255)
img.itemset((i, j, 2), 255)
global panelA
image = Image.fromarray(img)
width = 350
ratio = float(width) / image.size[0]
height = int(image.size[1] * ratio)
image.thumbnail((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
if panelA is None:
panelA = Label(image=image)
panelA.image = image
panelA.pack(side="right", anchor=SW, padx=10, pady=10)
else:
panelA.configure(image=image)
panelA.image = image
def rgb2gray(self, rgb):
for i in range(0, rgb.shape[0]):
for j in range(0, rgb.shape[1]):
gray = 0.2989 * rgb.item(i, j, 0) + 0.5870 * rgb.item(i, j, 1) + 0.1140 * rgb.item(i, j, 2)
rgb.itemset((i, j, 0), gray)
rgb.itemset((i, j, 1), gray)
rgb.itemset((i, j, 2), gray)
return rgb
def smoothing(self, gray, type, mask):
if gray is None:
tkMessageBox.showinfo("Message", "Please press 'open' button first")
else:
result = np.copy(gray)
for i in range(0, gray.shape[0]):
for j in range(0, gray.shape[1]):
# neighborhood averaging
if type == 'neighborhood_averaging':
change = self.neighborhoodAveraging(gray, i, j, mask)
# median filtering
elif type == 'median_filtering':
change = self.medianFiltering(gray, i, j, mask)
# Bilateral filter
elif type == 'Bilateral_filter':
change = self.BilateralFilter(gray, i, j, mask, 50, 3)
# Min filtering
elif type == 'Min_filtering':
change = self.minFiltering(gray, i, j, mask)
# Max/Min filtering
elif type == 'Max_filtering':
change = self.maxFiltering(gray, i, j, mask)
# peak filtering
elif type == 'peak_filter':
change = self.peakFilter(gray, i, j, mask)
# valley filtering
elif type == 'valley_filter':
change = self.valleyFilter(gray, i, j, mask)
result.itemset((i, j, 0), change)
result.itemset((i, j, 1), change)
result.itemset((i, j, 2), change)
if len(self.label["text"]) % 70 >= 50:
self.label["text"] += "\n"
self.label["text"] += type + "(" + str(mask * 2 + 1) + "x" + str(mask * 2 + 1) + ") ->"
global img
img = np.copy(result)
global panelB
image = Image.fromarray(result)
width = 350
ratio = float(width) / image.size[0]
height = int(image.size[1] * ratio)
image.thumbnail((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
if panelB is None:
panelB = Label(image=image)
panelB.image = image
panelB.pack(side="right", anchor=SE, padx=10, pady=10)
else:
panelB.configure(image=image)
panelB.image = image
def neighborhoodAveraging(self, gray, x, y, m):
average = 0
total = 0
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
average += gray.item(x + i, y + j, 0)
total += 1
return average / total
def medianFiltering(self, gray, x, y, m):
mylist = []
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
mylist.append(gray.item(x + i, y + j, 0))
return np.median(mylist)
def BilateralFilter(self, gray, x, y, m, sigmaColor, sigmaSpace):
sum = 0
result = 0
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
colorTemp = self.colorGauss(gray.item(x, y, 0), gray.item(x + i, y + j, 0), sigmaColor)
spaceTemp = self.spaceGauss(x, y, x + i, y + j, sigmaSpace)
result += gray.item(x + i, y + j, 0) * colorTemp * spaceTemp
sum += colorTemp * spaceTemp
return result / sum
def colorGauss(self, l1, l2, sigmaColor):
return math.exp(- ((l1 - l2) ** 2) / (2 * (sigmaColor ** 2)))
def spaceGauss(self, x1, y1, x2, y2, sigmaSpace):
d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
return math.exp(- d ** 2 / (2 * sigmaSpace ** 2))
def minFiltering(self, gray, x, y, m):
mylist = []
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
mylist.append(gray.item(x + i, y + j, 0))
return min(mylist)
def maxFiltering(self, gray, x, y, m):
mylist = []
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
mylist.append(gray.item(x + i, y + j, 0))
return max(mylist)
def peakFilter(self, gray, x, y, m):
mylist = []
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
mylist.append(gray.item(x + i, y + j, 0))
mylist.sort(reverse=True)
if mylist[0] == gray.item(x, y, 0):
return mylist[1]
else:
return gray.item(x, y, 0)
def valleyFilter(self, gray, x, y, m):
mylist = []
for i in range(-m, m + 1):
for j in range(-m, m + 1):
if (gray.shape[0] - 1) >= (x + i) >= 0 and (gray.shape[1] - 1) >= (y + j) >= 0:
mylist.append(gray.item(x + i, y + j, 0))
mylist.sort()
if mylist[0] == gray.item(x, y, 0):
return mylist[1]
else:
return gray.item(x, y, 0)
if __name__ == '__main__':
root = Tk()
panelA = None
panelB = None
root.resizable(width=False, height=False)
root.minsize(width=800, height=600)
root.title("Smoothing of Image Processing")
app = Main(master=root)
app.mainloop()