File size: 1,144 Bytes
6d5cf07
 
 
 
 
 
 
 
 
 
 
 
 
 
7b69507
6d5cf07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np

def bbox_overlaps(boxes, query_boxes):
    """
    Parameters
    ----------
    boxes: (N, 4) ndarray of float
    query_boxes: (K, 4) ndarray of float
    Returns
    -------
    overlaps: (N, K) ndarray of overlap between boxes and query_boxes
    """
    N = boxes.shape[0]
    K = query_boxes.shape[0]
    overlaps = np.zeros((N, K), dtype=np.float32)
    
    box_area = (
        (query_boxes[:, 2] - query_boxes[:, 0] + 1) *
        (query_boxes[:, 3] - query_boxes[:, 1] + 1)
    )
    
    for n in range(N):
        iw = (
            np.maximum(0, np.minimum(boxes[n, 2], query_boxes[:, 2]) -
                       np.maximum(boxes[n, 0], query_boxes[:, 0]) + 1)
        )
        
        ih = (
            np.maximum(0, np.minimum(boxes[n, 3], query_boxes[:, 3]) -
                       np.maximum(boxes[n, 1], query_boxes[:, 1]) + 1)
        )
        
        ua = (
            (boxes[n, 2] - boxes[n, 0] + 1) *
            (boxes[n, 3] - boxes[n, 1] + 1) +
            box_area - iw * ih
        )
        
        overlaps[n, :] = np.where((iw > 0) & (ih > 0), iw * ih / ua, 0)
    
    return overlaps