-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocal_enhancement.py
68 lines (56 loc) · 3.5 KB
/
local_enhancement.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
import cv2
import numpy as np
def local_enhancement(image):
img = cv2.imread(image, 0)
clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(10, 10))
img_equalized = clahe.apply(img)
h, w = img.shape
# img_GB = cv2.GaussianBlur(img, (3, 3), 0)
img_GB = cv2.bilateralFilter(img, 3, 50, 50)
canny_threshold_common = [40, 100]
canny_threshold_enhanced = [30, 60]
edge_canny_up = cv2.Canny(img_GB[:round(h/6), round(w/2)-20:round(w/2)+20], *canny_threshold_common, apertureSize=3)
edge_canny_middle = cv2.Canny(img_GB[round(h/6):round(h/2), round(w/2)-20:round(w/2)+20],
*canny_threshold_enhanced, apertureSize=3)
t = list(edge_canny_middle.ravel() == 255).count(1)/len(list(edge_canny_middle.ravel()))
while not 1/40 < t < 1/20:
# print(t)
if t <= 1/40:
canny_threshold_enhanced = [canny_threshold_enhanced[0] - 1, canny_threshold_enhanced[1] - 1]
else:
canny_threshold_enhanced = [canny_threshold_enhanced[0] + 1, canny_threshold_enhanced[1] + 1]
edge_canny_middle = cv2.Canny(img_GB[round(h / 6):round(h / 2), round(w / 2) - 20:round(w / 2) + 20],
*canny_threshold_enhanced, apertureSize=3)
t = list(edge_canny_middle.ravel() == 255).count(1) / len(list(edge_canny_middle.ravel()))
edge_canny_down = cv2.Canny(img_GB[round(h/2):, round(w/2)-20:round(w/2)+20], *canny_threshold_common, apertureSize=3)
edge_canny_middle_horizontally = np.vstack((edge_canny_up, edge_canny_middle, edge_canny_down))
edge_canny_left = cv2.Canny(img_GB[:, :round(w/2)-20], *canny_threshold_common, apertureSize=3)
edge_canny_right = cv2.Canny(img_GB[:, round(w/2)+20:], *canny_threshold_common, apertureSize=3)
edge_canny = np.hstack((edge_canny_left, edge_canny_middle_horizontally, edge_canny_right))
edge_equalized = cv2.Canny(img_equalized, *canny_threshold_common, apertureSize=3)
# edge_canny, contours, hierarchy = cv2.findContours(edge_canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# small_areas = [i for i in contours if cv2.contourArea(i) < 1]
# cv2.fillPoly(edge_canny, small_areas, 0)
#
# edge_equalized, contours, hierarchy = cv2.findContours(edge_equalized, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# small_areas = [i for i in contours if cv2.contourArea(i) < 20]
# cv2.fillPoly(edge_equalized, small_areas, 0)
# 形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 4))
edge_canny = cv2.dilate(edge_canny, kernel)
edge_canny = cv2.dilate(edge_canny, kernel)
edge_canny = cv2.dilate(edge_canny, kernel)
edge_canny = cv2.erode(edge_canny, kernel)
edge_canny = cv2.morphologyEx(edge_canny, cv2.MORPH_CLOSE, kernel)
kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
edge_canny = cv2.morphologyEx(edge_canny, cv2.MORPH_CLOSE, kernel2)
# 形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
edge_equalized = cv2.dilate(edge_equalized, kernel)
edge_equalized = cv2.dilate(edge_equalized, kernel)
edge_equalized = cv2.dilate(edge_equalized, kernel)
edge_equalized = cv2.erode(edge_equalized, kernel)
edge_equalized = cv2.morphologyEx(edge_equalized, cv2.MORPH_CLOSE, kernel)
kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
edge_equalized = cv2.morphologyEx(edge_equalized, cv2.MORPH_CLOSE, kernel2)
return img, img_equalized, edge_canny, edge_equalized