Ver Fonte

add paired_overlap_ratio

quarrying há 3 anos atrás
pai
commit
bd670916e2
1 ficheiros alterados com 38 adições e 7 exclusões
  1. 38 7
      khandy/boxes/boxes_overlap.py

+ 38 - 7
khandy/boxes/boxes_overlap.py

@@ -2,7 +2,7 @@ import numpy as np
 
 
 def paired_intersection(boxes1, boxes2):
-    """
+    """Compute paired intersection areas between boxes.
     Args:
         boxes1: a numpy array with shape [N, 4] holding N boxes
         boxes2: a numpy array with shape [N, 4] holding N boxes
@@ -16,12 +16,10 @@ def paired_intersection(boxes1, boxes2):
     Notes:
         can called as itemwise_intersection, matched_intersection, aligned_intersection
     """
-    x_mins1, y_mins1, x_maxs1, y_maxs1 = np.split(boxes1[:,:4], 4, axis=1)
-    x_mins2, y_mins2, x_maxs2, y_maxs2 = np.split(boxes2[:,:4], 4, axis=1)
-    max_x_mins = np.maximum(x_mins1, x_mins2)
-    min_x_maxs = np.minimum(x_maxs1, x_maxs2)
-    max_y_mins = np.maximum(y_mins1, y_mins2)
-    min_y_maxs = np.minimum(y_maxs1, y_maxs2)
+    max_x_mins = np.maximum(boxes1[:, 0], boxes2[:, 0])
+    max_y_mins = np.maximum(boxes1[:, 1], boxes2[:, 1])
+    min_x_maxs = np.minimum(boxes1[:, 2], boxes2[:, 2])
+    min_y_maxs = np.minimum(boxes1[:, 3], boxes2[:, 3])
     intersect_widths = np.maximum(0., min_x_maxs - max_x_mins)
     intersect_heights = np.maximum(0., min_y_maxs - max_y_mins)
     return intersect_widths * intersect_heights
@@ -69,6 +67,39 @@ def pairwise_intersection(boxes1, boxes2):
     return intersect_areas
     
     
+def paired_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
+    """Compute paired overlap ratio between boxes.
+    
+    Args:
+        boxes1: a numpy array with shape [N, 4] holding N boxes
+        boxes2: a numpy array with shape [N, 4] holding N boxes
+        ratio_type:
+            iou: Intersection-over-union (iou).
+            ioa: Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
+                their intersection area over box2's area. Note that ioa is not symmetric,
+                that is, IOA(box1, box2) != IOA(box2, box1).
+                
+    Returns:
+        a numpy array with shape [N,] representing itemwise overlap ratio.
+    """
+    intersect_area = paired_intersection(boxes1, boxes2)
+    area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
+    area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
+    
+    if ratio_type in ['union', 'iou']:
+        union_area = area1 - intersect_area
+        union_area += area2
+        intersect_area /= union_area
+    elif ratio_type == 'min':
+        min_area = np.minimum(area1, area2)
+        intersect_area /= min_area
+    elif ratio_type == 'ioa':
+        intersect_area /= area2
+    else:
+        raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))
+    return intersect_area
+
+
 def pairwise_overlap_ratio(boxes1, boxes2, ratio_type='iou'):
     """Compute pairwise overlap ratio between boxes.