def __init__(self, rec, sym, staff_rec, sharp_notes = [], flat_notes = [],img = [],staff_img=[],best_scale=1): self.rec = rec self.sym = sym self.xpadd = 20 self.ypadd = 50 staff_lower, staff_upper, staff_thresh = int(best_scale*100)-20, int(best_scale*100)+20, 0.60 after = img[ int(staff_rec.y):int(staff_rec.y + staff_rec.h), int(rec.x + rec.w):int(rec.x + 2 * rec.w)] after = cv2.copyMakeBorder(after,self.ypadd,self.ypadd,self.xpadd,self.xpadd,cv2.BORDER_CONSTANT,value=255) #zero padding locations, scale = fit(after, staff_img, staff_lower, staff_upper, staff_thresh) locs = [] for i in range(len(staff_img)): locs.append([pt for pt in zip(*locations[i][::-1])]) locs = [j for i in locs for j in i] before = img[int(staff_rec.y):int(staff_rec.y + staff_rec.h), int(rec.x - rec.w):int(rec.x)] before = cv2.copyMakeBorder(before,self.ypadd,self.ypadd,self.xpadd,self.xpadd, cv2.BORDER_CONSTANT, value=255) # zero padding locations2, scale2 = fit(before, staff_img, staff_lower, staff_upper, staff_thresh) locs2 = [] for i in range(len(staff_img)): locs2.append([pt for pt in zip(*locations2[i][::-1])]) locs2 = [j for i in locs2 for j in i] if(len(locs)<4): middle = rec.y + (rec.h / 2.0) height = (middle - staff_rec.y) / staff_rec.h note_def = note_defs[int(height / note_step + 0.5)] print(note_def) print("is not accurate") else: heights = [p[1] for p in locs] heights2 = [q[1] for q in locs2] avgheight = sum(heights)/len(heights) print(avgheight+ staff_rec.y - self.ypadd) avgheight2 = sum(heights2)/len(heights2) print(avgheight2+ staff_rec.y - self.ypadd) avgheight = int((avgheight+avgheight2)/2 + 0.5) avgheight = avgheight + staff_rec.y - self.ypadd middle = rec.y + (rec.h / 2.0) height = (middle - (avgheight + 38*best_scale)) / (9*best_scale) #need to improve note_def = note_defs[int(height + 0.5) - 3] print("Middle of note:",(rec.x + rec.w/2),middle) print("Average Height of Staff:",avgheight) print("Distance Between Middle Average Height:",middle - avgheight) print("Note Steps From First Line:",height) print("Note Found:",note_def[0]) print("Best Scale",best_scale) print("--------------") self.note = note_def[0] self.pitch = note_def[1] if any(n for n in sharp_notes if n.note[0] == self.note[0]): self.note += "#" self.pitch += 1 if any(n for n in flat_notes if n.note[0] == self.note[0]): self.note += "b" self.pitch -= 1
def locate_rectangles(img, templates, start, stop, threshold): locations = fit(img, templates, start, stop, threshold) img_locations = [] for i in range(len(templates)): w, h = templates[i].shape[::-1] for pt in zip(*locations[i][::-1]): img_locations.append(Rectangle(pt[0], pt[1], w, h)) return img_locations
def locate_images(img, templates, start, stop, threshold): locations, scale = fit(img, templates, start, stop, threshold) img_locations = [] for i in range(len(templates)): w, h = templates[i].shape[::-1] w *= scale h *= scale img_locations.append([Rectangle(pt[0], pt[1], w, h) for pt in zip(*locations[i][::-1])]) return img_locations
def locate_images(img, templates, start, stop, threshold): locations, scale = fit(img, templates, start, stop, threshold)#return best_location, best_scale #호출 결과 리스트에 template타입 별 sheet내의 비슷한 좌표가 location으로 return #scale : template의 확대비 img_locations = [] for i in range(len(templates)): w, h = templates[i].shape[::-1] w *= scale h *= scale img_locations.append([Rectangle(pt[0], pt[1], w, h) for pt in zip(*locations[i][::-1])]) return img_locations
def locate_images(img, templates, start, stop, threshold): #最もよくマッチングした際の、テンプレート画像の拡大縮小率(scale)と、 #テンプレート画像がマッチングした場所(locations)を算出 locations, scale = fit(img, templates, start, stop, threshold) img_locations = [] for i in range(len(templates)): w, h = templates[i].shape[::-1] #テンプレート画像を拡大縮小 w *= scale h *= scale #マッチングした場所をRectangleクラスのインスタンスにする img_locations.append( [Rectangle(pt[0], pt[1], w, h) for pt in zip(*locations[i][::-1])]) return img_locations
def locate_images_elem(img, templates, start, stop, threshold, boxes): img_locations = [] ypadd = 50 for box in boxes: one_box_img = img[int(box.y):int(box.y + box.h), :] one_box_img = cv2.copyMakeBorder(one_box_img, ypadd, ypadd, 0, 0, cv2.BORDER_CONSTANT, value=255) # zero padding locations, scale = fit(one_box_img, templates, start, stop, threshold) for i in range(len(templates)): w, h = templates[i].shape[::-1] w *= scale h *= scale img_locations.append([ Rectangle(box.x + pt[0], box.y + pt[1] - ypadd, w, h) for pt in zip(*locations[i][::-1]) ]) return img_locations