-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathimage.py
More file actions
256 lines (207 loc) · 6.62 KB
/
Copy pathimage.py
File metadata and controls
256 lines (207 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 8 22:49:54 2019
image
@author: chineseocr
"""
from PIL import Image
import numpy as np
import cv2
def draw_boxes(im, bboxes,color=(0,0,0)):
"""
boxes: bounding boxes
"""
tmp = np.copy(im)
c = color
h, w, = im.shape[:2]
thick = int((h + w) / 300)
i = 0
for box in bboxes:
x1,y1,x2,y2,x3,y3,x4,y4 = box[:8]
cx =np.mean([x1,x2,x3,x4])
cy = np.mean([y1,y2,y3,y4])
cv2.line(tmp,(int(x1),int(y1)),(int(x2),int(y2)),c,1,lineType=cv2.LINE_AA)
cv2.line(tmp,(int(x2),int(y2)),(int(x3),int(y3)),c,1,lineType=cv2.LINE_AA)
cv2.line(tmp,(int(x3),int(y3)),(int(x4),int(y4)),c,1,lineType=cv2.LINE_AA)
cv2.line(tmp,(int(x4),int(y4)),(int(x1),int(y1)),c,1,lineType=cv2.LINE_AA)
mess=str(i)
cv2.putText(tmp, mess, (int(cx), int(cy)),0, 1e-3 * h, c, thick // 2)
i+=1
return tmp
def draw_lines(im, bboxes,color=(0,0,0),lineW=3):
"""
boxes: bounding boxes
"""
tmp = np.copy(im)
c = color
h, w = im.shape[:2]
i = 0
for box in bboxes:
x1,y1,x2,y2= box
cv2.line(tmp,(int(x1),int(y1)),(int(x2),int(y2)),c,lineW,lineType=cv2.LINE_AA)
i+=1
return tmp
def letterbox_image(image, size):
'''resize image with unchanged aspect ratio using padding'''
image_w, image_h = image.size
w, h = size
new_w = int(image_w * min(w*1.0/image_w, h*1.0/image_h))
new_h = int(image_h * min(w*1.0/image_w, h*1.0/image_h))
resized_image = image.resize((new_w,new_h), Image.BICUBIC)
fx = new_w/image_w
fy = new_h/image_h
dx = (w-new_w)//2
dy = (h-new_h)//2
boxed_image = Image.new('RGB', size, (128,128,128))
boxed_image.paste(resized_image, (dx,dy))
return boxed_image,fx,fy,dx,dy
def exp(x):
x = np.clip(x,-6,6)
y = 1 / (1 + np.exp(-x))
return y
def minAreaLine(coords):
"""
"""
rect=cv2.minAreaRect(coords[:,::-1])
box = cv2.boxPoints(rect)
box = box.reshape((8,)).tolist()
box = sort_box(box)
x1,y1,x2,y2,x3,y3,x4,y4=box
degree,w,h,cx,cy = solve(box)
if w<h:
xmin =(x1+x2)/2
xmax = (x3+x4)/2
ymin = (y1+y2)/2
ymax = (y3+y4)/2
else:
xmin =(x1+x4)/2
xmax = (x2+x3)/2
ymin = (y1+y4)/2
ymax = (y2+y3)/2
return [xmin,ymin,xmax,ymax]
def minAreaRectBox(coords):
"""
多边形外接矩形
"""
rect=cv2.minAreaRect(coords[:,::-1])
box = cv2.boxPoints(rect)
box = box.reshape((8,)).tolist()
box = sort_box(box)
return box
def sort_box(box):
x1,y1,x2,y2,x3,y3,x4,y4 = box[:8]
pts = (x1,y1),(x2,y2),(x3,y3),(x4,y4)
pts = np.array(pts, dtype="float32")
(x1,y1),(x2,y2),(x3,y3),(x4,y4) = _order_points(pts)
return x1,y1,x2,y2,x3,y3,x4,y4
from scipy.spatial import distance as dist
def _order_points(pts):
# 根据x坐标对点进行排序
"""
---------------------
作者:Tong_T
来源:CSDN
原文:https://blog.csdn.net/Tong_T/article/details/81907132
版权声明:本文为博主原创文章,转载请附上博文链接!
"""
x_sorted = pts[np.argsort(pts[:, 0]), :]
# 从排序中获取最左侧和最右侧的点
# x坐标点
left_most = x_sorted[:2, :]
right_most = x_sorted[2:, :]
# 现在,根据它们的y坐标对最左边的坐标进行排序,这样我们就可以分别抓住左上角和左下角
left_most = left_most[np.argsort(left_most[:, 1]), :]
(tl, bl) = left_most
# 现在我们有了左上角坐标,用它作为锚来计算左上角和右上角之间的欧氏距离;
# 根据毕达哥拉斯定理,距离最大的点将是我们的右下角
distance = dist.cdist(tl[np.newaxis], right_most, "euclidean")[0]
(br, tr) = right_most[np.argsort(distance)[::-1], :]
# 返回左上角,右上角,右下角和左下角的坐标
return np.array([tl, tr, br, bl], dtype="float32")
def solve(box):
"""
绕 cx,cy点 w,h 旋转 angle 的坐标
x = cx-w/2
y = cy-h/2
x1-cx = -w/2*cos(angle) +h/2*sin(angle)
y1 -cy= -w/2*sin(angle) -h/2*cos(angle)
h(x1-cx) = -wh/2*cos(angle) +hh/2*sin(angle)
w(y1 -cy)= -ww/2*sin(angle) -hw/2*cos(angle)
(hh+ww)/2sin(angle) = h(x1-cx)-w(y1 -cy)
"""
x1,y1,x2,y2,x3,y3,x4,y4= box[:8]
cx = (x1+x3+x2+x4)/4.0
cy = (y1+y3+y4+y2)/4.0
w = (np.sqrt((x2-x1)**2+(y2-y1)**2)+np.sqrt((x3-x4)**2+(y3-y4)**2))/2
h = (np.sqrt((x2-x3)**2+(y2-y3)**2)+np.sqrt((x1-x4)**2+(y1-y4)**2))/2
#x = cx-w/2
#y = cy-h/2
sinA = (h*(x1-cx)-w*(y1 -cy))*1.0/(h*h+w*w)*2
angle = np.arcsin(sinA)
return angle,w,h,cx,cy
#####################直线处理#####################
def fit_line(p1,p2):
"""A = Y2 - Y1
B = X1 - X2
C = X2*Y1 - X1*Y2
AX+BY+C=0
直线一般方程
"""
x1,y1 = p1
x2,y2 = p2
A = y2-y1
B = x1-x2
C = x2*y1-x1*y2
return A,B,C
def line_point_line(point1,point2):
"""
A1x+B1y+C1=0
A2x+B2y+C2=0
x = (B1*C2-B2*C1)/(A1*B2-A2*B1)
y = (A2*C1-A1*C2)/(A1*B2-A2*B1)
求解两条直线的交点
"""
A1,B1,C1 = fit_line(point1[0],point1[1])
A2,B2,C2 = fit_line(point2[0],point2[1])
x = (B1*C2-B2*C1)/(A1*B2-A2*B1)
y = (A2*C1-A1*C2)/(A1*B2-A2*B1)
return x,y
def sqrt(p1,p2):
return np.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
def point_to_points(p,points,alpha=10):
##点到点之间的距离
sqList = [ sqrt(p,point) for point in points]
if max(sqList)<alpha:
return True
else:
return False
def point_line_cor(p,A,B,C):
##判断点与之间的位置关系
#一般式直线方程(Ax+By+c)=0
x,y = p
r = A*x+B*y+C
return r
def line_to_line(points1,points2,alpha=10):
"""
线段之间的距离
"""
x1,y1,x2,y2 = points1
ox1,oy1,ox2,oy2 = points2
A1,B1,C1 = fit_line((x1,y1),(x2,y2))
A2,B2,C2 = fit_line((ox1,oy1),(ox2,oy2))
flag1 = point_line_cor([x1,y1],A2,B2,C2)
flag2 = point_line_cor([x2,y2],A2,B2,C2)
if (flag1>0 and flag2>0) or (flag1<0 and flag2<0):
x = (B1*C2-B2*C1)/(A1*B2-A2*B1)
y = (A2*C1-A1*C2)/(A1*B2-A2*B1)
p = (x,y)
r0 = sqrt(p,(x1,y1))
r1 = sqrt(p,(x2,y2))
if min(r0,r1)<alpha:
if r0<r1:
points1 = [p[0],p[1],x2,y2]
else:
points1 = [x1,y1,p[0],p[1]]
return points1
#####################直线处理#####################