-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpreprocess.py
71 lines (53 loc) · 2.44 KB
/
preprocess.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
import numpy as np
import cv2 as cv
def get_grayscale_image(image):
return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
def get_blurred_image(image, method='GAUSSIAN', kernel=(5, 5)):
if method == 'GAUSSIAN':
return cv.GaussianBlur(image, kernel, 0)
elif method == 'BILATERAL':
return cv.bilateralFilter(image, 9, 50, 50)
else:
return None
def get_image_directive(image, vertical=1, horizontal=0, kernel_size=3):
return cv.Sobel(image, cv.CV_8U, vertical, horizontal, ksize=kernel_size)
def clip_histogram(image):
img = image.copy()
mean = np.mean(img)
img[img < mean] = 0
return img
def get_binary_image(image, method='OTSU'):
if method == 'ADAPTIVE':
return cv.adaptiveThreshold(image, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 115, 1)
elif method == 'OTSU':
ret, thresholded = cv.threshold(image, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
return thresholded
elif method == 'BINARY':
ret, thresholded = cv.threshold(image, 0, 255, cv.THRESH_BINARY)
return thresholded
else:
return None
def get_morphed(image):
img = image.copy()
element = cv.getStructuringElement(shape=cv.MORPH_RECT, ksize=(17, 3))
morph_img_threshold = img.copy()
cv.morphologyEx(src=img, op=cv.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
# cv.imshow("close morphed", morph_img_threshold)
# cv.waitKey(0)
element = cv.getStructuringElement(shape=cv.MORPH_RECT, ksize=(2, 4))
cv.morphologyEx(src=morph_img_threshold, op=cv.MORPH_OPEN, kernel=element, dst=morph_img_threshold)
# cv.imshow("open morphed", morph_img_threshold)
# cv.waitKey(0)
element = cv.getStructuringElement(shape=cv.MORPH_RECT, ksize=(30, 5))
cv.morphologyEx(src=morph_img_threshold, op=cv.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
# cv.imshow("close1 morphed", morph_img_threshold)
# cv.waitKey(0)
element = cv.getStructuringElement(shape=cv.MORPH_RECT, ksize=(5, 5))
cv.morphologyEx(src=morph_img_threshold, op=cv.MORPH_OPEN, kernel=element, dst=morph_img_threshold)
# cv.imshow("open1 morphed", morph_img_threshold)
# cv.waitKey(0)
element = cv.getStructuringElement(shape=cv.MORPH_RECT, ksize=(50, 5))
cv.morphologyEx(src=morph_img_threshold, op=cv.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
# cv.imshow("close2 morphed", morph_img_threshold)
# cv.waitKey(0)
return morph_img_threshold