-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinemapping.py
138 lines (95 loc) · 5.09 KB
/
finemapping.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
# coding=utf-8
import cv2
import numpy as np
import niblack_thresholding as nt
import deskew
def fitLine_ransac(pts, zero_add=0):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((136- x) * vy / vx) + y)
return lefty+30+zero_add, righty+30+zero_add
return 0, 0
def fastDeskew(image):
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
skew_h, skew_v = skew_detection(image_gray)
print("校正角度 h ", skew_h, "v", skew_v)
deskew, M = v_rot(image, int((90-skew_v)*1.5), image.shape, 60)
return deskew, M
# 精定位算法
def findContoursAndDrawBoundingBox(image_rgb):
line_upper = []
line_lower = []
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
# for k in np.linspace(-1.5, -0.2, 10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 17, k)
# cv2.imshow("image1", binary_niblack)
# cv2.waitKey(0)
contours, hierarchy = cv2.findContours(binary_niblack.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # imagex,
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2]) > 3 and bdbox[3]*bdbox[2] < 100):
# cv2.rectangle(rgb, (bdbox[0], bdbox[1]), (bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]), (255, 0, 0), 1)
line_upper.append([bdbox[0], bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0], bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb, 30, 30, 0, 0, cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower), 3)
rows, cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1, cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper), -3)
rows, cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0, 255, 0), 1, cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA], [cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136, 36], [0, 36], [136, 0], [0, 0]])
mat = cv2.getPerspectiveTransform(pts_map1, pts_map2)
image = cv2.warpPerspective(rgb, mat, (136, 36), flags=cv2.INTER_CUBIC)
cv2.imwrite(r"D:\python\python_work\image.jpg", image)
image, M = deskew.fastDeskew(image)
# cv2.imshow("image::::::::", image)
return image
# 多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = []
line_lower = []
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2, 10):
# for k in np.linspace(-15, 0, 15):
#
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image, 19, k)
contours, hierarchy = cv2.findContours(binary_niblack.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # imagex,
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2]) > 3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb, (bdbox[0], bdbox[1]), (bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]), (255, 0, 0), 1)
line_upper.append([bdbox[0], bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0], bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2], bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb, 30, 30, 0, 0, cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower), 2)
rows, cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1, cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper), -4)
rows, cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0, 255, 0), 1, cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA], [cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136, 36], [0, 36], [136, 0], [0, 0]])
mat = cv2.getPerspectiveTransform(pts_map1, pts_map2)
image = cv2.warpPerspective(rgb, mat, (136, 36), flags=cv2.INTER_CUBIC)
image, M = deskew.fastDeskew(image)
return image