-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbbox_transforms.py
417 lines (376 loc) · 13.3 KB
/
bbox_transforms.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
""" Original code is from:
`https://github.com/open-mmlab/mmrotate/blob/main/mmrotate/core/bbox/transforms.py`"""
import numpy as np
import math
import torch
import cv2
def swap_axis(tensor):
if torch.is_tensor(tensor):
swap_bbox = torch.zeros_like(tensor)
swap_bbox[:, 0] = tensor[:, 0]
swap_bbox[:, 1] = tensor[:, 1]
swap_bbox[:, 2] = tensor[:, 3]
swap_bbox[:, 3] = tensor[:, 2]
swap_bbox[:, 4] = tensor[:, 4]
else:
swap_bbox = np.zeros_like(tensor)
swap_bbox[:, 0] = tensor[:, 0]
swap_bbox[:, 1] = tensor[:, 1]
swap_bbox[:, 2] = tensor[:, 3]
swap_bbox[:, 3] = tensor[:, 2]
swap_bbox[:, 4] = tensor[:, 4]
return swap_bbox
def norm_angle(angle, angle_range):
"""Limit the range of angles.
Args:
angle (ndarray): shape(n, ).
angle_range (Str): angle representations.
Returns:
angle (ndarray): shape(n, ).
"""
if angle_range == 'oc':
return angle
elif angle_range == 'le135':
return (angle + np.pi / 4) % np.pi - np.pi / 4
elif angle_range == 'le90':
return (angle + np.pi / 2) % np.pi - np.pi / 2
else:
print('Not yet implemented.')
def obb2poly_oc(rboxes):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (torch.Tensor): [x_ctr,y_ctr,w,h,angle]
Returns:
polys (torch.Tensor): [x0,y0,x1,y1,x2,y2,x3,y3]
"""
x = rboxes[:, 0]
y = rboxes[:, 1]
w = rboxes[:, 2]
h = rboxes[:, 3]
a = rboxes[:, 4]
cosa = torch.cos(a)
sina = torch.sin(a)
wx, wy = w / 2 * cosa, w / 2 * sina
hx, hy = -h / 2 * sina, h / 2 * cosa
p1x, p1y = x - wx - hx, y - wy - hy
p2x, p2y = x + wx - hx, y + wy - hy
p3x, p3y = x + wx + hx, y + wy + hy
p4x, p4y = x - wx + hx, y - wy + hy
return torch.stack([p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y], dim=-1)
def obb2poly_le135(rboxes):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (torch.Tensor): [x_ctr,y_ctr,w,h,angle]
Returns:
polys (torch.Tensor): [x0,y0,x1,y1,x2,y2,x3,y3]
"""
N = rboxes.shape[0]
if N == 0:
return rboxes.new_zeros((rboxes.size(0), 8))
x_ctr, y_ctr, width, height, angle = rboxes.select(1, 0), rboxes.select(
1, 1), rboxes.select(1, 2), rboxes.select(1, 3), rboxes.select(1, 4)
tl_x, tl_y, br_x, br_y = \
-width * 0.5, -height * 0.5, \
width * 0.5, height * 0.5
rects = torch.stack([tl_x, br_x, br_x, tl_x, tl_y, tl_y, br_y, br_y],
dim=0).reshape(2, 4, N).permute(2, 0, 1)
sin, cos = torch.sin(angle), torch.cos(angle)
M = torch.stack([cos, -sin, sin, cos], dim=0).reshape(2, 2,
N).permute(2, 0, 1)
polys = M.matmul(rects).permute(2, 1, 0).reshape(-1, N).transpose(1, 0)
polys[:, ::2] += x_ctr.unsqueeze(1)
polys[:, 1::2] += y_ctr.unsqueeze(1)
return polys.contiguous()
def obb2poly_le90(rboxes):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (torch.Tensor): [x_ctr,y_ctr,w,h,angle]
Returns:
polys (torch.Tensor): [x0,y0,x1,y1,x2,y2,x3,y3]
"""
N = rboxes.shape[0]
if N == 0:
return rboxes.new_zeros((rboxes.size(0), 8))
x_ctr, y_ctr, width, height, angle = rboxes.select(1, 0), rboxes.select(
1, 1), rboxes.select(1, 2), rboxes.select(1, 3), rboxes.select(1, 4)
tl_x, tl_y, br_x, br_y = \
-width * 0.5, -height * 0.5, \
width * 0.5, height * 0.5
rects = torch.stack([tl_x, br_x, br_x, tl_x, tl_y, tl_y, br_y, br_y],
dim=0).reshape(2, 4, N).permute(2, 0, 1)
sin, cos = torch.sin(angle), torch.cos(angle)
M = torch.stack([cos, -sin, sin, cos], dim=0).reshape(2, 2,
N).permute(2, 0, 1)
polys = M.matmul(rects).permute(2, 1, 0).reshape(-1, N).transpose(1, 0)
polys[:, ::2] += x_ctr.unsqueeze(1)
polys[:, 1::2] += y_ctr.unsqueeze(1)
return polys.contiguous()
def cal_line_length(point1, point2):
"""Calculate the length of line.
Args:
point1 (List): [x,y]
point2 (List): [x,y]
Returns:
length (float)
"""
return math.sqrt(
math.pow(point1[0] - point2[0], 2) +
math.pow(point1[1] - point2[1], 2))
def get_best_begin_point_single(coordinate):
"""Get the best begin point of the single polygon.
Args:
coordinate (List): [x1, y1, x2, y2, x3, y3, x4, y4, score]
Returns:
reorder coordinate (List): [x1, y1, x2, y2, x3, y3, x4, y4, score]
"""
x1, y1, x2, y2, x3, y3, x4, y4, score = coordinate
xmin = min(x1, x2, x3, x4)
ymin = min(y1, y2, y3, y4)
xmax = max(x1, x2, x3, x4)
ymax = max(y1, y2, y3, y4)
combine = [[[x1, y1], [x2, y2], [x3, y3], [x4, y4]],
[[x2, y2], [x3, y3], [x4, y4], [x1, y1]],
[[x3, y3], [x4, y4], [x1, y1], [x2, y2]],
[[x4, y4], [x1, y1], [x2, y2], [x3, y3]]]
dst_coordinate = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]
force = 100000000.0
force_flag = 0
for i in range(4):
temp_force = cal_line_length(combine[i][0], dst_coordinate[0]) \
+ cal_line_length(combine[i][1], dst_coordinate[1]) \
+ cal_line_length(combine[i][2], dst_coordinate[2]) \
+ cal_line_length(combine[i][3], dst_coordinate[3])
if temp_force < force:
force = temp_force
force_flag = i
if force_flag != 0:
pass
return np.hstack(
(np.array(combine[force_flag]).reshape(8), np.array(score)))
def get_best_begin_point(coordinates):
"""Get the best begin points of polygons.
Args:
coordinate (ndarray): shape(n, 9).
Returns:
reorder coordinate (ndarray): shape(n, 9).
"""
coordinates = list(map(get_best_begin_point_single, coordinates.tolist()))
coordinates = np.array(coordinates)
return coordinates
def obb2poly_np_oc(rbboxes):
""" Modified !
Convert oriented bounding boxes to polygons.
Args:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle,score] modify-> [x_ctr, y_ctr, h, w, angle(radian), score]
Returns:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3,score]
"""
x = rbboxes[:, 0]
y = rbboxes[:, 1]
h = rbboxes[:, 2]
w = rbboxes[:, 3]
a = rbboxes[:, 4]
score = rbboxes[:, 5]
cosa = np.cos(a)
sina = np.sin(a)
wx, wy = -w / 2 * sina, w / 2 * cosa
hx, hy = h / 2 * cosa, h / 2 * sina
p1x, p1y = x - wx - hx, y - wy - hy
p2x, p2y = x - wx + hx, y - wy + hy
p3x, p3y = x + wx + hx, y + wy + hy
p4x, p4y = x + wx - hx, y + wy - hy
polys = np.stack([p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y, score], axis=-1)
polys = get_best_begin_point(polys)
return polys
def obb2poly_np_le135(rrects):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle,score]
Returns:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3,score]
"""
polys = []
for rrect in rrects:
x_ctr, y_ctr, width, height, angle, score = rrect[:6]
tl_x, tl_y, br_x, br_y = -width / 2, -height / 2, width / 2, height / 2
rect = np.array([[tl_x, br_x, br_x, tl_x], [tl_y, tl_y, br_y, br_y]])
R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
poly = R.dot(rect)
x0, x1, x2, x3 = poly[0, :4] + x_ctr
y0, y1, y2, y3 = poly[1, :4] + y_ctr
poly = np.array([x0, y0, x1, y1, x2, y2, x3, y3, score],
dtype=np.float32)
polys.append(poly)
polys = np.array(polys)
polys = get_best_begin_point(polys)
return polys
def obb2poly_np_le90(obboxes):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle,score]
Returns:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3,score]
"""
try:
center, w, h, theta, score = np.split(obboxes, (2, 3, 4, 5), axis=-1)
except: # noqa: E722
results = np.stack([0., 0., 0., 0., 0., 0., 0., 0., 0.], axis=-1)
return results.reshape(1, -1)
Cos, Sin = np.cos(theta), np.sin(theta)
vector1 = np.concatenate([w / 2 * Cos, w / 2 * Sin], axis=-1)
vector2 = np.concatenate([-h / 2 * Sin, h / 2 * Cos], axis=-1)
point1 = center - vector1 - vector2
point2 = center + vector1 - vector2
point3 = center + vector1 + vector2
point4 = center - vector1 + vector2
polys = np.concatenate([point1, point2, point3, point4, score], axis=-1)
polys = get_best_begin_point(polys)
return polys
def obb2poly(rbboxes, version='oc'):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (torch.Tensor): [x_ctr,y_ctr,w,h,angle]
version (Str): angle representations.
Returns:
polys (torch.Tensor): [x0,y0,x1,y1,x2,y2,x3,y3]
"""
if version == 'oc':
results = obb2poly_oc(rbboxes)
elif version == 'le135':
results = obb2poly_le135(rbboxes)
elif version == 'le90':
results = obb2poly_le90(rbboxes)
else:
raise NotImplementedError
return results
def obb2poly_np(rbboxes, version='oc'):
"""Convert oriented bounding boxes to polygons.
Args:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle]
version (Str): angle representations.
Returns:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]
"""
if version == 'oc':
results = obb2poly_np_oc(rbboxes)
elif version == 'le135':
results = obb2poly_np_le135(rbboxes)
elif version == 'le90':
results = obb2poly_np_le90(rbboxes)
else:
raise NotImplementedError
return results
def poly2obb_np(polys, version='oc'):
"""Convert polygons to oriented bounding boxes.
Args:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]
version (Str): angle representations.
Returns:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle]
"""
if version == 'oc':
results = poly2obb_np_oc(polys)
elif version == 'le135':
results = poly2obb_np_le135(polys)
elif version == 'le90':
results = poly2obb_np_le90(polys)
else:
raise NotImplementedError
return results
def poly2obb_np_oc(poly):
""" Modified !!
Convert polygons to oriented bounding boxes.
Args:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]
Returns:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle] modified -> [x_ctr, y_ctr, h, w, angle(radian)]
"""
bboxps = np.array(poly).reshape((4, 2))
rbbox = cv2.minAreaRect(bboxps)
x, y, h, w, a = rbbox[0][0], rbbox[0][1], rbbox[1][0], rbbox[1][1], rbbox[2]
# assert 0 < a <= 90, f'error from poly2obb_np_oc function.'
if w < 2 or h < 2:
return
while not 0 < a <= 90:
if a == -90:
a += 180
else:
a += 90
w, h = h, w
a = a / 180 * np.pi
assert 0 < a <= np.pi / 2
return x, y, h, w, a
def poly2obb_np_le135(poly):
"""Convert polygons to oriented bounding boxes.
Args:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]
Returns:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle]
"""
poly = np.array(poly[:8], dtype=np.float32)
pt1 = (poly[0], poly[1])
pt2 = (poly[2], poly[3])
pt3 = (poly[4], poly[5])
pt4 = (poly[6], poly[7])
edge1 = np.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) + (pt1[1] - pt2[1]) *
(pt1[1] - pt2[1]))
edge2 = np.sqrt((pt2[0] - pt3[0]) * (pt2[0] - pt3[0]) + (pt2[1] - pt3[1]) *
(pt2[1] - pt3[1]))
if edge1 < 2 or edge2 < 2:
return
width = max(edge1, edge2)
height = min(edge1, edge2)
angle = 0
if edge1 > edge2:
angle = np.arctan2(float(pt2[1] - pt1[1]), float(pt2[0] - pt1[0]))
elif edge2 >= edge1:
angle = np.arctan2(float(pt4[1] - pt1[1]), float(pt4[0] - pt1[0]))
angle = norm_angle(angle, 'le135')
x_ctr = float(pt1[0] + pt3[0]) / 2
y_ctr = float(pt1[1] + pt3[1]) / 2
return x_ctr, y_ctr, width, height, angle
def poly2obb_np_le90(poly):
"""Convert polygons to oriented bounding boxes.
Args:
polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]
Returns:
obbs (ndarray): [x_ctr,y_ctr,w,h,angle]
"""
bboxps = np.array(poly).reshape((4, 2))
rbbox = cv2.minAreaRect(bboxps)
x, y, w, h, a = rbbox[0][0], rbbox[0][1], rbbox[1][0], rbbox[1][1], rbbox[
2]
if w < 2 or h < 2:
return
a = a / 180 * np.pi
if w < h:
w, h = h, w
a += np.pi / 2
while not np.pi / 2 > a >= -np.pi / 2:
if a >= np.pi / 2:
a -= np.pi
else:
a += np.pi
assert np.pi / 2 > a >= -np.pi / 2
return x, y, w, h, a
def obb2hbb_oc(rbboxes):
""" Modified !
Convert oriented bounding boxes to horizontal bounding boxes.
Args:
obbs (torch.Tensor): [x_ctr,y_ctr,w,h,angle] modify -> [x_ctr, y_ctr, h, w, angle(radian)]
Returns:
hbbs (torch.Tensor): [x_ctr,y_ctr,w,h,pi/2]
"""
h = rbboxes[:, 2::5]
w = rbboxes[:, 3::5]
a = rbboxes[:, 4::5]
cosa = torch.cos(a)
sina = torch.sin(a)
hbbox_h = cosa * w + sina * h
hbbox_w = sina * w + cosa * h
hbboxes = rbboxes.clone().detach()
hbboxes[:, 2::5] = hbbox_w
hbboxes[:, 3::5] = hbbox_h
hbboxes[:, 4::5] = np.pi / 2
return hbboxes