-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.py
More file actions
509 lines (390 loc) · 17.9 KB
/
Tools.py
File metadata and controls
509 lines (390 loc) · 17.9 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
"""
Function Name: tools
Description: Collecting some commonly used functions in Image Processing
Argument: None
Parameter: None
Return: None
Edited by: 2020-08-20 Bill Gao
"""
import numpy as np
import cv2
def thresholdTest(input_image, bgr = False):
"""
Function Name: thresholdTest
Description: input an image, and select the upper and lower threshold value
Argument:
input_image [np.array] -> input image
bgr [bool] -> Convert a bgr image into a greyscale image
Parameters: None
Return:
Edited by: 2020-08-20 Bill Gao
"""
if len(input_image.shape) == 3:
if bgr == True:
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
else:
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("upper", "Trackbars", 255, 255, lambda tmp:None)
cv2.createTrackbar("lower", "Trackbars", 0, 255, lambda tmp:None)
while (True):
upper = cv2.getTrackbarPos("upper", "Trackbars")
lower = cv2.getTrackbarPos("lower", "Trackbars")
binary_image = cv2.inRange(input_image, lower, upper)
bitwise_image = cv2.bitwise_and(input_image, input_image, mask = binary_image)
cv2.imshow("binary_image", binary_image)
cv2.imshow("bitwise_image", bitwise_image)
if cv2.waitKey(1) & 0xFF == ord("w"):
print("lower, upper = {}, {}\n".format(lower, upper))
break
cv2.destroyWindow("Trackbars")
cv2.destroyWindow("binary_image")
cv2.destroyWindow("bitwise_image")
def threshold(input_image, lower, upper, show_image = None, bgr = False):
"""
Function Name: threshold
Description: Convert the input image into a hsv image, and turn pixels whose intensity are between
the given lower and upper threshold value into white, and turn the other into black.
Argument:
input_image [np.array] -> Input Image
lower [int] -> lower threshold value
upper [int] -> upper threshold value
show_image [bool] -> Show Result
bgr [bool] -> Convert a bgr image into a greyscale image
Parameters: None
Return:
[np.array] -> Binary image
[np.array] -> Colour image
Edited by: 2020-08-20 Bill Gao
"""
if len(input_image.shape) == 3:
if bgr == True:
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
else:
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)
binary_image = cv2.inRange(input_image, lower, upper)
bitwise_image = cv2.bitwise_and(input_image, input_image, mask = binary_image)
if show_image:
cv2.imshow("threshold_out_binary", binary_image)
cv2.imshow("threshold_out_colour", bitwise_image)
cv2.waitKey(0)
cv2.destroyWindow("threshold_out_binary")
cv2.destroyWindow("threshold_out_colour")
return binary_image, bitwise_image
def hsvTest(input_image, bgr = False):
"""
Function Name: hsvTest
Description: input an image, and select the upper and lower threshold value of HSV
Argument:
input_image [np.array] -> input image
bgr [bool] -> Convert a bgr image into a greyscale image
Parameters: None
Return:
Edited by: 2020-08-20 Bill Gao
"""
if bgr == True:
img_hsv = cv2.cvtColor(input_image, cv2.COLOR_bgr2HSV)
else:
img_hsv = cv2.cvtColor(input_image, cv2.COLOR_RGB2HSV)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("upper_H", "Trackbars", 180, 180, lambda tmp:None)
cv2.createTrackbar("upper_S", "Trackbars", 255, 255, lambda tmp:None)
cv2.createTrackbar("upper_V", "Trackbars", 255, 255, lambda tmp:None)
cv2.createTrackbar("lower_H", "Trackbars", 0, 180, lambda tmp:None)
cv2.createTrackbar("lower_S", "Trackbars", 0, 255, lambda tmp:None)
cv2.createTrackbar("lower_V", "Trackbars", 0, 255, lambda tmp:None)
print("Press 'w' to end the while loop")
while (True):
upper_H = cv2.getTrackbarPos("upper_H", "Trackbars")
upper_S = cv2.getTrackbarPos("upper_S", "Trackbars")
upper_V = cv2.getTrackbarPos("upper_V", "Trackbars")
lower_H = cv2.getTrackbarPos("lower_H", "Trackbars")
lower_S = cv2.getTrackbarPos("lower_S", "Trackbars")
lower_V = cv2.getTrackbarPos("lower_V", "Trackbars")
lower = np.array([lower_H, lower_S, lower_V], dtype = np.uint8)
upper = np.array([upper_H, upper_S, upper_V], dtype = np.uint8)
binary_image = cv2.inRange(img_hsv,lower ,upper )
bitwise_image = cv2.bitwise_and(input_image, input_image, mask = binary_image)
cv2.imshow("binary_image", binary_image)
cv2.imshow("bitwise_image", bitwise_image)
if cv2.waitKey(1) & 0xFF == ord("w"):
print("lower, upper = [{},{},{}], [{},{},{}]\n".format(lower[0],
lower[1],
lower[2],
upper[0],
upper[1],
upper[2]))
break
cv2.destroyWindow("binary_image")
cv2.destroyWindow("bitwise_image")
cv2.destroyWindow("Trackbars")
def hsv(input_image, lower, upper, show_image = None, bgr = False):
"""
Function Name: hsv
Description: Convert the input image into a hsv image, and turn pixels whose intensity are between
the given lower and upper threshold value into white, and turn the other into black.
Argument:
input_image [np.array] -> Input Image
lower [list] -> lower threshold value
upper [list] -> upper threshold value
show_image [bool] -> Show Result
bgr [bool] -> Convert a bgr image into a hsv image
Parameters: None
Return:
[np.array] -> Binary image
[np.array] -> Colour image
Edited by: 2020-08-20 Bill Gao
"""
if bgr == True:
hsv_image = cv2.cvtColor(input_image, cv2.COLOR_bgr2HSV)
lower = np.array(lower, dtype = np.uint8)
upper = np.array(upper, dtype = np.uint8)
else:
hsv_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2HSV)
lower = np.array(lower, dtype = np.uint8)
upper = np.array(upper, dtype = np.uint8)
binary_image = cv2.inRange(hsv_image, lower, upper)
bitwise_image = cv2.bitwise_and(input_image, input_image, mask = binary_image)
if show_image:
cv2.imshow("binary_image", binary_image)
cv2.imshow("bitwise_image", bitwise_image)
cv2.waitKey(0)
cv2.destroyWindow("binary_image")
cv2.destroyWindow("bitwise_image")
return binary_image, bitwise_image
def cannyTest(input_image, bgr = False):
"""
Function Name: cannyTest
Description: input an image, and select the upper and lower threshold value of canny edge detection
Argument:
input_image [np.array] -> input image
bgr [bool] -> Convert a bgr image into a greyscale image
Parameters: None
Return:
Edited by: 2020-08-20 Bill Gao
"""
if bgr == True:
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
else:
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)
blurred = cv2.GaussianBlur(input_image, (3,3), 0)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("upper", "Trackbars", 0, 255, lambda tmp:None)
cv2.createTrackbar("lower", "Trackbars", 0, 255, lambda tmp:None)
print("\n Press 'w' to end the while loop")
while (True):
upper = cv2.getTrackbarPos("upper", "Trackbars")
lower = cv2.getTrackbarPos("lower", "Trackbars")
canny_image = cv2.Canny(input_image , lower, upper)
cv2.imshow("canny_image", canny_image)
if cv2.waitKey(1) & 0xFF == ord("w"):
print("[lower, upper] = [{},{}]".format(lower, upper))
break
cv2.destroyWindow("canny_image")
def Canny(input_image,lower = 0, upper = 255, bgr = False):
"""
Function Name: Canny
Description: input lower and upper threshold value to apply canny edge detection
Argument:
input_image [type] -> [description]
bgr [bool] -> Convert a bgr image into a greyscale image
Parameters: None
Return:
[np.arrary] -> canny edge image
Edited by: 2020-08-20 Bill Gao
"""
if bgr == True:
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
else:
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)
blurred = cv2.GaussianBlur(input_image, (3,3), 0)
canny_image = cv2.Canny(blurred, lower, upper)
return canny_image
# Image_extracting Parameters
mouse_row_0, mouse_col_0, mouse_row_1, mouse_col_1, mouse_count = 0, 0, 0, 0, 0
def mouseAction(event, x, y, flags, param):
global mouse_row_0, mouse_col_0, mouse_row_1, mouse_col_1, mouse_count
if event == cv2.EVENT_LBUTTONDOWN:
print("Left button down")
if mouse_count == 0:
mouse_col_0, mouse_row_0 = x, y
mouse_count = 1
else:
mouse_col_1, mouse_row_1 = x, y
mouse_count = 0
def Image_extracting(input_image, return_image = True):
global mouse_row_0, mouse_col_0, mouse_row_1, mouse_col_1, mouse_count
cv2.namedWindow("Image_extracting")
cv2.imshow("Image_extracting", input_image)
cv2.setMouseCallback("Image_extracting", mouseAction)
cv2.waitKey(0)
extract_img = input_image[mouse_row_0: mouse_row_1, mouse_col_0:mouse_col_1]
print("Format: [row, col]")
print("upper left: [{}, {}], button right: [{}, {}]".format(mouse_row_0, mouse_col_0, mouse_row_1, mouse_col_1))
cv2.imshow("extract", extract_img)
cv2.waitKey(0)
cv2.destroyWindow("extract")
if return_image:
return extract_img
def slidingWindow(input_image, kernel):
"""
Function Name: slidingWindow
Description: Apply the sliding window method without using opencv
Argument:
input_image [np.array] -> input image
kernel [np.array] -> kernel for applying the sliding window method
Parameters: None
Return:
[np.array] -> processed image
Edited by: 2020-08-20 Bill Gao
"""
row_image, col_image, channel_image = input_image.shape
k_row, k_col = kernel.shape
feature_row = row_image - k_row + 1
feature_col = col_image - k_col + 1
feature = np.lib.stride_tricks.as_strided(input_image,
shape = (1,
channel_image,
feature_row,
feature_col,
k_row,
k_col),
strides = (1,
1,
channel_image*col_image,
channel_image*1,
channel_image*col_image,
channel_image*1))
feature = feature.reshape(feature_row*feature_col*3, k_row, k_col)
feature_map = np.uint8(np.tensordot(kernel, feature, [(0,1), (1,2)]))
Processed_image = np.lib.stride_tricks.as_strided(feature_map,
shape = (feature_row,
feature_col,
channel_image),
strides = (feature_col,
1,
feature_row * feature_col))
return Processed_image
def cameraFocusTest(Port):
"""
Function Name: cameraFocusTest
Description: Adjust the foucs of camera
Argument:
Port [type] -> [description]
Parameters: None
Return: None
Edited by: 2020-08-20 Bill Gao
"""
cv2.namedWindow('Trackbar')
cv2.createTrackbar('focus','Trackbar',0,255,lambda tmp:None)
cap = cv2.VideoCapture(Port,cv2.CAP_DSHOW)
focus = 0 # min: 0, max: 255, increment:5
cap.set(cv2.CAP_PROP_AUTOFOCUS, 1)
while (1):
_, frame = cap.read()
num_focus = cv2.getTrackbarPos('focus','Trackbar')
cap.set(cv2.CAP_PROP_FOCUS, num_focus)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF==ord("q"):
cv2.destroyAllWindows()
break
def findVertex(input_data, row_col = True):
"""
Function Name: findVertex
Description: Find the vertices of the input_data, return value will be [x, y] instead of [rows, cols]
Argument:
input_data [np.array] -> input data
Parameters: None
Return:
[np.array] -> coordinate of upper_left
[np.array] -> coordinate of upper_right
[np.array] -> coordinate of lower_right
[np.array] -> coordinate of lower_left
Edited by: 2020-08-20 Bill Gao
"""
Left_x = np.argsort(input_data[:,0])[0:2]
Right_x = np.argsort(input_data[:,0])[2:4]
if row_col:
upper_left = input_data[Left_x][np.argmin(input_data[Left_x][:,1])]
upper_right = input_data[Left_x][np.argmax(input_data[Left_x][:,1])]
lower_left = input_data[Right_x][np.argmin(input_data[Right_x][:,1])]
lower_right = input_data[Right_x][np.argmax(input_data[Right_x][:,1])]
else:
upper_left = input_data[Left_x][np.argmin(input_data[Left_x][:,1])]
upper_right = input_data[Left_x][np.argmax(input_data[Left_x][:,1])]
lower_left = input_data[Right_x][np.argmin(input_data[Right_x][:,1])]
lower_right = input_data[Right_x][np.argmax(input_data[Right_x][:,1])]
return upper_left, upper_right, lower_right, lower_left
import cv2
import numpy as np
import os
image = cv2.imread('./Image/lena.png',0)
def otsu(input_image, show_value = False):
"""
follow the formula from https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
"""
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([input_image], [0] ,None, [256], [0,256])
hist_norm = hist.ravel()/hist.sum()
# cumulative distribution function (CDF)
Q = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
thresh = -1
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
q1, q2 = Q[i],Q[255]-Q[i] # cum sum of classes
# make sure it would not divide by zero
if q1 < 1.e-6 or q2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(p1*i1)/q1, np.sum(p2*i2)/q2
var1, var2 = np.sum(((i1-mu1)**2)*p1)/q1, np.sum(((i2-mu2)**2)*p2)/q2
# calculates the minimization function
fn_tmp = var1*q1 + var2*q2
if fn_tmp < fn_min:
fn_min = fn_tmp
thresh = i
# find otsu's threshold value with Opencv2 function
ret, otsu = cv2.threshold(input_image,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# show threshold value of handmade otsu and opencv otsu
if show_value:
print( "manual otsu: {}, opencv otsu: {}".format(thresh,ret) )
return thresh
def valley_emphasis(input_image, show_value = False):
"""
follow the formula from the thsis:
Automatic thersholding for defect detection - Hui-Fuang Ng 2006
"""
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([input_image], [0], None, [256], [0,256])
hist_norm = hist.ravel()/hist.sum()
# cumulative distribution function (CDF) for probabilities
omega_total = hist_norm.cumsum()
bins = np.arange(256)
fn_max = 0
valley = 0
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
omega1, omega2 = omega_total[i], omega_total[255]-omega_total[i] # cum sum of classes
# make sure it would not divide by zero
if omega1 < 1.e-6 or omega2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(p1*i1)/omega1, np.sum(p2*i2)/omega2
# calculates the minimization function
fn_tmp = (1-hist_norm[i])*(omega1*(mu1**2) + omega2*(mu2**2))
if fn_tmp > fn_max:
fn_max = fn_tmp
valley = i
# show threshold value of handmade otsu and valley emphasis
if show_value:
otsu_value = otsu(input_image)
print("otsu: {}, valley: {}".format(otsu_value, valley))
return valley