-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.py
192 lines (150 loc) · 5.72 KB
/
ui.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
183
184
185
186
187
188
189
190
191
192
import PySimpleGUI as sg
import cv2 as cv
import numpy as np
import methods
def select_image():
image_path = '';
while True:
image_path = sg.popup_get_file('Select image')
if image_path == '':
sg.popup('Please select an image')
if image_path != '':
break
image = cv.imread(image_path)
return image
def resize(image_original):
image_original_copy = np.ndarray.copy(image_original)
cv.imshow('Original Image', image_original)
layout_request = [
[
sg.Text('Do you wish to resize the image?'),
sg.Button('Resize'), sg.Button('No')
]
]
layout_resize = [
[
sg.Image(data='', key='-IMAGE-'),
]
]
layout_slider = [
[
sg.Slider(range=(20, 100),
default_value=100,
size=(20, 15),
orientation='horizontal', key='-SLIDER-', enable_events=True)
],
[
sg.Button('Done resizing')
]
]
window_request = sg.Window(title='Alert', layout=layout_request)
event1, values1 = window_request.read()
if event1 == 'Resize':
window_request.close()
cv.destroyWindow('Original Image')
window_resize = sg.Window(title='Resize Window', layout=layout_resize, finalize=True)
im_new = cv.imencode('.png', image_original)[1].tobytes()
window_resize['-IMAGE-'].update(data=im_new)
window_slider = sg.Window(title='', layout=layout_slider)
while True:
window_resize.read(timeout=50)
event3, values3 = window_slider.read()
if event3 == sg.WIN_CLOSED:
break
elif event3 == 'Done resizing':
window_resize.close()
window_slider.close()
# resize image
scale_factor = values3['-SLIDER-']
width = int(image_original.shape[1] * scale_factor / 100)
height = int(image_original.shape[0] * scale_factor / 100)
dim = (width, height)
image_original_copy = cv.resize(image_original, dim, interpolation=cv.INTER_AREA)
im_new = cv.imencode('.png', image_original_copy)[1].tobytes()
window_resize['-IMAGE-'].update(data=im_new)
else:
cv.destroyWindow('Original Image')
window_request.close()
return image_original_copy
def mark_crack(image):
image_bw = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
output_image = np.ndarray.copy(image)
layout_sliders = [
[
sg.Text('Alpha'),
sg.Slider(range=(0, 10),
default_value=5,
size=(50, 15),
orientation='horizontal', key='-ALPHA-', enable_events=True)
],
[
sg.Text('Beta'),
sg.Slider(range=(0, 10),
default_value=5,
size=(50, 15),
orientation='horizontal', key='-BETA-', enable_events=True)
],
[
sg.Text('gamma'),
sg.Slider(range=(0, 100),
default_value=5,
size=(50, 15),
orientation='horizontal', key='-GAMMA-', enable_events=True)
],
[
sg.In(key='-CONTOUR_AREA-', default_text='100'),
sg.Button(button_text='Set Contour Area', key='-SET_CONTOUR-')
],
[
sg.Text('Show crack on input image'), sg.Button('Show')
]
]
layout_im = [
[
sg.Image(data='', key='-IMAGE-'),
]
]
window_im = sg.Window(title='Output Image', layout=layout_im, finalize=True)
im_new = cv.imencode('.png', image)[1].tobytes()
window_im['-IMAGE-'].update(data=im_new)
window_sliders = sg.Window(title='Editing Parameters', layout=layout_sliders)
contour_area = 100
while True:
window_im.read(timeout=10)
event_sliders, values_sliders = window_sliders.read()
if event_sliders == sg.WIN_CLOSED:
break
elif event_sliders == '-SET_CONTOUR-':
area = values_sliders['-CONTOUR_AREA-']
contour_area = validate_contour_area(area)
elif event_sliders == 'Show':
img = methods.draw_contour_on_image(image, output_image)
cv.imshow('Output Image', img)
cv.waitKey()
alpha = values_sliders['-ALPHA-']
beta = values_sliders['-BETA-']
gamma = values_sliders['-GAMMA-']
output_image = mark_crack_method(image=image_bw, alpha=alpha, beta=beta, gamma=(gamma/100),
contour_area=contour_area)
output_image_png = cv.imencode('.png', output_image)[1].tobytes()
window_im['-IMAGE-'].update(data=output_image_png)
def validate_contour_area(input_string):
try:
float(input_string)
except ValueError:
sg.popup_error('Invalid input\nPlease Try Again\n\n\nContour Area set to 100', title='Error')
return 100
else:
return float(input_string)
def mark_crack_method(image, alpha, beta, gamma, contour_area):
_image = methods.filter(image)
_image = methods.adjust_contrast(image=_image, alpha=alpha, beta=beta)
_image = methods.gamma_correction(_image, gamma)
_image = methods.invert_image(_image)
_image = methods.thresh_otsu(_image)
contours = methods.draw_contours(_image)
_image = methods.remove_contours(_image, contours, contour_area)
return _image
im = select_image()
im = resize(im)
mark_crack(im)