-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback_cut.py
235 lines (203 loc) · 6.7 KB
/
back_cut.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
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
import cv2
import numpy as np
import operator
import math
# def returnWhitePoints(img):
# testdata = []
# for x in range(img.shape[0]):
# whiteNumRow = 0
# for y in range(img.shape[1]):
# if int(img[x][y]) > 175:
# whiteNumRow += 1
# testdata.append(whiteNumRow)
# # 列白点
# for x in range(img.shape[1]):
# whiteNumCol = 0
# for y in range(img.shape[0]):
# if int(img[y][x]) > 175:
# whiteNumCol += 1
# testdata.append(whiteNumCol)
# return testdata
#
#
# # 欧氏距离
# def eulideanDistance(testList, trainList, value):
# distance = 0
# for x in range(len(trainList)):
# distance += pow((testList[x] - trainList[x]), 2)
# distances = math.sqrt(distance)
# return [distances, value]
np.set_printoptions(threshold=np.inf)
# img = cv2.imread('number_plate1.jpg')
img = cv2.imread('thistest.jpg')
# cv2.imshow('original', img)
# HSV处理
hsvimg = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# cv2.imshow('hsvimg', hsvimg)
h, s, v = cv2.split(hsvimg)
hh = cv2.equalizeHist(h)
ss = cv2.equalizeHist(s)
vv = cv2.equalizeHist(v)
newhsv = cv2.merge([hh, ss, vv])
# cv2.imshow('s', s)
lower_blue = np.array([80, 43, 46])
upper_blue = np.array([140, 255, 255])
mask = cv2.inRange(newhsv, lower_blue, upper_blue)
cv2.imshow('mask', mask)
print(mask.shape)
colStart = 0
colEnd = 0
# 再次扫描,去掉车牌左右黑色部分
# 起始列
for col in range(mask.shape[1]):
count = 0
for row in range(mask.shape[0]):
if mask[row, col] > 100:
count += 1
if count > 3:
colStart = col
break
# 结束列
for col in range(mask.shape[1], -1, -1):
count = 0
for row in range(mask.shape[0]):
if mask[row, col - 1] > 100:
count += 1
if count > 3:
colEnd = col
break
print("起始与结束: ", colStart, colEnd)
roi = img[:, colStart:colEnd]
cv2.imshow("ROI区域", roi)
newRoi = cv2.resize(roi, (440, 140))
# cv2.imshow("NewROI区域", newRoi)
# ---------形态学处理---------------------------------
gray = cv2.cvtColor(newRoi, cv2.COLOR_BGR2GRAY)
# print(gray)
# equ = cv2.equalizeHist(gray)
# gauss = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
# median = cv2.medianBlur(gauss, 5)
# cv2.imshow('media', median)
# Sobel算子,X方向求梯度
# sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
# cv2.imshow('sobel', sobel)
# 二值化
ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
# cv2.imshow('binary', binary)
# --------进行扫描,去掉上下边框-----------------------------------
print("ROI shape:", newRoi.shape)
picRow = newRoi.shape[0]
picCol = newRoi.shape[1]
# 竖着切下去
for x in range(picRow):
lengthList = []
lenStart = 0
lenEnd = 0
countWhitePoints = 0
for y in range(picCol - 1):
if binary[x][y] > 0 and binary[x][y + 1] > 0:
lenStart = y
lengthList.append(lenStart)
if binary[x][y] > 0:
countWhitePoints += 1
# 如果跳变次数小于10即lengthList的长度小于10,则把这一行统统置为0
# print(countWhitePoints)
if lengthList.__len__() < 10 or countWhitePoints < 50:
for y in range(picCol):
binary[x][y] = 0
cv2.imshow("process binary: ", binary)
# 横着切过去,上一行 下一行的数值几乎都是黑色,那这一行就归为黑色
list = []
for x in range(picRow):
whitePoints = []
newLenStart = 0
for y in range(picCol - 1):
if binary[x][y] > 0 and binary[x][y + 1] > 0:
newLenStart = y
whitePoints.append(newLenStart)
# print(whitePoints)
# if binary[x][y] > 0:
# whitePoints.append(y)
list.append(whitePoints)
print('list1',list[1])
# print(len(list[0]))
for x in range(len(list) - 1):
# 第一行的判断
if x == 0:
if list[x + 1] == [] and list[x + 2] == [] and len(list[x]) != 0:
for y in range(picCol):
binary[x][y] = 0
# 最后一行的判断
elif x == len(list):
if list[x - 1] == [] and list[x - 2] == [] and len(list[x]) != 0:
for y in range(picCol):
binary[x][y] = 0
# 中间行的判断
else:
if list[x - 1] == [] and list[x + 1] == [] and len(list[x]) != 0:
for y in range(picCol):
binary[x][y] = 0
cv2.imshow("last process binary: ", binary)
testPic = newRoi.copy()
reImg, contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
picPoints = []
# widthList = []
for contour in contours:
if cv2.contourArea(contour) < 300:
continue
# print(contour)
x, y, w, h = cv2.boundingRect(contour)
if h < picRow / 2.5:
continue
if w < 8:
continue
picPoints.append([x, y, w, h])
print(picPoints)
picPoints.sort(key=operator.itemgetter(0))
print(picPoints)
# 点不包含汉字在内
if len(picPoints) == 7:
picPoints.remove(picPoints[0])
# print(picPoints)
x_points_start = []
x_points_end = []
y_start = picPoints[0][1] - 5
y_end = picPoints[0][1] + picPoints[0][3] + 5
for xx, yy, ww, hh in picPoints:
testPic = cv2.rectangle(testPic, (xx - 5, yy - 5), (xx + ww + 5, yy + hh + 5), (0, 255, 0), 2)
x_points_start.append(xx - 5)
x_points_end.append(xx + ww + 5)
print(x_points_start)
print(x_points_end)
firstWidth = x_points_end[0] - x_points_start[0]
firstX = x_points_start[0] - firstWidth
firstX_End = x_points_start[0]
print(firstX, firstX_End)
chinese_pic = None
# 判断汉字左边和上边越界问题
if firstX < 0:
if y_start < 0:
cv2.imshow("FirstNumber", binary[0:y_end, 0:firstX_End])
chinese_pic = binary[0:y_end, 0:firstX_End].copy()
else:
cv2.imshow("FirstNumber", binary[y_start:y_end, 0:firstX_End])
chinese_pic = binary[y_start:y_end, 0:firstX_End].copy()
else:
if y_start < 0:
cv2.imshow("FirstNumber", binary[0:y_end, firstX:firstX_End])
chinese_pic = binary[0:y_end, firstX:firstX_End].copy()
else:
cv2.imshow("FirstNumber", binary[y_start:y_end, firstX:firstX_End])
chinese_pic = binary[y_start:y_end, firstX:firstX_End].copy()
cv2.imshow('test', testPic)
cv2.imshow('chinese', chinese_pic)
cv2.imwrite('p0.jpg', chinese_pic)
#
for i in range(6):
if y_start < 0:
cv2.imshow("第" + str(i) + "个", binary[0:y_end, x_points_start[i]:x_points_end[i]])
cv2.imwrite('p'+str(i+1)+'.jpg', binary[0:y_end, x_points_start[i]:x_points_end[i]])
else:
cv2.imshow("第" + str(i) + "个", binary[y_start:y_end, x_points_start[i]:x_points_end[i]])
cv2.imwrite('p' + str(i + 1) + '.jpg', binary[0:y_end, x_points_start[i]:x_points_end[i]])
cv2.waitKey(0)