def select_note_heads_whole(image_orig, image_bin, groups,dicts,regions):

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

    regions_dict = {}
    for contour in contours:
        rect = cv2.minAreaRect(contour)
        box = cv2.boxPoints(rect)
        box = np.int0(box)

        (x,y),radius = cv2.minEnclosingCircle(contour)
        center = (int(x),int(y))
        radius = int(radius)

        if radius==9:
            print 'rad ',radius
            cv2.circle(image_orig,center,radius,(0,255,0),2)

            # cv2.drawContours(image_orig,[box],0,(0,0,255),2)
            (x,y),(w,h),angle = rect
            #if angle < -40 and angle > -50 and w > 6 and w<8 and h > 5:
            # if w<8 and w > 4  and h> 9 and angle > -1:
            #     print '////////////////'
            #     print w,h,angle
            x,y,w,h = cv2.boundingRect(contour)
            region = image_bin[y:y+h+1,x:x+w+1]
            regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
            # cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)
            row = find_row(groups,y)
            dicts[row][x] = [img_fun.resize_region(region),(x,y,w,h)]
            r = reg.Region(x,y,w,h)
            regions.add_region(r)

    return image_orig,dicts,regions
def select_note_heads_quarter(image_orig, image_bin, groups, dicts,regions):
    img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    regions_dict = {}
    for contour in contours:

        x,y,w,h = cv2.boundingRect(contour)
        if w >8 and w<15 and h>7 and h<12:
            region = image_bin[y:y+h+1,x:x+w+1];
            regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
            cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)
            row = find_row(groups,y)
            dicts[row][x] = [img_fun.resize_region(region),(x,y,w,h)]
            r = reg.Region(x,y,w,h)
            regions.add_region(r)

    return image_orig,dicts,regions
def select_note_heads(image_orig, image_bin):

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

    regions_dict = {}
    for contour in contours:
        x,y,w,h = cv2.boundingRect(contour)

        #if w>10 and w<20:
        if w>16 and w < 23 and h<19:
            print str(w) + ',' + str(h)
            region = image_bin[y:y+h+1,x:x+w+1];
            regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
            cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)

    sorted_regions_dict = collections.OrderedDict(sorted(regions_dict.items()))
    sorted_regions = np.array(sorted_regions_dict.values())



    region_positions = []

    for x, y, w, h in sorted_regions[0:, 1]:
        region_positions.append((x,y))
    return image_orig, sorted_regions[:, 0], region_positions
def select_note_heads_half(image_orig, image_bin, groups, dicts,regions):

    img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)



    regions_dict = {}
    for contour in contours:
        rect = cv2.minAreaRect(contour)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        (x,y),(w,h),angle = rect
        if angle < -20 and angle>-50:
            x,y,w,h = cv2.boundingRect(contour)
            region = image_bin[y:y+h+1,x:x+w+1];
            regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
            cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)
            row = find_row(groups,y)
            dicts[row][x] = [img_fun.resize_region(region),(x,y,w,h)]
            r = reg.Region(x,y,w,h)
            regions.add_region(r)
    return image_orig,dicts,regions
def select_note_heads(image_orig, image_bin, groups):

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

    dicts = [dict() for x in range(len(groups))]


    regions_dict = {}
    for contour in contours:
        x,y,w,h = cv2.boundingRect(contour)

        #if w>10 and w<20:
        #if w>12 and w < 18 and h<23 and h>18:

        region = image_bin[y:y+h+1,x:x+w+1];
        row = find_row(groups,y)

        #print str(x) + "," + str(y) + ", row: " + str(row)


        dicts[row][x] = [img_fun.resize_region(region),(x,y,w,h)]

        regions_dict[x] = [img_fun.resize_region(region), (x,y,w,h)]
        cv2.rectangle(image_orig,(x,y),(x+w,y+h),(0,255,0),2)

    #sorted_regions_all_lines = get_sorted_regions(dicts)



    #sorted_regions_dict = collections.OrderedDict(sorted(regions_dict.items()))
    #sorted_regions = np.array(sorted_regions_dict.values())

    sorted_regions = get_sorted_regions(dicts)

    region_positions = []

    for x, y, w, h in sorted_regions[0:, 1]:
        region_positions.append(y)
    return image_orig, sorted_regions[:, 0], region_positions
def select_horizontal_lines(image_orig, image_bin):

    img, contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    region_positions = []
    regions_dict = {}
    for contour in contours:
        x,y,w,h = cv2.boundingRect(contour)
        region_positions.append(y)
        region = image_bin[y:y+h+1,x:x+w+1]
        regions_dict[x] = [img_fun.resize_region(region), (y, x, w, h)]
        cv2.rectangle(image_orig, (x, y), (x+w, y+h), (0, 255, 0), 2)

    sorted_regions_dict = collections.OrderedDict(sorted(regions_dict.items()))
    sorted_regions = np.array(sorted_regions_dict.values())

    return image_orig, sorted_regions[:, 0], region_positions