py_bbox.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # @Author : lightXu
  2. # @File : py_bbox.py
  3. import numpy as np
  4. def bbox_overlaps(boxes, query_boxes):
  5. """
  6. Parameters
  7. ----------
  8. boxes: (N, 4) ndarray of float
  9. query_boxes: (K, 4) ndarray of float
  10. Returns
  11. -------
  12. overlaps: (N, K) ndarray of overlap between boxes and query_boxes
  13. """
  14. N = boxes.shape[0]
  15. K = query_boxes.shape[0]
  16. overlaps = np.zeros((N, K), dtype=np.float)
  17. for k in range(K):
  18. box_area = (
  19. (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
  20. (query_boxes[k, 3] - query_boxes[k, 1] + 1)
  21. )
  22. for n in range(N):
  23. iw = (
  24. min(boxes[n, 2], query_boxes[k, 2]) -
  25. max(boxes[n, 0], query_boxes[k, 0]) + 1
  26. )
  27. if iw > 0:
  28. ih = (
  29. min(boxes[n, 3], query_boxes[k, 3]) -
  30. max(boxes[n, 1], query_boxes[k, 1]) + 1
  31. )
  32. if ih > 0:
  33. ua = float(
  34. (boxes[n, 2] - boxes[n, 0] + 1) *
  35. (boxes[n, 3] - boxes[n, 1] + 1) +
  36. box_area - iw * ih
  37. )
  38. overlaps[n, k] = iw * ih / ua
  39. return overlaps