euidong / SheetMusic

SheetMusic to RealMusic
4 stars 3 forks source link

8분따리(작성중) #22

Open AnYoungMin opened 5 years ago

AnYoungMin commented 5 years ago

----------제한사항--------------------------

  1. 의동이의 악보오선지 나누기와 quarter matching이 잘 되었다고 가정하고 작성된 코드라 앞에서 꼬이면 얜 그냥 막장됨
  2. 의동이의 오선자르기를 넘어가는 음 line[0]-gap*4를 넘어가는 실험결과 추가 세줄 넘어간 미??부터 처리가 안되서 걍 4분따리로 간주됨

----------전역부분 추가할 내용(threshold는 반드시0.7)--------------------------------------------

right_down_tuplet_files = [
    "resources/template/tuplet_rd.png"]
right_up_tuplet_files = [
    "resources/template/tuplet_ru.png"]

right_down_tuplet_imgs = [cv2.imread(right_down_tuplet_file, 0) for right_down_tuplet_file in right_down_tuplet_files]
right_up_tuplet_imgs = [cv2.imread(right_up_tuplet_file, 0) for right_up_tuplet_file in right_up_tuplet_files]

right_down_tuplet_lower, right_down_tuplet_upper, right_down_tuplet_thresh = 50, 150, 0.7
right_up_tuplet_lower, right_up_tuplet_upper, right_up_tuplet_thresh = 50, 150, 0.7

+프로젝트의 resources/templates 폴더내에 다음 두 파일 추가 tuplet_rd tuplet_ru

-----------------------전역함수 (이즈옥타)-------------------------------------------------

def isOcta(img, rec): #꼭 img로 deletemeasure된 이미지 넘겨줄것
    area=rec.w*rec.h
    cnt=0
    for i in range(int(rec.x),int(rec.x+rec.w)):
        for j in range(int(rec.y),int(rec.y+rec.h)):
            if img[j,i]==0:
                cnt+=1
    return cnt/area>=0.16

--------------------메인부분 앞쪽에----------------------------------------------------------------- CutMeasures를 호출해서 오선지 rec받는부분에

오선지저장리스트, gaplist = CutMeasures(img) 처럼 gaplist라는 각 오선별 gap을 저장해줄 list추가 선언 필요 cutmeasure는

------------------matching merging quarter 바로밑에 추가할 부분 끝까지 전부다 복붙---------------------

print("Matching right_down_tuplet image...")
    right_down_tuplet_recs = locate_images(img_gray, right_down_tuplet_imgs, right_down_tuplet_lower, right_down_tuplet_upper, right_down_tuplet_thresh)

    print("Matching right_up_tuplet image...")
    right_up_tuplet_recs = locate_images(img_gray, right_up_tuplet_imgs, right_up_tuplet_lower, right_up_tuplet_upper, right_up_tuplet_thresh)

    print("Merging tuplet image results...")
    tuplet_recs=right_down_tuplet_recs+right_up_tuplet_recs

    tuplet_recs = merge_recs([j for i in tuplet_recs for j in i], 0.5)
#이하는 tuplet matchtemplate결과 4분음표를 잡는 경우가 있어 이를 제거해주는 과정
    for staff in '이문자열을지우고cutmeasure해준오선리스트배열이름넣을것':
        staff_tuplet_recs= [r for r in tuplet_recs if staff.y<=r.y and staff.y+staff.h>=r.y]
        staff_quarter_recs = [r for r in quarter_recs if staff.y<=r.y and staff.y+staff.h>=r.y]
        staff_tuplet_recs.sort(key=lambda r: r.x)
        staff_quarter_recs.sort(key=lambda r: r.x)
        for quarter in staff_quarter_recs:
            for tuplet in staff_tuplet_recs:
                if quarter.overlap(tuplet)>0:
                    tuplet_recs.remove(tuplet)
                elif quarter.x+quarter.w<=tuplet.x:
                    break
    tuplet_recs_img = img.copy()
    for r in tuplet_recs:
        r.draw(tuplet_recs_img, (0, 0, 255), 2)
    cv2.imwrite("tuplet.png",tuplet_recs_img)
    open_file('tuplet.png')

