def prepare_sharps(image):

    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)
    image_bin = img_fun.invert(image_bin)

    image_bin = cv2.erode(image_bin,np.ones((1,3)),iterations=1)
def prepare_bar_lines(image):

    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)
    image_bin = img_fun.invert(image_bin)



    # cv2.imshow('bar lines',image_bin)
    return image_bin
def prepare_half_notes(image):
    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)
    image_bin = cv2.morphologyEx(image_bin, cv2.MORPH_OPEN, np.ones((4,4)))
    image_bin = cv2.erode(image_bin,np.ones((2,2)),iterations=1)
    image_bin = img_fun.invert(image_bin)



    return  image_bin
def get_note_positions(image,groups):



    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)
    image_bin = img_fun.invert(image_bin)

    image_bin = cv2.dilate(image_bin, np.ones((6,2)), iterations=1)
    verticalSize = 4;
    verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (verticalSize,1));

    image_bin = cv2.erode(image_bin, verticalStructure, iterations=1)
    image_bin = cv2.erode(image_bin, np.ones((7,1)), iterations=1)
    #image_bin = cv2.dilate(image_bin, np.ones((6,2)), iterations=1)
    #image_bin = img_fun.erode(image_bin)



    #image_bin = disconnect_note_heads(image.copy(), image_bin)

    # image_bin = cv2.erode(image_bin, verticalStructure, iterations=1)
    # image_bin = cv2.erode(image_bin, verticalStructure, iterations=3)

    # image_bin = disconnect_note_heads(image.copy(), image_bin)
    #
    # image_bin = cv2.erode(image_bin, np.ones((2,2)), iterations=1)
    # image_bin = img_fun.dilate(image_bin)



    # kernel = np.ones((3,3)) # strukturni element 3x3 blok
    #
    # image_bin = img_fun.dilate(image_bin)
    # #image_bin = img_fun.dilate(image_bin)
    #
    # kernel_vet = np.ones((6,1))
    # kernel_hor = np.ones((1,5))
    # image_bin = cv2.erode(image_bin, kernel_hor, iterations=1)
    # image_bin = cv2.dilate(image_bin, kernel_vet, iterations=1)
    #
    # image_bin = cv2.dilate(image_bin, kernel, iterations=1)
    # #image_bin = cv2.erode(image_bin, kernel, iterations=1)

    cv2.imshow('preparet image', image_bin)

    image_orig,selected_regions, positions = select_note_heads(image.copy(), image_bin, groups)



    cv2.imshow('asd', image_orig)

    return positions
def prepare_whole_notes(image):
    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)
    # image_bin = cv2.erode(image_bin,np.ones((1,2)),iterations=3)
    #
    # image_bin = cv2.dilate(image_bin,np.ones((2,4)),iterations=2)

    image_bin = img_fun.invert(image_bin)

    # img_fun.show_image('whole_gray',image_gray)
    # img_fun.show_image('whole_bin',image_bin)

    return image_bin
def prepare_quarter_notes(image):
    image_gray = img_fun.image_gray(image)
    image_bin = img_fun.image_bin(image_gray)


    # img_fun.show_image(image_bin)
    # img_fun.show_image(image_gray)

    image_bin = img_fun.invert(image_bin)
    verticalSize = 3;
    horSize = 3
    verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (verticalSize,1));
    horStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,horSize));
    image_bin = cv2.erode(image_bin, verticalStructure, iterations=1)
    image_bin = cv2.morphologyEx(image_bin, cv2.MORPH_OPEN, np.ones((5,5)))
    image_bin = disconnect_note_heads(image.copy(),image_bin)

    # cv2.imshow('quarter bin',image_bin)

    return image_bin
import imageProcessingFunctions as img_fun
import cv2
import numpy as np
import collections

image = img_fun.load_image('images/sheet4.png')
image_gray = img_fun.image_gray(image)
image_bin = img_fun.image_bin(image_gray)
image_bin = img_fun.invert(image_bin)


verticalSize = 3;
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (verticalSize,1));

image_bin = cv2.erode(image_bin, verticalStructure, iterations=1)

kernel = np.ones((3,3)) # strukturni element 3x3 blok

image_bin = img_fun.dilate(image_bin)
image_bin = img_fun.dilate(image_bin)
image_bin = cv2.dilate(image_bin, kernel, iterations=1)


cv2.imshow('binary', image_bin)
cv2.waitKey(0)



def select_note_heads(image_orig, image_bin):

    img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)