def SegmentImage(image, filledImage, contours):
    #Create a list for all the segments found in the image
    segments = list()
    #Create a collective image that has all segments
    imagecollective = np.zeros((image.shape[0], image.shape[1]))
    imagecollective[:, :] = 0.5
    #Segment every contour
    for contour in contours:
        #Create new segment
        segment = Segment()
        #Get aspect ratio
        segment.BoundingBox = BoundingBox(np.min(contour[:, 1]),
                                          np.max(contour[:, 1]),
                                          np.min(contour[:, 0]),
                                          np.max(contour[:, 0]))
        segment.BoundingBox.ToInt()
        #Create the segment image (will be replaced with segment class)
        segment.CreateImage()
        #Copy the segment from the contour we have to the segment
        rr, cc = polygon(contour[:, 0], contour[:, 1], image.shape)
        rr = rr.astype(int)
        cc = cc.astype(int)
        segment.Image[rr - segment.BoundingBox.miny,
                      cc - segment.BoundingBox.minx] = image[rr, cc]
        segment.FilledImage[rr - segment.BoundingBox.miny,
                            cc - segment.BoundingBox.minx] = filledImage[rr,
                                                                         cc]
        imagecollective[rr, cc] = image[rr, cc]
        segments.append(segment)
    return imagecollective, segments
def MergeHalfCircles(circles):
    newCirclesList = list()
    merged = np.zeros(len(circles))
    for i in range(len(circles)):
        if (merged[i] == 1):
            continue
        c1 = circles[i]
        c2 = None
        for j in range(len(circles)):
            if (i == j or merged[j]):
                continue
            if (abs(c1.BoundingBox.miny - circles[j].BoundingBox.miny) <
                    circlesMergeThreshold
                    and abs(c1.BoundingBox.maxy - circles[j].BoundingBox.maxy)
                    < circlesMergeThreshold and
                (abs(c1.BoundingBox.minx - circles[j].BoundingBox.maxx) <
                 circlesMergeThreshold * 10
                 or abs(c1.BoundingBox.maxx - circles[j].BoundingBox.minx) <
                 circlesMergeThreshold * 10)):
                c2 = circles[j]
                merged[j] = 1
                break
        if (c2 != None and c1.BoundingBox.minx > c2.BoundingBox.minx):
            c1, c2 = c2, c1
        width = c1.Image.shape[1]
        height = c1.Image.shape[0]
        if (c2 != None):
            width = max(width, c2.Image.shape[1])
            height = max(height, c2.Image.shape[0])
        #Resize img1
        tmp = np.copy(c1.Image)
        c1.Image = np.zeros((height, width))
        c1.Image[:tmp.shape[0], :tmp.shape[1]] = tmp[:, :]
        if (c2 != None):
            #Resize img2
            tmp = np.copy(c2.Image)
            c2.Image = np.zeros((height, width))
            c2.Image[:tmp.shape[0], :tmp.shape[1]] = tmp[:, :]
            newCircle = np.concatenate((c1.Image, c2.Image), axis=1)
            segment = Segment()
            segment.BoundingBox = c1.BoundingBox
            segment.Image = newCircle
            newCirclesList.append(segment)
            merged[i] = 1
        else:
            newCirclesList.append(c1)
    connectCircles(newCirclesList)
    for circle in newCirclesList:
        contours = find_contours(circle.Image, 0.9)
        circle.FilledImage = FillPolygon(circle.Image, contours)
    return newCirclesList
def DetectNoteheads(notesAndConnectedSegments):
    newList = list()
    newSegmentsList = list()
    for seg in notesAndConnectedSegments:
        image = np.copy(seg.Image)
        tmpImg = np.copy(image[1:-2, 1:-2])
        ones = np.sum(image[5:-5, 5:-5] == 1)
        zeros = np.sum(image[5:-5, 5:-5] == 0)

        if (ones > 0 and zeros / ones > 1.2):
            image = ndimage.binary_fill_holes(image).astype(int)
            image = binary_erosion(image, selement)
            image = binary_erosion(image, selement)
            image = binary_erosion(image, selement2)

            newSeg = Segment(seg)
            newSeg.Image = image
            newSeg.BoundingBox = seg.BoundingBox
            newList.append(image)
            newSegmentsList.append(newSeg)
    return newSegmentsList, newList