#이하는 같은 오선 내에서 tuplet과 동일한 x좌표에 있는 quarter대가리를 octa로 분류시켜주는 작업
    octa_recs=[]
    for staff in '이문자열을지우고cutmeasure해준오선리스트배열이름넣을것':

        staff_tuplet_recs= [r for r in tuplet_recs if staff.y<=r.y and staff.y+staff.h>=r.y]
        staff_quarter_recs = [r for r in quarter_recs if staff.y<=r.y and staff.y+staff.h>=r.y]
        staff_tuplet_recs.sort(key=lambda r: r.x)
        staff_quarter_recs.sort(key=lambda r: r.x)
        check_box=[]
        for _ in range(len(staff_quarter_recs)):
            check_box.append(False)

        for tuplet in staff_tuplet_recs:

            for i in range(len(staff_quarter_recs)):

                if (staff_quarter_recs[i].x<=tuplet.x+tuplet.w+staff_quarter_recs[i].w/2) and (staff_quarter_recs[i].x>=tuplet.x-staff_quarter_recs[i].w):
                    check_box[i]=True
        for i in range(len(staff_quarter_recs)):
            if check_box[i]:
                quarter_recs.remove(staff_quarter_recs[i])
                octa_recs.append(staff_quarter_recs[i])

#이하는 이어진 8분음표가 아닌 홀로 떨어져있는 진짜 8분음표처럼 생긴애를 quarter중에 분류하는 작업
    for i in range(len('이문자열을지우고cutmeasure해준오선리스트배열이름넣을것')):
        #cnt+=1
        #prtline="Staff{} ".format(str(cnt))
        middle_x, staff_middle_y='이문자열을지우고cutmeasure해준오선리스트배열이름넣을것'[i].middle
        #copy_copy_img=copy_img.copy()
        staff_quarter_recs = [r for r in quarter_recs if '이문자열을지우고cutmeasure해준오선리스트배열이름넣을것'[i].y<=r.y and '이문자열을지우고cutmeasure해준오선리스트배열이름넣을것'.y+'이문자열을지우고cutmeasure해준오선리스트배열이름넣을것'.h>=r.y]
        staff_quarter_recs.sort(key=lambda r: r.x)
        #tempcnt=0
        for quarter in staff_quarter_recs:
            #tempcnt+=1
            if quarter.y>staff_middle_y-gaplist[i]/2:#가운데아래 음표
                temp=Rectangle(quarter.x+quarter.w,quarter.y-gaplist[i]*3+int(quarter.h*0.3),int(quarter.w/2),quarter.h)
                if isOcta('!!!!여기에 반드시오선지운이미지넣을것!!!',temp):
                    quarter_recs.remove(quarter)
                    octa_recs.append(quarter)
            else:
                if quarter.y>staff_middle_y-gaplist[i]*0.8:#가운데줄 음표
                    temp=Rectangle(quarter.x+int(quarter.w*0.2),quarter.y+gaplist[i]*3-int(quarter.h*0.3),int(quarter.w/2),quarter.h)
                    if isOcta('!!!!여기에 반드시오선지운이미지넣을것!!!',temp):
                        quarter_recs.remove(quarter)
                        octa_recs.append(quarter)
                else:#가운데 위 음표
                    temp=Rectangle(quarter.x+int(quarter.w*0.2),quarter.y+gaplist[i]*3-int(quarter.h*0.3),int(quarter.w/2),quarter.h)
                    if isOcta('!!!!여기에 반드시오선지운이미지넣을것!!!',temp):
                        quarter_recs.remove(quarter)
                        octa_recs.append(quarter)