boxes_overlap.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. """
  32. rows = boxes1.shape[0]
  33. cols = boxes2.shape[0]
  34. intersect_areas = np.zeros((rows, cols), dtype=boxes1.dtype)
  35. if rows * cols == 0:
  36. return intersect_areas
  37. swap = False
  38. if boxes1.shape[0] > boxes2.shape[0]:
  39. boxes1, boxes2 = boxes2, boxes1
  40. intersect_areas = np.zeros((cols, rows), dtype=boxes1.dtype)
  41. swap = True
  42. for i in range(boxes1.shape[0]):
  43. x_begin = np.maximum(boxes1[i, 0], boxes2[:, 0])
  44. y_begin = np.maximum(boxes1[i, 1], boxes2[:, 1])
  45. x_end = np.minimum(boxes1[i, 2], boxes2[:, 2])
  46. y_end = np.minimum(boxes1[i, 3], boxes2[:, 3])
  47. x_end -= x_begin
  48. y_end -= y_begin
  49. np.maximum(x_end, 0, x_end)
  50. np.maximum(y_end, 0, y_end)
  51. x_end *= y_end
  52. intersect_areas[i, :] = x_end
  53. if swap:
  54. intersect_areas = intersect_areas.T
  55. return intersect_areas
  56. def paired_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
  57. """Compute paired overlap ratio between boxes.
  58. Args:
  59. boxes1: a numpy array with shape [N, 4] holding N boxes
  60. boxes2: a numpy array with shape [N, 4] holding N boxes
  61. ratio_type:
  62. iou: Intersection-over-union (iou).
  63. ioa: Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
  64. their intersection area over box2's area. Note that ioa is not symmetric,
  65. that is, IOA(box1, box2) != IOA(box2, box1).
  66. min: Compute the ratio as the area of intersection between box1 and box2,
  67. divided by the minimum area of the two bounding boxes.
  68. Returns:
  69. a numpy array with shape [N,] representing itemwise overlap ratio.
  70. References:
  71. `core.box_list_ops.matched_iou` in Tensorflow object detection API
  72. `structures.boxes.matched_boxlist_iou` in detectron2
  73. `mmdet.core.bbox.bbox_overlaps`, see https://mmdetection.readthedocs.io/en/v2.17.0/api.html#mmdet.core.bbox.bbox_overlaps
  74. """
  75. intersect_areas = paired_intersection(boxes1, boxes2)
  76. areas1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
  77. areas2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
  78. if ratio_type in ['union', 'iou']:
  79. union_areas = areas1 - intersect_areas
  80. union_areas += areas2
  81. intersect_areas /= union_areas
  82. elif ratio_type == 'min':
  83. min_areas = np.minimum(areas1, areas2)
  84. intersect_areas /= min_areas
  85. elif ratio_type == 'ioa':
  86. intersect_areas /= areas2
  87. else:
  88. raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))
  89. return intersect_areas
  90. def pairwise_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
  91. """Compute pairwise overlap ratio between boxes.
  92. Args:
  93. boxes1: a numpy array with shape [N, 4] holding N boxes
  94. boxes2: a numpy array with shape [M, 4] holding M boxes
  95. ratio_type:
  96. iou: Intersection-over-union (iou).
  97. ioa: Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
  98. their intersection area over box2's area. Note that ioa is not symmetric,
  99. that is, IOA(box1, box2) != IOA(box2, box1).
  100. min: Compute the ratio as the area of intersection between box1 and box2,
  101. divided by the minimum area of the two bounding boxes.
  102. Returns:
  103. a numpy array with shape [N, M] representing pairwise overlap ratio.
  104. References:
  105. `utils.np_box_ops.iou` in Tensorflow object detection API
  106. `utils.np_box_ops.ioa` in Tensorflow object detection API
  107. `utils.np_box_ops.giou` in Tensorflow object detection API
  108. `mmdet.core.bbox.bbox_overlaps`, see https://mmdetection.readthedocs.io/en/v2.17.0/api.html#mmdet.core.bbox.bbox_overlaps
  109. `torchvision.ops.box_iou`, see https://pytorch.org/vision/stable/ops.html#torchvision.ops.box_iou
  110. `torchvision.ops.generalized_box_iou`, see https://pytorch.org/vision/stable/ops.html#torchvision.ops.generalized_box_iou
  111. http://ww2.mathworks.cn/help/vision/ref/bboxoverlapratio.html
  112. """
  113. intersect_areas = pairwise_intersection(boxes1, boxes2)
  114. areas1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
  115. areas2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
  116. if ratio_type in ['union', 'iou']:
  117. union_areas = np.expand_dims(areas1, axis=1) - intersect_areas
  118. union_areas += np.expand_dims(areas2, axis=0)
  119. intersect_areas /= union_areas
  120. elif ratio_type == 'min':
  121. min_areas = np.minimum(np.expand_dims(areas1, axis=1), np.expand_dims(areas2, axis=0))
  122. intersect_areas /= min_areas
  123. elif ratio_type == 'ioa':
  124. intersect_areas /= np.expand_dims(areas2, axis=0)
  125. else:
  126. raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))
  127. return intersect_areas