File size: 982 Bytes
d44e389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np


def convert_to_square(bboxes):
    """Convert bounding boxes to a square form.
    Arguments:
        bboxes: a float numpy array of shape [n, 4].
    Returns:
        a float numpy array of shape [4],
            squared bounding boxes.
    """

    square_bboxes = np.zeros_like(bboxes)
    x1, y1, x2, y2 = bboxes
    h = y2 - y1 + 1.0
    w = x2 - x1 + 1.0
    max_side = np.maximum(h, w)
    square_bboxes[0] = x1 + w * 0.5 - max_side * 0.5
    square_bboxes[1] = y1 + h * 0.5 - max_side * 0.5
    square_bboxes[2] = square_bboxes[0] + max_side - 1.0
    square_bboxes[3] = square_bboxes[1] + max_side - 1.0
    return square_bboxes


def scale_box(box, scale):
    x1, y1, x2, y2 = box
    center_x, center_y = (x1 + x2) / 2, (y1 + y2) / 2
    w, h = x2 - x1, y2 - y1
    new_w, new_h = w * scale, h * scale
    y1, y2, x1, x2 = center_y - new_h / 2, center_y + new_h / 2, center_x - new_w / 2, center_x + new_w / 2,
    return np.array((x1, y1, x2, y2))