feat(slices): implement def polys_iou
This commit is contained in:
parent
542141e5c0
commit
e3504f7122
1 changed files with 39 additions and 0 deletions
|
|
@ -181,3 +181,42 @@ class PhotoSlice:
|
||||||
)
|
)
|
||||||
self.normalize_top_left()
|
self.normalize_top_left()
|
||||||
self.side_handles_enabled = True
|
self.side_handles_enabled = True
|
||||||
|
|
||||||
|
|
||||||
|
# polys_iou: function definition for ScanSlicer behavior.
|
||||||
|
def polys_iou(poly1, poly2):
|
||||||
|
a = np.array(poly1, dtype=np.float32).reshape(-1, 2)
|
||||||
|
b = np.array(poly2, dtype=np.float32).reshape(-1, 2)
|
||||||
|
|
||||||
|
if len(a) < 3 or len(b) < 3:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
# Fast reject for disjoint axis-aligned bounds.
|
||||||
|
a_min_x = float(np.min(a[:, 0]))
|
||||||
|
a_max_x = float(np.max(a[:, 0]))
|
||||||
|
a_min_y = float(np.min(a[:, 1]))
|
||||||
|
a_max_y = float(np.max(a[:, 1]))
|
||||||
|
b_min_x = float(np.min(b[:, 0]))
|
||||||
|
b_max_x = float(np.max(b[:, 0]))
|
||||||
|
b_min_y = float(np.min(b[:, 1]))
|
||||||
|
b_max_y = float(np.max(b[:, 1]))
|
||||||
|
if a_max_x < b_min_x or b_max_x < a_min_x or a_max_y < b_min_y or b_max_y < a_min_y:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
area_a = cv2.contourArea(a)
|
||||||
|
area_b = cv2.contourArea(b)
|
||||||
|
if area_a <= 0 or area_b <= 0:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
try:
|
||||||
|
inter_area, _ = cv2.intersectConvexConvex(a, b)
|
||||||
|
except cv2.error:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
if inter_area <= 0:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
union_area = area_a + area_b - inter_area
|
||||||
|
if union_area <= 0:
|
||||||
|
return 0.0
|
||||||
|
return float(inter_area / union_area)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue