def __call__(self,im_staffonly, ratio): from gamera.core import Image,RGBPixel,Point from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton im_full=self im_staffless=im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness]=find_stafflines_int(im_staffonly) # ratio=staffline_height/staffspace_height thickness=(stafflines[1]-stafflines[0])/(1/ratio+1) im_newlines=Image(im_full.ul,im_full.size) # we just draw the lines ourselves for y in stafflines: im_newlines.draw_line(Point(first_x,y),Point(last_x,y),RGBPixel(255,255,255),thickness) # new full: lines OR symbols def_full=im_staffless.or_image(im_newlines) # new staffonly: lines AND NOT symbols def_staffonly=im_staffless.image_copy() def_staffonly.invert() def_staffonly.and_image(im_newlines,True) # staffless image doesn't change def_staffless=im_staffless.image_copy() # construct skeletons staffline_skel=[] for y in stafflines: skel=StafflineSkeleton() skel.left_x=first_x # all stafflines are completely straight skel.y_list=(last_x-first_x+1)*[y] staffline_skel.append(skel) return [def_full,def_staffonly,staffline_skel]
def __doc_example1__(images): image = Image((0, 0), Dim(100, 100), RGB, DENSE) for i in range(10): image.draw_line((randint(0, 100), randint(0, 100)), (randint(0, 100), randint(0, 100)), RGBPixel(randint(0, 255), randint(0, 255), randint(0, 255)), float(randint(0, 4))) return image
def __doc_example1__(images): from random import randint from gamera.core import Image, Dim image = Image((0, 0), Dim(100, 100), RGB, DENSE) for i in range(10): image.draw_line((randint(0, 100), randint(0, 100)), (randint(0, 100), randint(0, 100)), RGBPixel(randint(0, 255), randint(0, 255), randint(0, 255)), float(randint(0, 4))) return image
def __doc_example1__(images): from random import randint from gamera.core import Image, Dim image = Image((0, 0), Dim(100, 100), RGB, DENSE) for i in range(10): image.draw_line((randint(0, 100), randint(0, 100)), (randint(0, 100), randint(0, 100)), RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)), float(randint(0, 4))) return image
def __call__(self, im_staffonly, min, max, c=0.5, random_seed=0): from gamera.core import Image, RGBPixel from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton seed(random_seed) im_full = self im_staffless = im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness] = find_stafflines_int(im_staffonly) def_staffonly = Image(im_full.ul, im_full.size) # state machine for the thickness states = states_mh(max - min + 1, c) # draw the deformed stafflines in a blank image last_thickness = 0 even_shift = 0 for l in range(len(stafflines)): y = stafflines[l] for x in range(first_x, last_x + 1): thickness = states.next() + min y_st = y - thickness / 2 if thickness % 2 == 0: if thickness != last_thickness: if random() < 0.5: even_shift = 1 else: even_shift = 0 y_st = y_st + even_shift if thickness > 0: def_staffonly.draw_line((x, y_st), (x, y_st + thickness - 1), RGBPixel(255, 255, 255)) last_thickness = thickness # stafflines = stafflines AND NOT symbols im_staffless_invert = im_staffless.image_copy() im_staffless_invert.invert() def_staffonly.and_image(im_staffless_invert, True) # full = symbols OR stafflines def_full = im_staffless.image_copy() def_full.or_image(def_staffonly, True) # construct skeletons (in fact they didn't change) staffline_skel = [] for y in stafflines: skel = StafflineSkeleton() skel.left_x = first_x # all stafflines are completely straight skel.y_list = (last_x - first_x + 1) * [y] staffline_skel.append(skel) return [def_full, def_staffonly, staffline_skel]
def __call__(self,im_staffonly,min,max,c=0.5,random_seed=0): from gamera.core import Image,RGBPixel from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton seed(random_seed) im_full=self im_staffless=im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness]=find_stafflines_int(im_staffonly) def_staffonly=Image(im_full.ul,im_full.size) # state machine for the thickness states=states_mh(max-min+1,c) # draw the deformed stafflines in a blank image last_thickness=0 even_shift=0 for l in range(len(stafflines)): y=stafflines[l] for x in range(first_x,last_x+1): thickness=states.next()+min y_st=y-thickness/2 if thickness%2==0: if thickness!=last_thickness: if random()<0.5: even_shift=1 else: even_shift=0 y_st=y_st+even_shift if thickness>0: def_staffonly.draw_line((x,y_st),(x,y_st+thickness-1),RGBPixel(255,255,255)) last_thickness=thickness # stafflines = stafflines AND NOT symbols im_staffless_invert=im_staffless.image_copy() im_staffless_invert.invert() def_staffonly.and_image(im_staffless_invert,True) # full = symbols OR stafflines def_full=im_staffless.image_copy() def_full.or_image(def_staffonly,True) # construct skeletons (in fact they didn't change) staffline_skel=[] for y in stafflines: skel=StafflineSkeleton() skel.left_x=first_x # all stafflines are completely straight skel.y_list=(last_x-first_x+1)*[y] staffline_skel.append(skel) return [def_full,def_staffonly,staffline_skel]
def __call__(self, im_staffonly, maxdiff, c=0.5, random_seed=0): from gamera.core import Image, RGBPixel from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton seed(random_seed) im_full = self im_staffless = im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness] = find_stafflines_int(im_staffonly) def_staffonly = Image(im_full.ul, im_full.size) states = states_mh(2 * maxdiff + 1, c) m = (thickness - 1) / 2 if (thickness and 1) == 1: p = m else: p = m + 1 staffline_skel = [] for l in range(len(stafflines)): y = stafflines[l] - maxdiff skel = StafflineSkeleton() skel.left_x = first_x skel.y_list = (last_x - first_x + 1) * [y] for x in range(first_x, last_x + 1): y_l = y + states.next() skel.y_list[x - first_x] = y_l def_staffonly.draw_line((x, y_l - m), (x, y_l + p), RGBPixel(255, 255, 255)) staffline_skel.append(skel) im_staffless_invert = im_staffless.image_copy() im_staffless_invert.invert() def_staffonly.and_image(im_staffless_invert, True) def_staffless = im_staffless.image_copy() def_full = im_staffless.image_copy() def_full.or_image(def_staffonly, True) return [def_full, def_staffonly, staffline_skel]
def __call__(self,im_staffonly,maxdiff,c=0.5,random_seed=0): from gamera.core import Image,RGBPixel from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton seed(random_seed) im_full=self im_staffless=im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness]=find_stafflines_int(im_staffonly) def_staffonly=Image(im_full.ul,im_full.size) states=states_mh(2*maxdiff+1,c) m=(thickness-1)/2 if (thickness and 1)==1: p=m else: p=m+1 staffline_skel=[] for l in range(len(stafflines)): y=stafflines[l]-maxdiff skel=StafflineSkeleton() skel.left_x=first_x skel.y_list=(last_x-first_x+1)*[y] for x in range(first_x,last_x+1): y_l=y+states.next() skel.y_list[x-first_x]=y_l def_staffonly.draw_line((x,y_l-m),(x,y_l+p),RGBPixel(255,255,255)) staffline_skel.append(skel) im_staffless_invert=im_staffless.image_copy() im_staffless_invert.invert() def_staffonly.and_image(im_staffless_invert,True) def_staffless=im_staffless.image_copy() def_full=im_staffless.image_copy() def_full.or_image(def_staffonly,True) return [def_full,def_staffonly,staffline_skel]
def __call__(self, im_staffonly, ratio): from gamera.core import Image, RGBPixel, Point from gamera.toolkits.musicstaves.stafffinder import StafflineSkeleton im_full = self im_staffless = im_full.xor_image(im_staffonly) [first_x, last_x, stafflines, thickness] = find_stafflines_int(im_staffonly) # ratio=staffline_height/staffspace_height thickness = (stafflines[1] - stafflines[0]) / (1 / ratio + 1) im_newlines = Image(im_full.ul, im_full.size) # we just draw the lines ourselves for y in stafflines: im_newlines.draw_line(Point(first_x, y), Point(last_x, y), RGBPixel(255, 255, 255), thickness) # new full: lines OR symbols def_full = im_staffless.or_image(im_newlines) # new staffonly: lines AND NOT symbols def_staffonly = im_staffless.image_copy() def_staffonly.invert() def_staffonly.and_image(im_newlines, True) # staffless image doesn't change def_staffless = im_staffless.image_copy() # construct skeletons staffline_skel = [] for y in stafflines: skel = StafflineSkeleton() skel.left_x = first_x # all stafflines are completely straight skel.y_list = (last_x - first_x + 1) * [y] staffline_skel.append(skel) return [def_full, def_staffonly, staffline_skel]
def show_result(self, highlight_groups=True): """Returns an RGB image with the found staff lines highlighted. Parameters: *highlight_groups*: when *True* (default), each stave system is underlayed in a different color. When false, only the edge points are marked in different colors, which is less visible for skeleton or average staff line representation. """ rgb = Image(self.image, RGB) if highlight_groups: # highlight staff systems for staffno, staff in enumerate(self.linelist): if not staff: continue staffcolor = RGBPixel(190 + (11*staffno) % 66, \ 190 + (31*(staffno + 1)) % 66, \ 190 + (51*(staffno + 2)) % 66) topline = staff[0].to_average() botline = staff[-1].to_average() leftx = min([topline.left_x, botline.left_x]) rightx = max([topline.right_x, botline.right_x]) topy = topline.average_y boty = botline.average_y border = max( [self.staffspace_height / 2, self.staffline_height * 4]) if leftx - border > 0: leftx -= border if rightx + border < rgb.ncols: rightx += border if topy - border > 0: topy -= border if boty + border < rgb.nrows: boty += border rgb.draw_filled_rect((leftx, topy), (rightx, boty), staffcolor) # draw original image rgb.highlight(self.image, RGBPixel(0, 0, 0)) for staffno, staff in enumerate(self.linelist): if not staff: continue if highlight_groups: staffcolor = RGBPixel(255, 0, 0) else: staffcolor = RGBPixel((53*(staffno)) % 256,\ (111*(staffno+1)) % 256,\ (111*(staffno+2)) % 256) for line in staff: if isinstance(line, StafflinePolygon): lastp = None for p in line.vertices: rgb.draw_marker(p,self.staffline_height*2,3,\ staffcolor) if lastp: rgb.draw_line(lastp, p, RGBPixel(255, 0, 0)) lastp = p elif isinstance(line, StafflineAverage): rgb.draw_line(Point(0,line.average_y),\ Point(rgb.ncols,line.average_y),\ RGBPixel(255,0,0)) rgb.draw_marker(Point(line.left_x,line.average_y),\ self.staffline_height*2,3,\ staffcolor) rgb.draw_marker(Point(line.right_x,line.average_y),\ self.staffline_height*2,3,\ staffcolor) elif isinstance(line, StafflineSkeleton): # doing this in Python might be too slow, # implement it in C++ later x = line.left_x for y in line.y_list: rgb.set(Point(x, y), RGBPixel(255, 0, 0)) x += 1 if len(line.y_list) > 0: rgb.draw_marker(Point(line.left_x,line.y_list[0]),\ self.staffline_height*2,3,\ staffcolor) rgb.draw_marker(Point(x-1,line.y_list[-1]),\ self.staffline_height*2,3,\ staffcolor) return rgb
def show_result(self, highlight_groups=True): """Returns an RGB image with the found staff lines highlighted. Parameters: *highlight_groups*: when *True* (default), each stave system is underlayed in a different color. When false, only the edge points are marked in different colors, which is less visible for skeleton or average staff line representation. """ rgb = Image(self.image, RGB) if highlight_groups: # highlight staff systems for staffno, staff in enumerate(self.linelist): if not staff: continue staffcolor = RGBPixel(190 + (11*staffno) % 66, \ 190 + (31*(staffno + 1)) % 66, \ 190 + (51*(staffno + 2)) % 66) topline = staff[0].to_average() botline = staff[-1].to_average() leftx = min([topline.left_x, botline.left_x]) rightx = max([topline.right_x, botline.right_x]) topy = topline.average_y boty = botline.average_y border = max([self.staffspace_height/2, self.staffline_height*4]) if leftx - border > 0: leftx -= border if rightx + border < rgb.ncols: rightx += border if topy - border > 0: topy -= border if boty + border < rgb.nrows: boty += border rgb.draw_filled_rect((leftx, topy), (rightx, boty), staffcolor) # draw original image rgb.highlight(self.image, RGBPixel(0, 0, 0)) for staffno, staff in enumerate(self.linelist): if not staff: continue if highlight_groups: staffcolor = RGBPixel(255,0,0) else: staffcolor = RGBPixel((53*(staffno)) % 256,\ (111*(staffno+1)) % 256,\ (111*(staffno+2)) % 256) for line in staff: if isinstance(line, StafflinePolygon): lastp = None for p in line.vertices: rgb.draw_marker(p,self.staffline_height*2,3,\ staffcolor) if lastp: rgb.draw_line(lastp,p,RGBPixel(255,0,0)) lastp = p elif isinstance(line, StafflineAverage): rgb.draw_line(Point(0,line.average_y),\ Point(rgb.ncols,line.average_y),\ RGBPixel(255,0,0)) rgb.draw_marker(Point(line.left_x,line.average_y),\ self.staffline_height*2,3,\ staffcolor) rgb.draw_marker(Point(line.right_x,line.average_y),\ self.staffline_height*2,3,\ staffcolor) elif isinstance(line, StafflineSkeleton): # doing this in Python might be too slow, # implement it in C++ later x = line.left_x for y in line.y_list: rgb.set(Point(x, y), RGBPixel(255, 0, 0)) x +=1 if len(line.y_list) > 0: rgb.draw_marker(Point(line.left_x,line.y_list[0]),\ self.staffline_height*2,3,\ staffcolor) rgb.draw_marker(Point(x-1,line.y_list[-1]),\ self.staffline_height*2,3,\ staffcolor) return rgb