コード例 #1
0
def find_lines(image, projection=True, horizontal_opening=True):
    """Recognize staff lines on image and return them
    as a list of lists of lists of row numbers,
    distances between them and average staff spacing.
    :param image:
    :param projection:
    :param horizontal_opening:
    """

    first = None
    second = None
    if projection:
        first = get_white_rows(imo.crop_image(
            imo.project_image(image)))
    if horizontal_opening:
        if projection:
            line_distances = group_rows(first)[1]
            avg_staff_spacing = []
            for staff_line_distances in line_distances:
                avg_staff_spacing += staff_line_distances
            avg_staff_spacing = sum(avg_staff_spacing) * 1. / len(avg_staff_spacing)
            second = get_white_rows(imo.open_image(image, np.ones((1, 2 * avg_staff_spacing))))
        else:
            second = get_white_rows(imo.open_image(image, np.ones((1, 20))))

    if projection and horizontal_opening:
        image_lines = intersect_lists(first, second)
    elif projection:
        image_lines = first
    elif horizontal_opening:
        image_lines = second
    else:
        raise Exception("To find lines either horizontal projection"
                        "of the image must be done or a horizontal"
                        "morphological opening")

    lines, line_distances, staff_distances = \
        group_rows(image_lines)
    avg_staff_spacing = []
    for staff_line_distances in line_distances:
        avg_staff_spacing += staff_line_distances
    avg_staff_spacing = sum(avg_staff_spacing) * 1. / len(avg_staff_spacing)
    if len(staff_distances) > 0:
        avg_staff_distance = sum(staff_distances) * 1. / len(staff_distances)
    else:
        avg_staff_distance = 0
    return lines, line_distances, avg_staff_spacing, staff_distances, avg_staff_distance
コード例 #2
0
def open_image(image, kernel=None):
    return imo.open_image(image, kernel)