[WIP] Refactoring meta-detection
This commit is contained in:
parent
99af2943b5
commit
17c40c996a
@ -58,55 +58,51 @@ def is_related(alpha: Rectangle, beta: Rectangle):
|
||||
return (is_near_enough(alpha, beta) and has_correct_position(alpha, beta)) or overlap(alpha, beta)
|
||||
|
||||
|
||||
def fuse_rects(rect1, rect2):
|
||||
if rect1 == rect2:
|
||||
return rect1
|
||||
x1, y1, w1, h1 = rect1
|
||||
x2, y2, w2, h2 = rect2
|
||||
|
||||
topleft = list(min(product([x1, x2], [y1, y2])))
|
||||
bottomright = list(max(product([x1 + w1, x2 + w2], [y1 + h1, y2 + h2])))
|
||||
|
||||
w = [bottomright[0] - topleft[0]]
|
||||
h = [bottomright[1] - topleft[1]]
|
||||
return tuple(topleft + w + h)
|
||||
def bounding_rect(alpha: Rectangle, beta: Rectangle):
|
||||
return Rectangle(
|
||||
min(alpha.x1, beta.x1),
|
||||
min(alpha.y1, beta.y1),
|
||||
max(alpha.x2, beta.x2),
|
||||
max(alpha.y2, beta.y2),
|
||||
)
|
||||
|
||||
|
||||
def rectangles_differ(r):
|
||||
return r[0] != r[1]
|
||||
|
||||
|
||||
def find_related_rects(rects):
|
||||
rect_pairs = lfilter(star(is_related), combinations(rects, 2))
|
||||
rect_pairs = lfilter(rectangles_differ, rect_pairs)
|
||||
if not rect_pairs:
|
||||
return [], rects
|
||||
rel_rects = set([rect for pair in rect_pairs for rect in pair])
|
||||
unrel_rects = [rect for rect in rects if rect not in rel_rects]
|
||||
return rect_pairs, unrel_rects
|
||||
# def find_related_rects(rects):
|
||||
# rect_pairs = lfilter(star(is_related), combinations(rects, 2))
|
||||
# rect_pairs = lfilter(rectangles_differ, rect_pairs)
|
||||
# if not rect_pairs:
|
||||
# return [], rects
|
||||
# rel_rects = set([rect for pair in rect_pairs for rect in pair])
|
||||
# unrel_rects = [rect for rect in rects if rect not in rel_rects]
|
||||
# return rect_pairs, unrel_rects
|
||||
|
||||
|
||||
def connect_related_rectangles(rectangles: Iterable[Rectangle]):
|
||||
boxes = lmap(rectangle_to_box, rectangles)
|
||||
rectangles = list(rectangles)
|
||||
|
||||
current_idx = 0
|
||||
|
||||
while True:
|
||||
if current_idx + 1 >= len(boxes) or len(boxes) <= 1:
|
||||
if current_idx + 1 >= len(rectangles) or len(rectangles) <= 1:
|
||||
break
|
||||
merge_happened = False
|
||||
current_rect = boxes.pop(current_idx)
|
||||
current_rect = rectangles.pop(current_idx)
|
||||
|
||||
for idx, maybe_related_rect in enumerate(boxes):
|
||||
for idx, maybe_related_rect in enumerate(rectangles):
|
||||
if is_related(current_rect, maybe_related_rect):
|
||||
current_rect = fuse_rects(current_rect, maybe_related_rect)
|
||||
boxes.pop(idx)
|
||||
current_rect = bounding_rect(current_rect, maybe_related_rect)
|
||||
|
||||
rectangles.pop(idx)
|
||||
merge_happened = True
|
||||
break
|
||||
boxes.insert(0, current_rect)
|
||||
rectangles.insert(0, current_rect)
|
||||
if not merge_happened:
|
||||
current_idx += 1
|
||||
elif merge_happened:
|
||||
current_idx = 0
|
||||
|
||||
return lmap(box_to_rectangle, boxes)
|
||||
return rectangles
|
||||
|
||||
@ -182,9 +182,4 @@ def intersection_along_axis(alpha, beta, axis):
|
||||
|
||||
|
||||
def overlap(alpha: Rectangle, beta: Rectangle):
|
||||
a_x1, a_y1, a_w1, a_h1 = alpha
|
||||
b_x2, b_y2, b_w2, b_h2 = beta
|
||||
|
||||
dx = min(a_x1 + a_w1, b_x2 + b_w2) - max(a_x1, b_x2)
|
||||
dy = min(a_y1 + a_h1, b_y2 + b_h2) - max(a_y1, b_y2)
|
||||
return True if (dx >= 0) and (dy >= 0) else False
|
||||
return intersection(alpha, beta) > 0
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user