def _filter_coordinates(coords, mask, width, max_background):
    """Discard coordinates with too much background."""
    filtered = []
    for x, y in coords:
        tile_mask = mask[y:y+width, x:x+width]
        if tile_mask.size == 0:
            continue
        bg_perc = 1 - tile_mask.sum()/tile_mask.size
        if bg_perc < max_background:
            filtered.append(((x, y), bg_perc))
    return filtered