-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.py
More file actions
110 lines (103 loc) · 6.62 KB
/
core.py
File metadata and controls
110 lines (103 loc) · 6.62 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
import cv2
import numpy as np
def locate(img_src, img_mask):
"""
该函数通过cv2对img_mask进行边缘检测,获取车牌区域的边缘坐标(存储在contours中)和最小外接矩形4个端点坐标,
再从车牌的边缘坐标中计算出和最小外接矩形4个端点最近的点即为平行四边形车牌的四个端点,从而实现车牌的定位
:param img_src: 原始图片
:param img_mask: 通过u_net进行图像分隔得到的二值化图片,车牌区域呈现白色,背景区域为黑色
:return: 定位的车牌
"""
contours, hierarchy = cv2.findContours(img_mask[:, :, 0], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not len(contours): # contours1长度为0说明未检测到车牌
# print("未检测到车牌")
return [], []
else:
Lic_img = []
img_src_copy = img_src.copy() # img_src_copy用于绘制出定位的车牌轮廓
for ii, cont in enumerate(contours):
x, y, w, h = cv2.boundingRect(cont) # 获取最小外接矩形
img_cut_mask = img_mask[y:y + h, x:x + w] # 将标签车牌区域截取出来
# cv2.imshow('img_cut_mask',img_cut_mask)
# cv2.waitKey(0)
# print('w,h,均值,宽高比',w,h,np.mean(img_cut_mask),w/h)
# contours中除了车牌区域可能会有宽或高都是1或者2这样的小噪点,
# 而待选车牌区域的均值应较高,且宽和高不会非常小,因此通过以下条件进行筛选
if np.mean(img_cut_mask) >= 75 and w > 15 and h > 15:
rect = cv2.minAreaRect(cont) # 针对坐标点获取带方向角的最小外接矩形,中心点坐标,宽高,旋转角度
box = cv2.boxPoints(rect).astype(np.int32) # 获取最小外接矩形四个顶点坐标
cv2.drawContours(img_mask, contours, -1, (0, 0, 255), 2)
cv2.drawContours(img_mask, [box], 0, (0, 255, 0), 2)
# cv2.imshow('img_mask',img_mask)
cv2.waitKey(0)
cont = cont.reshape(-1, 2).tolist()
# 由于转换矩阵的两组坐标位置需要一一对应,因此需要将最小外接矩形的坐标进行排序,最终排序为[左上,左下,右上,右下]
box = sorted(box, key=lambda xy: xy[0]) # 先按照左右进行排序,分为左侧的坐标和右侧的坐标
box_left, box_right = box[:2], box[2:] # 此时box的前2个是左侧的坐标,后2个是右侧的坐标
box_left = sorted(box_left, key=lambda x: x[1]) # 再按照上下即y进行排序,此时box_left中为左上和左下两个端点坐标
box_right = sorted(box_right, key=lambda x: x[1]) # 此时box_right中为右上和右下两个端点坐标
box = np.array(box_left + box_right) # [左上,左下,右上,右下]
# print(box)
x0, y0 = box[0][0], box[0][1] # 这里的4个坐标即为最小外接矩形的四个坐标,接下来需获取平行(或不规则)四边形的坐标
x1, y1 = box[1][0], box[1][1]
x2, y2 = box[2][0], box[2][1]
x3, y3 = box[3][0], box[3][1]
def point_to_line_distance(X, Y):
if x2 - x0:
k_up = (y2 - y0) / (x2 - x0) # 斜率不为无穷大
d_up = abs(k_up * X - Y + y2 - k_up * x2) / (k_up ** 2 + 1) ** 0.5
else: # 斜率无穷大
d_up = abs(X - x2)
if x1 - x3:
k_down = (y1 - y3) / (x1 - x3) # 斜率不为无穷大
d_down = abs(k_down * X - Y + y1 - k_down * x1) / (k_down ** 2 + 1) ** 0.5
else: # 斜率无穷大
d_down = abs(X - x1)
return d_up, d_down
d0, d1, d2, d3 = np.inf, np.inf, np.inf, np.inf
l0, l1, l2, l3 = (x0, y0), (x1, y1), (x2, y2), (x3, y3)
for each in cont: # 计算cont中的坐标与矩形四个坐标的距离以及到上下两条直线的距离,对距离和进行权重的添加,成功计算选出四边形的4个顶点坐标
x, y = each[0], each[1]
dis0 = (x - x0) ** 2 + (y - y0) ** 2
dis1 = (x - x1) ** 2 + (y - y1) ** 2
dis2 = (x - x2) ** 2 + (y - y2) ** 2
dis3 = (x - x3) ** 2 + (y - y3) ** 2
d_up, d_down = point_to_line_distance(x, y)
weight = 0.987
if weight * d_up + (1 - weight) * dis0 < d0: # 小于则更新
d0 = weight * d_up + (1 - weight) * dis0
l0 = (x, y)
if weight * d_down + (1 - weight) * dis1 < d1:
d1 = weight * d_down + (1 - weight) * dis1
l1 = (x, y)
if weight * d_up + (1 - weight) * dis2 < d2:
d2 = weight * d_up + (1 - weight) * dis2
l2 = (x, y)
if weight * d_down + (1 - weight) * dis3 < d3:
d3 = weight * d_down + (1 - weight) * dis3
l3 = (x, y)
# print([l0,l1,l2,l3])
# for l in [l0, l1, l2, l3]:
# cv2.circle(img=img_mask, color=(0, 255, 255), center=tuple(l), thickness=2, radius=2)
# cv2.imshow('img_mask',img_mask)
# cv2.waitKey(0)
cv2.drawContours(img_src_copy, [np.array([l0, l1, l3, l2])], -1, (0, 255, 0), 1) # 在img_src_copy上绘制出定位的车牌轮廓,(0, 255, 0)表示绘制线条为绿色
return img_src_copy
def erode(image):
# print(image.shape)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#cv.imshow("binary", binary)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))#定义结构元素的形状和大小
dst = cv2.erode(binary, kernel)#腐蚀操作
# cv2.imshow("erode_demo", dst)
return dst
def dilate(image):
print(image.shape)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#cv.imshow("binary", binary)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))#定义结构元素的形状和大小
dst = cv2.dilate(binary, kernel)#膨胀操作
# cv2.imshow("dilate_demo", dst)
return dst