def rm_img_in_compo(corners_img, corners_compo): """ Remove imgs in component """ corners_img_new = [] for img in corners_img: is_nested = False for compo in corners_compo: if util.corner_relation(img, compo) == -1: is_nested = True break if not is_nested: corners_img_new.append(img) return corners_img_new
def merge_corner(corners, compos_class, is_merge_nested): """ i. merge overlapped corners ii. remove nested corners :param corners: corners: [(top_left, bottom_right)] -> top_left: (column_min, row_min) -> bottom_right: (column_max, row_max) :return: new corners """ def merge_overlapped(corner_a, corner_b): (top_left_a, bottom_right_a) = corner_a (col_min_a, row_min_a) = top_left_a (col_max_a, row_max_a) = bottom_right_a (top_left_b, bottom_right_b) = corner_b (col_min_b, row_min_b) = top_left_b (col_max_b, row_max_b) = bottom_right_b col_min = min(col_min_a, col_min_b) col_max = max(col_max_a, col_max_b) row_min = min(row_min_a, row_min_b) row_max = max(row_max_a, row_max_b) return (col_min, row_min), (col_max, row_max) new_corners = [] new_class = [] for i in range(len(corners)): is_intersected = False for j in range(len(new_corners)): r = util.corner_relation(corners[i], new_corners[j]) if is_merge_nested: # if corners[i] is in new_corners[j], ignore corners[i] if r == -1: is_intersected = True break # if new_corners[j] is in corners[i], replace new_corners[j] with corners[i] elif r == 1: is_intersected = True new_corners[j] = corners[i] new_class[j] = compos_class[i] # if [i] and [j] are overlapped if r == 2: is_intersected = True if compos_class[i] == new_class[j]: new_corners[j] = merge_overlapped(corners[i], new_corners[j]) if not is_intersected: new_corners.append(corners[i]) new_class.append(compos_class[i]) return new_corners, new_class