Merge in RR/vidocp from refactoring to master
Squashed commit of the following:
commit 36a62a13e51148d2420cb12930e84d78629db6b0
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:54:53 2022 +0100
refactoring
commit e652da1fa88a048f9a5211b4e8c0b96074fb5849
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:53:17 2022 +0100
refactoring
commit d9567da428c81f9cd7971a657281df0a90166810
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:47:18 2022 +0100
refactoring
commit 9d30009dceec0357db6499bfaffae8ce97718ee0
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:45:53 2022 +0100
refactoring
commit e8863d67aaaff138fb088c4e496a91b6354cc059
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:42:45 2022 +0100
refactoring
commit 89a99d3586db4fbafa743a45bdd02eaf0c1f341f
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:39:49 2022 +0100
refactoring
commit aa66b6865b00b0490b9e7695a6bae386e6f96723
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:31:21 2022 +0100
refactoring
commit 98d77cb522a08821c3a13ae2cffbe7239c654762
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:27:55 2022 +0100
refactoring
commit fed3a7e4f1b8b7ca4e14f9e495459c26490fb50b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:26:16 2022 +0100
refactoring
commit 504cafbd5d4bba183d9943b36c60548aae34e402
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:25:44 2022 +0100
renaming
commit c9780a57e5a048529d36958ba678eddb11759cef
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:24:41 2022 +0100
removed obsolete import
commit d555e86475e82024f8e1a5fc5b0ac70faa091ee1
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Sun Feb 6 14:24:04 2022 +0100
refactored figure detection once
63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
from collections import namedtuple
|
|
from functools import partial
|
|
|
|
|
|
def remove_overlapping(rectangles):
|
|
def overlap(a, b):
|
|
return compute_intersection(a, b) > 0
|
|
|
|
def does_not_overlap(rect, rectangles):
|
|
return not any(overlap(rect, r2) for r2 in rectangles if not rect == r2)
|
|
|
|
rectangles = list(map(xywh_to_vec_rect, rectangles))
|
|
rectangles = filter(partial(does_not_overlap, rectangles=rectangles), rectangles)
|
|
rectangles = map(vec_rect_to_xywh, rectangles)
|
|
return rectangles
|
|
|
|
|
|
def remove_included(rectangles):
|
|
def included(a, b):
|
|
return b.xmin >= a.xmin and b.ymin >= a.ymin and b.xmax <= a.xmax and b.ymax <= a.ymax
|
|
|
|
def is_not_included(rect, rectangles):
|
|
return not any(included(r2, rect) for r2 in rectangles if not rect == r2)
|
|
|
|
rectangles = list(map(xywh_to_vec_rect, rectangles))
|
|
rectangles = filter(partial(is_not_included, rectangles=rectangles), rectangles)
|
|
rectangles = map(vec_rect_to_xywh, rectangles)
|
|
return rectangles
|
|
|
|
|
|
Rectangle = namedtuple("Rectangle", "xmin ymin xmax ymax")
|
|
|
|
|
|
def make_box(x1, y1, x2, y2):
|
|
keys = "x1", "y1", "x2", "y2"
|
|
return dict(zip(keys, [x1, y1, x2, y2]))
|
|
|
|
|
|
def compute_intersection(a, b):
|
|
|
|
dx = min(a.xmax, b.xmax) - max(a.xmin, b.xmin)
|
|
dy = min(a.ymax, b.ymax) - max(a.ymin, b.ymin)
|
|
|
|
return dx * dy if (dx >= 0) and (dy >= 0) else 0
|
|
|
|
|
|
def has_no_parent(hierarchy):
|
|
return hierarchy[-1] <= 0
|
|
|
|
|
|
def xywh_to_vec_rect(rect):
|
|
x1, y1, w, h = rect
|
|
x2 = x1 + w
|
|
y2 = y1 + h
|
|
return Rectangle(x1, y1, x2, y2)
|
|
|
|
|
|
def vec_rect_to_xywh(rect):
|
|
x, y, x2, y2 = rect
|
|
w = x2 - x
|
|
h = y2 - y
|
|
return x, y, w, h
|