def combine_boxes(boxes, img_dim, dist_threshold=15, padding=0): """Uses UnionFind to group close-by contours into boxes (disjoint connected components). Parameters: boxes (list(numpy array)): List of numpy arrays of coordinates of a box as returned by enclosing_box. img_dim (tuple(int)): Tuple of image dimensions. dis_threshold (int): Threshold of number of pixels to determine whether boxes are too close. padding (int): Number of pixels that are padding. Return: (numpy array): Numpy array of combined boxes. """ n = len(boxes) uf = UnionFind(n) for i, j in combinations(range(n), 2): if closest_distance(boxes[i], boxes[j]) < dist_threshold: uf.union(i, j) box_groups = [[box for i in group for box in boxes[i]] for group in uf.groups()] combined_boxes = [ enclosing_box(group, img_dim, padding=padding) for group in box_groups ] filtered_boxes = [x for x in combined_boxes if fits_criteria_box(x)] return np.array(filtered_boxes)
def accounts_merge_using_union_find( accounts: List[List[str]]) -> List[List[str]]: uf = UnionFind() email_to_name = dict() for account in accounts: name = account[0] for email in account[1:]: email_to_name[email] = name uf.union(email, account[1]) return [[email_to_name[pmail]] + sorted(emails) for (pmail, emails) in uf.groups().items()]
def combine_boxes(boxes, img_dim, dist_threshold=15, padding=0): ''' Uses UnionFind to group close-by contours into boxes (disjoint connected components), for example, to combine letters into a text box ''' n = len(boxes) uf = UnionFind(n) for i, j in combinations(range(n), 2): if closest_distance(boxes[i], boxes[j]) < dist_threshold: uf.union(i, j) box_groups = [[box for i in group for box in boxes[i]] for group in uf.groups()] combined_boxes = [ enclosing_box(group, img_dim, padding=padding) for group in box_groups ] filtered_boxes = [x for x in combined_boxes if fits_criteria_box(x)] return np.array(filtered_boxes)