Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. def calculate_iou(lhs_bboxes, rhs_bboxes):
  2. """Calculate iou of two bboxes
  3. bbox: (x1, y1, x2, y2)
  4.  
  5. Arguments:
  6. lhs_bboxes {np.ndarray} -- (m, 4)
  7. rhs_bboxes {np.ndarray} -- (n, 4)
  8.  
  9. Returns:
  10. np.ndarray -- (m, n)
  11. """
  12. lhs_bboxes = np.expand_dims(lhs_bboxes, 1)
  13. x_y_min = np.maximum(lhs_bboxes[:, :, :2], rhs_bboxes[:, :2])
  14. x_y_max = np.minimum(lhs_bboxes[:, :, 2:], rhs_bboxes[:, 2:])
  15. w_h = x_y_max - x_y_min
  16. w_h = np.clip(w_h, a_min=0, a_max=None)
  17. intersect_area = w_h[:, :, 0] * w_h[:, :, 1]
  18. lhs_area = ((lhs_bboxes[:, :, 2] - lhs_bboxes[:, :, 0]) *
  19. (lhs_bboxes[:, :, 3] - lhs_bboxes[:, :, 1]))
  20. rhs_area = ((rhs_bboxes[:, 2] - rhs_bboxes[:, 0]) *
  21. (rhs_bboxes[:, 3] - rhs_bboxes[:, 1]))
  22. union_area = lhs_area + rhs_area - intersect_area
  23. iou = intersect_area / union_area
  24. return iou
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement