-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbox.pyx
33 lines (23 loc) · 1.27 KB
/
bbox.pyx
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
cimport cython
import numpy as np
cimport numpy as np
DTYPE = np.float
ctypedef np.float_t DTYPE_t
def bbox_overlaps(np.ndarray[DTYPE_t, ndim=2] anchors, np.ndarray[DTYPE_t, ndim=2] ground_truth):
cdef unsigned int N = anchors.shape[0]
cdef unsigned int K = ground_truth.shape[0]
cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
cdef DTYPE_t box_area
cdef DTYPE_t intersection_width, intersection_height
cdef unsigned int k, n
for k in range(K):
box_area = (ground_truth[k, 2] - ground_truth[k, 0] + 1) * (ground_truth[k, 3] - ground_truth[k, 1] + 1)
for n in range(N):
intersection_width = min(anchors[n, 2], ground_truth[k, 2]) - max(anchors[n, 0], ground_truth[k, 0]) + 1
if intersection_width > 0:
intersection_height = min(anchors[n, 3], ground_truth[k, 3]) - max(anchors[n, 1], ground_truth[k, 1]) + 1
if intersection_height > 0:
union_area = float((anchors[n, 2] - anchors[n, 0] + 1) * (anchors[n, 3] - anchors[n, 1] + 1) +
box_area - intersection_width * intersection_height)
overlaps[n, k] = intersection_width * intersection_height / float(union_area)
return overlaps