-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpardia.py
More file actions
395 lines (331 loc) · 14 KB
/
pardia.py
File metadata and controls
395 lines (331 loc) · 14 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
import os
import cv2 as cv
import numpy as np
import tkinter as tk
from tkinter import ttk
import customtkinter as ctk
from PIL import Image, ImageTk
from matplotlib import pyplot as plt
def passFunction(value):
pass
papyrus_file_name = 'greek_1.jpg'
original_papyrus_img = cv.imread(filename=papyrus_file_name, flags=1)
dict_names = {0: 'Papyrus CV GUI',
1: 'PAPYRUS_IMG',
2: 'GRAY',
3: 'TRACKER',
4: 'HSV_TRACKER',
}
papyrus_img_width = original_papyrus_img.shape[1]
papyrus_img_height = original_papyrus_img.shape[0]
resize_scale_factor = 4
new_img_width = int(papyrus_img_width / resize_scale_factor)
new_img_height = int(papyrus_img_height / resize_scale_factor)
img_resized = cv.resize(original_papyrus_img,
dsize=(new_img_width, new_img_height))
flag = False
points = []
img = img_resized.copy()
p1 = (0, 0)
p3 = (img.shape[1], img.shape[0])
color = (0, 0 ,0)
HSV_pickup_val = 'IMAGE'
global HSV_outimg
def restore_img(*args):
global HSV_outimg, cp, flag
cp = img_resized
HSV_outimg = img_resized
update_img_helper(img_resized)
crop_button.configure(state = ctk.NORMAL,)
def lock_img(img):
update_img_helper(img)
def create_separator():
separator = ttk.Separator(right_frame, orient='horizontal')
separator.pack(fill='x', pady=5)
def transparent_rectangle(img, pt1, pt2, color, opacity):
overlay = img.copy()
output = img.copy()
cv.rectangle(overlay, pt1, pt2, color, -1)
cv.addWeighted(overlay, opacity, output, 1-opacity, 0, output)
return output
def toggle_event_handling():
global flag
flag = not flag
if flag:
image_label.bind("<Button-1>", cropper_helper)
image_label.bind("<Motion>", dynamic_rectangle)
image_label.bind("<Button-3>", crop_event_tk)
crop_button.configure(text="cropping",
fg_color="red",
)
else:
image_label.unbind("<Button-1>")
image_label.unbind("<Motion>")
image_label.unbind("<Button-3>")
crop_button.configure(text="enter_crop_mode",
fg_color=['#2CC985', '#2FA572'],
)
cp = img_resized
HSV_outimg = img_resized
def cropper_helper(event):
global flag, points, img, img_resized, p1, p3
if not flag:
return
x, y = event.x, event.y
cp = img_resized.copy()
points.append((x,y))
if len(points) == 2:
(x1, y1), (x2, y2) = points
p1 = (x1, y1)
p3 = (x2, y2)
cp = transparent_rectangle(cp, p1, p3, color, 0.5)
points.clear()
update_img_helper(cp)
def dynamic_rectangle(event):
global flag, points, img, p1, p3
if not flag or len(points) != 1:
return
x, y = event.x, event.y
cp = img.copy()
cp = transparent_rectangle(cp, points[0], (x,y), color, 0.3)
update_img_helper(cp)
def crop_event_tk(event):
global flag, img, img_resized, p1, p3, cropped_img, cp, HSV_outimg
if len(points) == 0:
(x1, y1), (x2, y2) = p1, p3
cp = img_resized.copy()
cp = cp[min(y1,y2):max(y1,y2), min(x1,x2):max(x1,x2)]
update_img_helper(cp)
flag = not flag
crop_button.configure(text="enter_crop_mode",
fg_color=['#2CC985', '#2FA572'],
state = ctk.DISABLED,
)
HSV_outimg = cp
def update_img_helper(img):
img_pil = Image.fromarray(cv.cvtColor(img, cv.COLOR_BGR2RGB))
my_image = ctk.CTkImage(light_image=img_pil, dark_image=img_pil, size=img_pil.size)
image_label.configure(image=my_image)
image_label.image = my_image
def update_image(value):
blur_value = blur_scale.get()
blurred_image = cv.GaussianBlur(img, (blur_value*2+1, blur_value*2+1), 0)
update_img_helper(blurred_image)
###########################################################################
###########################################################################
def show_splash_screen(duration_ms):
splash_root = ctk.CTk()
splash_root.overrideredirect(True)
splash_image = cv.imread('mine_icon2.webp', flags=1)
splash_img_width = splash_image.shape[1]
splash_img_height = splash_image.shape[0]
resize_scale_factor = 2
new_img_width = int(splash_img_width / resize_scale_factor)
new_img_height = int(splash_img_height / resize_scale_factor)
splash_image = cv.resize(splash_image,
dsize=(new_img_width, new_img_height))
splash_img_pil = Image.fromarray(cv.cvtColor(splash_image, cv.COLOR_BGR2RGB))
splash_my_image = ctk.CTkImage(light_image=splash_img_pil,
dark_image=splash_img_pil,
size=splash_img_pil.size)
splash_image_label = ctk.CTkLabel(splash_root, image=splash_my_image, text="")
splash_image_label.pack()
# Center the splash screen on the screen
screen_width = splash_root.winfo_screenwidth()
screen_height = splash_root.winfo_screenheight()
window_width = splash_image.shape[1]
window_height = splash_image.shape[0]
center_x = int((screen_width - window_width) / 2)
center_y = int((screen_height - window_height) / 2)
splash_root.geometry(f"{window_width}x{window_height}+{center_x}+{center_y}")
# Show the splash screen for the specified duration then destroy
splash_root.after(duration_ms, splash_root.destroy)
splash_root.mainloop()
show_splash_screen(1000)
root = ctk.CTk()
root.iconbitmap('papyrus_icon.ico')
ctk.set_appearance_mode('dark')
ctk.set_default_color_theme("green")
root.title("PARDIA")
left_frame = ctk.CTkFrame(root)
left_frame.grid(row=0, column=0, padx=10, pady=10)
right_frame = ctk.CTkFrame(root)
right_frame.grid(row=0, column=1, padx=10, pady=10)
img_pil = Image.fromarray(cv.cvtColor(img_resized, cv.COLOR_BGR2RGB))
my_image = ctk.CTkImage(light_image=img_pil, dark_image=img_pil, size=img_pil.size)
image_label = ctk.CTkLabel(left_frame, image=my_image, text="")
image_label.pack()
restore_button = ctk.CTkButton(master= right_frame,
text="restore_original_img",
corner_radius=5,
command=restore_img,
)
restore_button.pack(pady=5)
crop_button = ctk.CTkButton(master=right_frame,
text="enter_crop_mode",
command=toggle_event_handling,
corner_radius=5,
)
crop_button.pack(pady=5)
###########################################################################
###########################################################################
global LH, LS, LV, UH, US, UV
global LH_label, LS_label, LV_label
LH, LS, LV = 0, 0, 0
UH, US, UV = 255, 255, 255
def HSV_img_combolist(event=None):
global LH, LS, LV, UH, US, UV
hsv = cv.cvtColor(cp, cv.COLOR_BGR2HSV)
LH = int(LH_slider.get())
LS = int(LS_slider.get())
LV = int(LV_slider.get())
UH = int(UH_slider.get())
US = int(US_slider.get())
UV = int(UV_slider.get())
LH_label.configure(text=f'LH: {LH}')
UH_label.configure(text=f'UH: {UH}')
LS_label.configure(text=f'LS: {LS}')
US_label.configure(text=f'US: {US}')
LV_label.configure(text=f'LV: {LV}')
UV_label.configure(text=f'UV: {UV}')
HSV_lower_bound = np.array([LH, LS, LV])
HSV_upper_bound = np.array([UH, US, UV])
HSV_mask = cv.inRange(hsv, HSV_lower_bound, HSV_upper_bound)
HSV_result_by_mask = cv.bitwise_and(cp, cp, mask=HSV_mask)
global HSV_pickup_val, HSV_outimg
if HSV_pickup_val == 'IMAGE':
update_img_helper(cp)
HSV_outimg = cp
elif HSV_pickup_val == 'BitwiseOR':
update_img_helper(HSV_result_by_mask)
HSV_outimg = HSV_result_by_mask
else:
update_img_helper(HSV_mask)
HSV_outimg = HSV_mask
HSV_mask = HSV_outimg
def update_HSV_pickup_val(event):
global HSV_pickup_val
HSV_pickup_val = HSV_pickup.get()
HSV_img_combolist()
HSV_scales_frame = ctk.CTkFrame(right_frame)
HSV_scales_frame.pack(pady=10)
my_font = ctk.CTkFont(size=20, weight='bold')
HSV_label_title = ctk.CTkLabel(HSV_scales_frame,
text='HSV_detection',
text_color=['#2CC985', '#2FA572'],
font=my_font)
HSV_label_title.pack(side='top',pady=5)
HSV_pickup = ctk.CTkComboBox(HSV_scales_frame,
values=['IMAGE' ,'BitwiseOR', 'MASK'],
state='readonly',
command=update_HSV_pickup_val
)
HSV_pickup.set('IMAGE')
HSV_pickup.pack(side='top')
def lower_HSV_slider(label, value, command):
lower_attr = ctk.CTkSlider(HSV_scales_frame,
from_=0,
to=255,
orientation=ctk.HORIZONTAL,
command=command,
)
lower_attr.set(0)
lower_attr_label = ctk.CTkLabel(HSV_scales_frame,
text=f'{label}: {value}')
lower_attr_label.pack(side='top')
lower_attr.pack(side='top')
return lower_attr, lower_attr_label
def upper_HSV_slider(label, value, command):
upper_attr = ctk.CTkSlider(HSV_scales_frame,
from_=0,
to=255,
orientation=ctk.HORIZONTAL,
command=command,
)
upper_attr.set(255)
upper_attr_label = ctk.CTkLabel(HSV_scales_frame,
text=f'{label}: {value}')
upper_attr_label.pack(side='top')
upper_attr.pack(side='top')
return upper_attr, upper_attr_label
LH_slider, LH_label = lower_HSV_slider('LH', LH, HSV_img_combolist)
UH_slider, UH_label = upper_HSV_slider('UH', UH, HSV_img_combolist)
LS_slider, LS_label = lower_HSV_slider('LS', LS, HSV_img_combolist)
US_slider, US_label = upper_HSV_slider('US', US, HSV_img_combolist)
LV_slider, LV_label = lower_HSV_slider('LV', LV, HSV_img_combolist)
UV_slider, UV_label = upper_HSV_slider('UV', UV, HSV_img_combolist)
###########################################################################
###########################################################################
morph_frame = ctk.CTkFrame(right_frame)
morph_frame.pack(pady=10)
morph_title = ctk.CTkLabel(morph_frame,
text='Morphological',
text_color=['#2CC985', '#2FA572'],
font=my_font)
morph_title.pack(side='top',pady=5)
global morph_done_val
morph_done_val = 0
def update_morph_done_val(event=None):
global morph_done_val
morph_done_val = morph_done.get()
morph_combolist()
morph_done = ctk.CTkSwitch(morph_frame, text='done?', command=update_morph_done_val)
morph_done.deselect()
morph_done.pack(side='bottom', pady=5)
def morph_combolist(event=None):
global ksize_val, iter_val, HSV_outimg, morph_done_val
ksize_val = int(ksize_slider.get())*2+1 # to get only odd values
iter_val = int(iter_slider.get())
ksize_label.configure(text=f'ksize: {ksize_val}')
iter_label.configure(text=f'iter: {iter_val}')
ksize_tuple = np.ones((ksize_val, ksize_val), dtype=np.uint8)
global morph_method_val, morph_outimg, HSV_outimg
if morph_method_val == 'None':
update_img_helper(HSV_outimg)
elif morph_method_val == 'DILATION':
dilation = cv.dilate(HSV_outimg.copy(), kernel=ksize_tuple,
iterations=iter_val)
update_img_helper(dilation)
elif morph_method_val == 'EROSION':
erosion = cv.erode(HSV_outimg.copy(), kernel=ksize_tuple,
iterations=iter_val)
update_img_helper(erosion)
elif morph_method_val == 'OPENING':
opening = cv.morphologyEx(HSV_outimg.copy(), cv.MORPH_OPEN,
kernel=ksize_tuple,
iterations=iter_val)
update_img_helper(opening)
elif morph_method_val == 'CLOSING':
closing = cv.morphologyEx(HSV_outimg.copy(), cv.MORPH_CLOSE,
kernel=ksize_tuple,
iterations=iter_val)
update_img_helper(closing)
def update_morph_method_val(event):
global morph_method_val
morph_method_val = morph_method_CB.get()
morph_combolist()
morph_method_CB = ctk.CTkComboBox(morph_frame,
values=['None', 'DILATION' ,'EROSION', 'OPENING', 'CLOSING'],
state='readonly',
command=update_morph_method_val
)
morph_method_CB.set('None')
morph_method_CB.pack(side='top')
def morph_slider(label, value, to_, command):
morph_slider_val = ctk.CTkSlider(morph_frame,
from_=1,
to=to_,
orientation=ctk.HORIZONTAL,
command=command,
)
morph_slider_val.set(1)
morph_slider_val_label = ctk.CTkLabel(morph_frame,
text=f'{label}: {value}')
morph_slider_val_label.pack(side='top')
morph_slider_val.pack(side='top')
return morph_slider_val, morph_slider_val_label
ksize_slider, ksize_label = morph_slider('ksize', LH, 6,morph_combolist)
iter_slider, iter_label = morph_slider('iter', LH, 99, morph_combolist)
###########################################################################
###########################################################################
root.mainloop()