boxes_overlap.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import numpy as np
  2. def paired_intersection(boxes1, boxes2):
  3. """Compute paired intersection areas between boxes.
  4. Args:
  5. boxes1: a numpy array with shape [N, 4] holding N boxes
  6. boxes2: a numpy array with shape [N, 4] holding N boxes
  7. Returns:
  8. a numpy array with shape [N,] representing itemwise intersection area
  9. References:
  10. `core.box_list_ops.matched_intersection` in Tensorflow object detection API
  11. Notes:
  12. can called as itemwise_intersection, matched_intersection, aligned_intersection
  13. """
  14. max_x_mins = np.maximum(boxes1[:, 0], boxes2[:, 0])
  15. max_y_mins = np.maximum(boxes1[:, 1], boxes2[:, 1])
  16. min_x_maxs = np.minimum(boxes1[:, 2], boxes2[:, 2])
  17. min_y_maxs = np.minimum(boxes1[:, 3], boxes2[:, 3])
  18. intersect_widths = np.maximum(0., min_x_maxs - max_x_mins)
  19. intersect_heights = np.maximum(0., min_y_maxs - max_y_mins)
  20. return intersect_widths * intersect_heights
  21. def pairwise_intersection(boxes1, boxes2):
  22. """Compute pairwise intersection areas between boxes.
  23. Args:
  24. boxes1: a numpy array with shape [N, 4] holding N boxes.
  25. boxes2: a numpy array with shape [M, 4] holding M boxes.
  26. Returns:
  27. a numpy array with shape [N, M] representing pairwise intersection area.
  28. References:
  29. `core.box_list_ops.intersection` in Tensorflow object detection API
  30. `utils.box_list_ops.intersection` in Tensorflow object detection API
  31. `core.evaluation.bbox_overlaps.bbox_overlaps` in mmdetection
  32. """
  33. rows = boxes1.shape[0]
  34. cols = boxes2.shape[0]
  35. intersect_areas = np.zeros((rows, cols), dtype=boxes1.dtype)
  36. if rows * cols == 0:
  37. return intersect_areas
  38. swap = False
  39. if boxes1.shape[0] > boxes2.shape[0]:
  40. boxes1, boxes2 = boxes2, boxes1
  41. intersect_areas = np.zeros((cols, rows), dtype=boxes1.dtype)
  42. swap = True
  43. for i in range(boxes1.shape[0]):
  44. x_begin = np.maximum(boxes1[i, 0], boxes2[:, 0])
  45. y_begin = np.maximum(boxes1[i, 1], boxes2[:, 1])
  46. x_end = np.minimum(boxes1[i, 2], boxes2[:, 2])
  47. y_end = np.minimum(boxes1[i, 3], boxes2[:, 3])
  48. x_end -= x_begin
  49. y_end -= y_begin
  50. np.maximum(x_end, 0, x_end)
  51. np.maximum(y_end, 0, y_end)
  52. x_end *= y_end
  53. intersect_areas[i, :] = x_end
  54. if swap:
  55. intersect_areas = intersect_areas.T
  56. return intersect_areas
  57. def paired_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
  58. """Compute paired overlap ratio between boxes.
  59. Args:
  60. boxes1: a numpy array with shape [N, 4] holding N boxes
  61. boxes2: a numpy array with shape [N, 4] holding N boxes
  62. ratio_type:
  63. iou: Intersection-over-union (iou).
  64. ioa: Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
  65. their intersection area over box2's area. Note that ioa is not symmetric,
  66. that is, IOA(box1, box2) != IOA(box2, box1).
  67. Returns:
  68. a numpy array with shape [N,] representing itemwise overlap ratio.
  69. """
  70. intersect_area = paired_intersection(boxes1, boxes2)
  71. area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
  72. area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
  73. if ratio_type in ['union', 'iou']:
  74. union_area = area1 - intersect_area
  75. union_area += area2
  76. intersect_area /= union_area
  77. elif ratio_type == 'min':
  78. min_area = np.minimum(area1, area2)
  79. intersect_area /= min_area
  80. elif ratio_type == 'ioa':
  81. intersect_area /= area2
  82. else:
  83. raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))
  84. return intersect_area
  85. def pairwise_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
  86. """Compute pairwise overlap ratio between boxes.
  87. Args:
  88. boxes1: a numpy array with shape [N, 4] holding N boxes
  89. boxes2: a numpy array with shape [M, 4] holding M boxes
  90. ratio_type:
  91. iou: Intersection-over-union (iou).
  92. ioa: Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
  93. their intersection area over box2's area. Note that ioa is not symmetric,
  94. that is, IOA(box1, box2) != IOA(box2, box1).
  95. Returns:
  96. a numpy array with shape [N, M] representing pairwise overlap ratio.
  97. References:
  98. `utils.np_box_ops.iou` in Tensorflow object detection API
  99. `utils.np_box_ops.ioa` in Tensorflow object detection API
  100. `core.evaluation.bbox_overlaps.bbox_overlaps` in mmdetection
  101. http://ww2.mathworks.cn/help/vision/ref/bboxoverlapratio.html
  102. """
  103. intersect_area = pairwise_intersection(boxes1, boxes2)
  104. area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
  105. area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
  106. if ratio_type in ['union', 'iou']:
  107. union_area = np.expand_dims(area1, axis=1) - intersect_area
  108. union_area += np.expand_dims(area2, axis=0)
  109. intersect_area /= union_area
  110. elif ratio_type == 'min':
  111. min_area = np.minimum(np.expand_dims(area1, axis=1), np.expand_dims(area2, axis=0))
  112. intersect_area /= min_area
  113. elif ratio_type == 'ioa':
  114. intersect_area /= np.expand_dims(area2, axis=0)
  115. else:
  116. raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))
  117. return intersect_area