-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
453 lines (366 loc) · 15.7 KB
/
Main.py
File metadata and controls
453 lines (366 loc) · 15.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import tkinter as tk
from tkinter.filedialog import *
from PIL import Image, ImageTk, ImageDraw
import numpy as np
import random
import string
# Written by Erdal S. Dogan on February 22nd, 2019
#
# COMP204 Programming Studio
# Instructor Muhittin Gokmen
#
# MEF University
# Sariyer, Istanbul
#
def isIsolated(array, i, j):
if ((array[i][j] == 1) and (array[i][j - 1] == array[i][j + 1] == array[i - 1][j] ==
array[i - 1][j - 1] == array[i - 1][j + 1] ==
array[i + 1][j - 1] == array[i + 1][j] == array[i + 1][j + 1] == 0)):
return True
return False
def surroundWithZeros(array):
dims = array.shape
final_arr = np.zeros(dims)
for m in range(1, dims[0] - 1):
for n in range(1, dims[1] - 1):
final_arr[m][n] = array[m][n]
return final_arr
def np2binary(array):
threshold = 150
dims = array.shape
for i in range(0, dims[0]):
for j in range(0, dims[1]):
if array[i][j] <= threshold:
array[i][j] = 0
else:
array[i][j] = 1
def contains1(array):
if 1 in array or 1 in array[1]:
return True
return False
######################################################################
######################## GUI ########################
######################################################################
root = tk.Tk()
root.title("Object Counting")
# get dimensions of the user's display
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print("Screen Width: {}px\nScreen Height: {}px".format(screen_width, screen_height))
# window's dimensions
window_width = int(screen_width / 1.5)
window_height = int(screen_height / 1.5)
# variables to center the main window when it's opened
center_x = int((screen_width - window_width) / 2)
center_y = int((screen_height - window_height) / 2)
size = "{}x{}+{}+{}".format(window_width, window_height, center_x, center_y)
frame_width = screen_width / 2
frame_height = screen_height / 2
frame_padx = (window_width - frame_width) / 2
frame_pady = (window_height - frame_height) / 2
button_cell_height = frame_height / 15
grid_cell_height = (frame_height - button_cell_height) / 2
grid_cell_width = frame_width / 3
root.geometry(size)
image_array = None
iteration = None
number_of_objects = None
algorithm = None
image_name = None
def displayImage(image):
scaled_image = scale(image)
image_tk = ImageTk.PhotoImage(scaled_image)
# preview original image
label1 = Label(preview, image=image_tk)
label1.image = image_tk
label1.grid(row=0, column=0)
# preview binary image
binary_image = binaryImage(scaled_image)
binary_image = ImageTk.PhotoImage(binary_image)
label2 = Label(binary_preview, image=binary_image)
label2.image = binary_image
label2.grid(row=0, column=0)
# initialize the global variable image array,
# in order to use it in different functions such as levialdi, tsf etc.
global image_array
image_array = np.array(image.convert("L"))
np2binary(image_array)
def openImage():
path = askopenfilename(parent=frame)
sliced = path.split('/') # extract the name of the file from the path
global image_name
image_name = sliced[-1] # last variable of the array which is the file name
raw_img = Image.open(path)
displayImage(raw_img)
def scale(img):
# 1- find the greater dimension of the image, height or width?
# 2- change the greater dimension of the image as the corresponding dimension of the frame
# e.g if the image is long and narrow, set the image height as the height of the frame and reset
# the width value while protecting the aspect ratio
#
#
dimension = img.size
greater = max(dimension)
aspectRatio = dimension[0] / dimension[1]
index = dimension.index(greater) # index of the grater dimension
if index == 0: # if width is greater than height
x = int(grid_cell_width - 50) # subtract 50 for visual improvement
y = int(x / aspectRatio)
elif index == 1: # if height is greater than width
y = int(grid_cell_height - 50)
x = int(y * aspectRatio)
img = img.resize((x, y)) # rescaled image
return img
# convert the array to binary array. if the rgb value is below a cer
def binaryImage(image):
image_array = np.array(image.convert("L"))
np2binary(image_array)
final_image = Image.fromarray(np.uint8(image_array) * 255)
return final_image
frame = Frame(root, width=frame_width, height=frame_height, borderwidth=1,
relief=RIDGE)
frame.grid_propagate(0)
frame.pack(padx=0, pady=frame_pady / 2)
for i in range(3):
frame.grid_columnconfigure(i, minsize=grid_cell_width)
# frames for widgets
openButtons_frame = Frame(frame, width=grid_cell_width, height=button_cell_height, relief=RAISED)
openButtons_frame.grid(row=0, column=0)
preview = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
preview.grid(row=1, column=0)
binary_preview = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
binary_preview.grid(row=2, column=0)
runButtons_frame = Frame(frame, width=grid_cell_width, height=button_cell_height, relief=RAISED)
runButtons_frame.grid(row=0, column=1)
saveButtons_frame = Frame(frame, width=grid_cell_width, height=button_cell_height, relief=RAISED)
saveButtons_frame.grid(row=0, column=2)
levialdi_frame = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
levialdi_frame.grid(row=1, column=1)
tsf_frame = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
tsf_frame.grid(row=2, column=1)
levialdi_result_frame = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
levialdi_result_frame.grid(row=1, column=2)
tsf_result_frame = Frame(frame, width=grid_cell_width, height=grid_cell_height, borderwidth=2, relief=RAISED)
tsf_result_frame.grid(row=2, column=2)
# create random image
def createRandom():
def randomLetter(): # create random letter
return random.choice(string.ascii_letters) # return random letter
img = Image.new('RGB', (200, 200), color=(0, 0, 0))
for i in range(random.randint(0, 75)):
d = ImageDraw.Draw(img)
# choose a random letter and random position
# d.text(x-coordinate, y-coordinate, string, color)
d.text((random.randint(0, 255), random.randint(0, 255)), randomLetter(), fill=(255, 255, 255))
displayImage(img)
global image_name
image_name = 'random-out.jpg'
img.save('random-out.jpg')
def levialdi():
global algorithm
algorithm = 'Levialdi' # set used algorithm name as Levialdi
def levialdi_deletion_condition(array, i, j):
if (array[i][j] == 1) and (array[i][j - 1] == array[i + 1][j - 1] == array[i + 1][j] == 0):
return True
return False
def levialdi_augmentation_condition(array, i, j):
if (array[i][j] == 0) and (array[i][j - 1] == array[i + 1][j] == 1):
return True
return False
global image_array, iteration, number_of_objects
iteration = 0 # store number of iterations
lev_array = image_array.copy() # work with the copy of the array
temp = lev_array.copy() # overwrite on the temp array
size = lev_array.shape
number_of_objects = 0
while contains1(lev_array):
for i in range(1, size[0] - 1):
for j in range(1, size[1] - 1):
if isIsolated(lev_array, i, j):
number_of_objects += 1
temp[i][j] = 0
elif levialdi_deletion_condition(lev_array, i, j):
temp[i][j] = 0
elif levialdi_augmentation_condition(lev_array, i, j):
temp[i][j] = 1
iteration += 1
lev_array = temp.copy()
lev_array = surroundWithZeros(lev_array)
final_image = Image.fromarray(np.uint8(lev_array) * 255)
final_image = scale(final_image)
final_image = ImageTk.PhotoImage(final_image)
label_im = Label(levialdi_frame, image=final_image)
label_im.image = final_image
label_im.grid(row=0, column=0)
frame.update()
result = "Levialdi Iterations {}\n Number of Objects {}".format(iteration, number_of_objects)
text = Label(levialdi_result_frame, text=result)
text.grid(row=0, column=0)
tsf_result_frame.update()
def twosubfields():
global algorithm
algorithm = 'TSF'
def get_bp(array, i, j):
neighbors = [array[i - 1][j - 1], array[i - 1][j], array[i - 1][j + 1],
array[i][j + 1], array[i + 1][j + 1], array[i + 1][j], array[i + 1][j - 1], array[i][j - 1]]
return np.sum(neighbors)
def get_cp(array, i, j):
# in order to make the array circular, 0th element set to be equal to last element,
# which is p8 in two-subfields algorithm
# p_array consist 8-neighbors of a pixel
neighbors = [array[i - 1][j - 1], array[i - 1][j], array[i - 1][j + 1],
array[i][j + 1], array[i + 1][j + 1], array[i + 1][j], array[i + 1][j - 1], array[i][j - 1]]
cp = 0
b1, b2, b3, b4, b5, b6, b7, b8 = neighbors[0], neighbors[1], neighbors[2], neighbors[3], neighbors[4], \
neighbors[5], neighbors[6], neighbors[7]
if b2 == 1 and b4 == 1:
b3 = 1
if b4 == 1 and b6 == 1:
b5 = 1
if b6 == 1 and b8 == 1:
b7 = 1
if b8 == 1 and b2 == 1:
b1 = 1
list1 = [b1, b2, b3, b4, b5, b6, b7, b8, b1]
length = len(list1)
for p in range(length - 1):
if list1[p] == 0 and list1[p + 1] == 1:
cp += 1
if cp == 0 and 1 in list1:
return 1
return cp
# return true if neighbors of the pixel contains length 3 or more run of zeros
# this function only checks if the neighbors contains 3 consequent zeros
def contains3lengthzeros(array, i, j):
# 8 neighbors of the pixel
b1, b2, b3, b4, b5, b6, b7, b8 = [array[i - 1][j - 1], array[i - 1][j], array[i - 1][j + 1],
array[i][j + 1], array[i + 1][j + 1], array[i + 1][j], array[i + 1][j - 1],
array[i][j - 1]]
neighbors = [b1, b2, b3, b4, b5, b6, b7, b8, b1, b2]
length = len(neighbors)
for m in range(length):
if neighbors[m - 2] == neighbors[m - 1] == neighbors[m] == 0:
# negative index is reading the array in reverse in python
# neighbors[-1] = neighbors[length - 1]
return True
return False
def deletion_condition(array, i, j):
if array[i][j] != 1:
return False
elif get_cp(array, i, j) == 1:
flag = True
if get_bp(array, i, j) == 1:
flag = False
if array[i - 1][j - 1] == array[i + 1][j - 1] == 0:
flag = True
if flag:
if contains3lengthzeros(array, i, j):
return True
return False
def augmentation_condition(array, i, j):
if array[i][j] != 0:
return False
b2, b6, b8 = array[i - 1][j], array[i + 1][j], array[i][j - 1]
if get_cp(array, i, j) == 1:
if (b8 == b2 == 1) or (b8 == b6 == 1):
return True
return False
global image_array
image_array = surroundWithZeros(image_array) # set most outer frame of the image as 0
tsf_array = image_array.copy()
temp = tsf_array.copy()
size = tsf_array.shape
global number_of_objects, iteration
number_of_objects = 0
iteration = 0
while contains1(tsf_array):
# 4 for loops for subfield 1
for i in range(1, size[0] - 1, 2):
for j in range(1, size[1] - 1, 2):
if isIsolated(tsf_array, i, j):
number_of_objects += 1
temp[i][j] = 0
elif deletion_condition(tsf_array, i, j):
temp[i][j] = 0
elif augmentation_condition(tsf_array, i, j):
temp[i][j] = 1
for k in range(2, size[0] - 1, 2):
for l in range(2, size[1] - 1, 2):
if isIsolated(tsf_array, k, l):
number_of_objects += 1
temp[k][l] = 0
elif deletion_condition(tsf_array, k, l):
temp[k][l] = 0
elif augmentation_condition(tsf_array, k, l):
temp[k][l] = 1
tsf_array = temp.copy()
# 4 for loops for subfield 2
for m in range(1, size[0] - 1, 2):
for n in range(2, size[1] - 1, 2):
if isIsolated(tsf_array, m, n):
number_of_objects += 1
temp[m][n] = 0
elif deletion_condition(tsf_array, m, n):
temp[m][n] = 0
elif augmentation_condition(tsf_array, m, n):
temp[m][n] = 1
for o in range(2, size[0] - 1, 2):
for p in range(1, size[1] - 1, 2):
if isIsolated(tsf_array, o, p):
number_of_objects += 1
temp[o][p] = 0
elif deletion_condition(tsf_array, o, p):
temp[o][p] = 0
elif augmentation_condition(tsf_array, o, p):
temp[o][p] = 1
iteration += 1
tsf_array = temp.copy()
final_image = Image.fromarray(np.uint8(tsf_array) * 255) # create image from array
final_image = scale(final_image) # scale the image so it fits the frames
final_image = ImageTk.PhotoImage(final_image) # PIL Image to tkinter image
label_im = Label(tsf_frame, image=final_image) # create new label which contains the image itself only
label_im.image = final_image
label_im.grid(row=0, column=0)
frame.update()
result = "TSF Iterations {}\n Number of Objects {}".format(iteration, number_of_objects)
text = Label(tsf_result_frame, text=result)
text.grid(row=0, column=0)
tsf_result_frame.update()
def save():
f = open('outputs.csv', 'a')
f.write('\nFile: {}, Algorithm: {}, NCC: {}, Iterations: {}'.format(image_name, algorithm,
number_of_objects, iteration))
f.close()
def draw():
pane = Tk()
pane.title("Draw Image(Beta)")
image = Image.new("RGB", (400, 400), 'white')
draw = ImageDraw.Draw(image)
def motion(event):
canvas.create_rectangle(event.x - 5, event.y - 5, event.x, event.y, fill='white')
draw.rectangle((event.x - 5, event.y - 5, event.x, event.y), fill='white')
canvas = Canvas(pane, width=400, height=400, background='black')
canvas.bind("<B1-Motion>", motion)
canvas.pack()
canvas.unbind("<Enter>")
displayImage(image)
# buttons
open_button = Button(openButtons_frame, text='Open', command=openImage)
open_button.pack(side=LEFT)
create_button = Button(openButtons_frame, text='Create', command=createRandom)
create_button.pack(side=RIGHT)
levialdi_button = Button(runButtons_frame, text='Levialdi', command=levialdi)
levialdi_button.pack(side=LEFT)
tsf_button = Button(runButtons_frame, text='TSF', command=twosubfields)
tsf_button.pack(side=RIGHT)
save_button = Button(saveButtons_frame, text='Save', command=save)
save_button.pack(side=LEFT)
draw_button = Button(openButtons_frame, text='Draw', command=draw)
draw_button.pack()
root.mainloop()
# TODO
# delete previous image before opening a new picture
# image saved notification pop-up
# improve the draw feature
# date stamp to the output file
# separate front-end and back-end