bbox.pyx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # --------------------------------------------------------
  2. # Fast R-CNN
  3. # Copyright (c) 2015 Microsoft
  4. # Licensed under The MIT License [see LICENSE for details]
  5. # Written by Sergey Karayev
  6. # --------------------------------------------------------
  7. cimport cython
  8. import numpy as np
  9. cimport numpy as np
  10. DTYPE = np.float
  11. ctypedef np.float_t DTYPE_t
  12. def bbox_overlaps(
  13. np.ndarray[DTYPE_t, ndim=2] boxes,
  14. np.ndarray[DTYPE_t, ndim=2] query_boxes):
  15. """
  16. Parameters
  17. ----------
  18. boxes: (N, 4) ndarray of float
  19. query_boxes: (K, 4) ndarray of float
  20. Returns
  21. -------
  22. overlaps: (N, K) ndarray of overlap between boxes and query_boxes
  23. """
  24. cdef unsigned int N = boxes.shape[0]
  25. cdef unsigned int K = query_boxes.shape[0]
  26. cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
  27. cdef DTYPE_t iw, ih, box_area
  28. cdef DTYPE_t ua
  29. cdef unsigned int k, n
  30. for k in range(K):
  31. box_area = (
  32. (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
  33. (query_boxes[k, 3] - query_boxes[k, 1] + 1)
  34. )
  35. for n in range(N):
  36. iw = (
  37. min(boxes[n, 2], query_boxes[k, 2]) -
  38. max(boxes[n, 0], query_boxes[k, 0]) + 1
  39. )
  40. if iw > 0:
  41. ih = (
  42. min(boxes[n, 3], query_boxes[k, 3]) -
  43. max(boxes[n, 1], query_boxes[k, 1]) + 1
  44. )
  45. if ih > 0:
  46. ua = float(
  47. (boxes[n, 2] - boxes[n, 0] + 1) *
  48. (boxes[n, 3] - boxes[n, 1] + 1) +
  49. box_area - iw * ih
  50. )
  51. overlaps[n, k] = iw * ih / ua
  52. return overlaps