Ejemplo n.º 1
0
Archivo: draw.py Proyecto: DDMAL/Gamera
 def __doc_example1__(images):
     image = Image((0, 0), Dim(100, 100), RGB, DENSE)
     for i in range(10):
         image.draw_filled_rect((randint(0, 100), randint(0, 100)),
                                (randint(0, 100), randint(0, 100)),
                                RGBPixel(randint(0, 255), randint(0, 255), randint(0, 255)))
     return image
    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]
Ejemplo n.º 3
0
  def __doc_example1__(images):
    from random import randint, seed
    from gamera.core import Image, Dim
    seed(0)
    image = Image((0, 0), Dim(320, 300), RGB, DENSE)

    # These are some various Unicode encoded names of different
    # languages (from the wikipedia.org front page).  Just a quick way
    # to test Unicode support.  I use the long encoding here so this
    # source file iteself will be portable.
    names = ['\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd1\x81\xd0\xba\xd0\xb8',
             '\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa',
             '\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\xd8\xa9',
             '\xc4\x8cesk\xc3\xa1',
             'Rom\xc3\xa2n\xc4\x83',
             '\xe1\x83\xa5\xe1\x83\x90\xe1\x83\xa0\xe1\x83\x97\xe1\x83\xa3\xe1\x83\x9a\xe1\x83\x98',
             '\xd0\x9c\xd0\xb0\xd0\xba\xd0\xb5\xd0\xb4\xd0\xbe\xd0\xbd\xd1\x81\xd0\xba\xd0\xb8',
             '\xc3\x8dslenska',
             'Lietuvi\xc5\xb3',
             'T\xc3\xbcrk\xc3\xa7e']
             
    try:
      for i, name in enumerate(names):
        image.draw_text((160, i * 30),
                        name, RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)), 20, halign=1)
    except Exception:
      pass
    return image
Ejemplo n.º 4
0
def init():
    if not has_version(3, 3, 2):
        raise RuntimeError('Gamera >= 3.3.2 is required')
    sys.modules['numpy'] = None
    result = _init()
    test_image = Image((0, 0), (5, 5), RGB)
    test_string = test_image._to_raw_string()
    try:
        sys.getrefcount
    except AttributeError:
        return result
    refcount = sys.getrefcount(test_string)
    if refcount >= 3:  # no coverage
        # See: https://groups.yahoo.com/neo/groups/gamera-devel/conversations/topics/2068
        raise RuntimeError('Memory leak in Gamera')
    else:
        assert refcount == 2
    try:
        PIL.fromstring = PIL.frombytes
        # Gamera still uses fromstring(), which was deprecated,
        # and finally removed in Pillow 3.0.0.
        # https://pillow.readthedocs.io/en/3.0.x/releasenotes/3.0.0.html#deprecated-methods
    except AttributeError:
        pass
    return result
Ejemplo n.º 5
0
    def __doc_example1__(images):
        from random import randint, seed
        from gamera.core import Image, Dim
        seed(0)
        image = Image((0, 0), Dim(320, 300), RGB, DENSE)

        # These are some various Unicode encoded names of different
        # languages (from the wikipedia.org front page).  Just a quick way
        # to test Unicode support.  I use the long encoding here so this
        # source file iteself will be portable.
        names = [
            '\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd1\x81\xd0\xba\xd0\xb8',
            '\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa',
            '\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\xd8\xa9',
            '\xc4\x8cesk\xc3\xa1', 'Rom\xc3\xa2n\xc4\x83',
            '\xe1\x83\xa5\xe1\x83\x90\xe1\x83\xa0\xe1\x83\x97\xe1\x83\xa3\xe1\x83\x9a\xe1\x83\x98',
            '\xd0\x9c\xd0\xb0\xd0\xba\xd0\xb5\xd0\xb4\xd0\xbe\xd0\xbd\xd1\x81\xd0\xba\xd0\xb8',
            '\xc3\x8dslenska', 'Lietuvi\xc5\xb3', 'T\xc3\xbcrk\xc3\xa7e'
        ]

        try:
            for i, name in enumerate(names):
                image.draw_text((160, i * 30),
                                name,
                                RGBPixel(randint(0, 255), randint(0, 255),
                                         randint(0, 255)),
                                20,
                                halign=1)
        except Exception:
            pass
        return image
Ejemplo n.º 6
0
Archivo: draw.py Proyecto: DDMAL/Gamera
 def __doc_example1__(images):
     image = Image((0, 0), Dim(100, 100), RGB, DENSE)
     for i in range(10):
         image.draw_circle((randint(0, 100), randint(0, 100)),
                           randint(0, 100),
                           RGBPixel(randint(0, 255), randint(0, 255), randint(0, 255)),
                           1.0, 0.1)
     return image
Ejemplo n.º 7
0
 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_filled_rect((randint(0, 100), randint(0, 100)),
                            (randint(0, 100), randint(0, 100)),
                            RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)))
   return image
Ejemplo n.º 8
0
 def __doc_example1__(images):
   from gamera.core import Image
   points = [(10,10),(20,30),(32,22),(85,14),(40,70),(80,85)]
   voronoi = Image((0,0),(90,90))
   voronoi.voronoi_from_points(points,[i+2 for i in range(len(points))])
   voronoi_edges = voronoi.labeled_region_edges()
   for p in points:
     voronoi_edges.set(p,1)
   return [voronoi_edges]
Ejemplo n.º 9
0
 def __doc_example1__(images):
     from gamera.core import Image
     points = [(10, 10), (20, 30), (32, 22), (85, 14), (40, 70), (80, 85)]
     voronoi = Image((0, 0), (90, 90))
     voronoi.voronoi_from_points(points,
                                 [i + 2 for i in range(len(points))])
     voronoi_edges = voronoi.labeled_region_edges()
     for p in points:
         voronoi_edges.set(p, 1)
     return [voronoi_edges]
Ejemplo n.º 10
0
 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_circle((randint(0, 100), randint(0, 100)),
                           randint(0, 100),
                           RGBPixel(randint(0, 255), randint(0, 255),
                                    randint(0, 255)), 1.0, 0.1)
     return image
Ejemplo n.º 11
0
 def __doc_example1__(images):
     from random import randint, seed
     from gamera.core import Image, Dim
     seed(0)
     image = Image((0, 0), Dim(100, 100), RGB, DENSE)
     for i in range(10):
         image.draw_filled_rect((randint(0, 100), randint(0, 100)),
                                (randint(0, 100), randint(0, 100)),
                                RGBPixel(randint(0, 255), randint(0, 255),
                                         randint(0, 255)))
     return image
Ejemplo n.º 12
0
 def __doc_example1__(images):
   from random import randint, seed
   from gamera.core import Image, Dim
   seed(0)
   image = Image((0, 0), Dim(100, 100), RGB, DENSE)
   for i in range(10):
     image.draw_circle((randint(0, 100), randint(0, 100)),
                       randint(0, 100),
                       RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)),
                       1.0, 0.1)
   return image
Ejemplo n.º 13
0
 def __doc_example1__(images):
     from gamera.core import Image, RGBPixel, Point as P
     img = Image((0, 0), (90, 90))
     points = [P(10, 10), P(20, 30), P(32, 22), P(85, 14), P(40, 70), P(80, 85)]
     for p in points:
         img.draw_filled_rect((p.x - 2, p.y - 2), (p.x + 1, p.y + 1), 1)
     r = img.max_empty_rect()
     rgb = img.to_rgb()
     rgb.draw_hollow_rect(r, RGBPixel(255, 0, 0))
     rgb.highlight(img, RGBPixel(0, 0, 0))
     return [rgb]
Ejemplo n.º 14
0
 def __doc_example1__(images):
     from gamera.toolkits.vector.plugins import transformation, render
     from gamera.core import Image, RGBPixel
     onebit = images[ONEBIT]
     paths = onebit.potrace()
     t = transformation.scale(3, 3)
     new_image = Image(0, 0, onebit.nrows*3, onebit.ncols*3, RGB)
     new_paths = paths.transform(t)
     for path in new_paths:
         new_image.draw_path_hollow(
             path, RGBPixel(255, 128, 128))
     return [onebit, new_image]
 def get_gamera_image(self):
     # Image has to be encoded Dense and not RLE.  If RLE, will seg fault
     image = GameraImage((self.ulx, self.uly),
                         (self.ulx + self.width, self.uly + self.height),
                         ONEBIT,
                         DENSE)
     # Build the image by pixels
     for x in range(self.width):
         for y in range(self.height):
             if self.pixel_matrix[x][y] == BinaryPixelEnum.BLACK:
                 image.set((x,y), 1)
             else:
                 image.set((x,y), 0)
     return image
  def __call__(self, size=10):
      from gamera.core import Image, GREYSCALE, Dim
      result = Image((0, 0), Dim(self.ncols * size, self.nrows * size), GREYSCALE)
      half_size = size / 2
 
      for r in xrange(self.nrows):
          for c in xrange(self.ncols):
              direction = self.get((c, r))
              if self.get((c, r)) != 0:
                  center_r = r * size + (size / 2)
                  size_c = c * size
                  subimage = result.subimage(Point(r*size, c*size), Dim(size, size))
                  dir = half_size * tan(direction)
                  subimage.draw_line((size_c, center_r - dir), (size_c + size, center_r + dir), 128)
      return result
Ejemplo n.º 17
0
  def __call__(self, size=10):
      from gamera.core import Image, GREYSCALE, Dim
      result = Image((0, 0), Dim(self.ncols * size, self.nrows * size), GREYSCALE)
      half_size = size / 2
 
      for r in xrange(self.nrows):
          for c in xrange(self.ncols):
              direction = self.get((c, r))
              if self.get((c, r)) != 0:
                  center_r = r * size + (size / 2)
                  size_c = c * size
                  subimage = result.subimage(Point(r*size, c*size), Dim(size, size))
                  dir = half_size * tan(direction)
                  subimage.draw_line((size_c, center_r - dir), (size_c + size, center_r + dir), 128)
      return result
    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]
Ejemplo n.º 19
0
 def __doc_example1__(images):
     from random import randint
     from gamera.core import Image, Dim
     image = Image((0, 0), Dim(100, 100), RGB, DENSE)
     points = [(randint(0, 100), randint(0, 100)) for x in range(4)]
     image.draw_bezier(*tuple(list(points) + [RGBPixel(255, 0, 0), 0.1]))
     image.draw_marker(points[0], 7, 0, RGBPixel(0, 0, 255))
     image.draw_marker(points[1], 7, 1, RGBPixel(0, 255, 0))
     image.draw_marker(points[2], 7, 1, RGBPixel(0, 255, 0))
     image.draw_marker(points[3], 7, 0, RGBPixel(0, 0, 255))
     return image
Ejemplo n.º 20
0
Archivo: draw.py Proyecto: DDMAL/Gamera
 def __doc_example1__(images):
     image = Image((0, 0), Dim(100, 100), RGB, DENSE)
     points = [(randint(0, 100), randint(0, 100)) for x in range(4)]
     image.draw_bezier(*tuple(list(points) + [RGBPixel(255, 0, 0), 0.1]))
     image.draw_marker(points[0], 7, 0, RGBPixel(0, 0, 255))
     image.draw_marker(points[1], 7, 1, RGBPixel(0, 255, 0))
     image.draw_marker(points[2], 7, 1, RGBPixel(0, 255, 0))
     image.draw_marker(points[3], 7, 0, RGBPixel(0, 0, 255))
     return image
Ejemplo n.º 21
0
 def __call__(self,speckle_size=3,filter=0):
     from gamera.core import Point, Image
     image=self.image_copy()
     if speckle_size > 0:
         image.despeckle(speckle_size)
     if filter == 0:
         filtered = image.rank(5)
     elif filter == 1:
         origin = Point(1,1)
         se = Image(Point(0,0),Point(2,2),ONEBIT)
         se.fill(1)
         filtered = image.binary_dilation(se,origin)
         filtered = filtered.binary_erosion(se,origin)
     else:
         image.invert()
         image.despeckle(5)
         image.invert()
         filtered = image
         
     return filtered
Ejemplo n.º 22
0
def init():
    if not has_version(3, 3, 2):
        raise RuntimeError('Gamera >= 3.3.2 is required')
    sys.modules['numpy'] = None
    result = _init()
    test_image = Image((0, 0), (5, 5), RGB)
    test_string = test_image._to_raw_string()
    refcount = sys.getrefcount(test_string)
    if refcount >= 3:  # no coverage
        # See: https://groups.yahoo.com/neo/groups/gamera-devel/conversations/topics/2068
        raise RuntimeError('Memory leak in Gamera')
    else:
        assert refcount == 2
    try:
        PIL.fromstring = PIL.frombytes
        # Gamera still uses fromstring(), which was deprecated,
        # and finally removed in Pillow 3.0.0.
        # https://pillow.readthedocs.io/en/3.0.x/releasenotes/3.0.0.html#deprecated-methods
    except AttributeError:
        pass
    return result
Ejemplo n.º 23
0
 def __doc_example1__(images):
   from random import randint, seed
   from gamera.core import Image, Dim
   seed(0)
   image = Image((0, 0), Dim(100, 100), RGB, DENSE)
   points = [(randint(0, 100), randint(0, 100)) for x in range(4)]
   image.draw_bezier(*tuple(list(points) + [RGBPixel(255, 0, 0), 0.1]))
   image.draw_marker(points[0], 7, 0, RGBPixel(0, 0, 255))
   image.draw_marker(points[1], 7, 1, RGBPixel(0, 255, 0))
   image.draw_marker(points[2], 7, 1, RGBPixel(0, 255, 0))
   image.draw_marker(points[3], 7, 0, RGBPixel(0, 0, 255))
   return image
Ejemplo n.º 24
0
    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 __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]
Ejemplo n.º 26
0
    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]
Ejemplo n.º 27
0
 def __doc_example1__(images):
     from gamera.core import Image
     from gamera.core import Point as P
     img = Image((0, 0), (90, 90))
     points = [
         P(10, 10),
         P(20, 30),
         P(32, 22),
         P(85, 14),
         P(40, 70),
         P(80, 85)
     ]
     for p in points:
         img.draw_filled_rect((p.x - 2, p.y - 2), (p.x + 1, p.y + 1), 1)
     r = img.max_empty_rect()
     rgb = img.to_rgb()
     rgb.draw_hollow_rect(r, RGBPixel(255, 0, 0))
     rgb.highlight(img, RGBPixel(0, 0, 0))
     return [rgb]
Ejemplo n.º 28
0
    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]
Ejemplo n.º 29
0
   def remove_staves(self, crossing_symbols='all', num_lines=5, adj_ratio=2.5, noise_size=5, alpha=10.0, beta=0.1, angle_threshold=3, string_join_factor=0.25, debug=False):
      """Detects and removes staff lines from a music/tablature image.

Signature:

  ``remove_staves(crossing_symbols='all', num_lines=5, adj_ratio=2.5, noise_size=5, alpha=10.0, beta=0.1, angle_threshold=3.0, string_join_factor=0.25)``

with

  *crossing_symbols*:
    Ignored.

  *num_lines*:
    The number of stafflines in each staff.  (Autodetection of number of stafflines not yet
    implemented).

It is unlikely one would need to provide the arguments below:

  *adj_ratio*
    The maximum ratio between adjacent vertical runs in order to be considered part of the
    same section.  Higher values of this number will result in fewer, larger sections.

  *noise_size*
    The maximum size of a section that will be considered noise.

  *alpha*
    The minimum aspect ratio of a potential staffline section.

  *beta*
    The minimum \"straightness\" of a potential staffline section (as given by the gamma fittness
    function of a least-squares fit line).

  *angle_threshold*
    The maximum distance from the mean angle a section may be to be considered as a potential
    staffline (in degrees).

  *string_join_factor*
    The threshold for joining filaments together into filament strings.

  *debug*
    When True, returns a tuple of images to help debug each stage of the algorithm.
    Times for each stages are also displayed.  The tuples elements are:

    *result*
       The normal result, with stafflines removed.

    *sections*
       Shows the segmentation by LAG.  Use display_ccs to show the segments differently
       coloured.

    *potential_stafflines*
       Shows the sections that are potentially part of stafflines

    *filament_strings*
       Shows how the vertically aligned potential stafflines are grouped into
       filament_strings

    *staffline_chunks*
       The filament strings believed to be part of stafflines

    *stafflines*
       Draws the modelled lines on top of the image.
"""
      if num_lines <= 0:
         num_lines = 5

      t = time.clock()
      sections = self._make_line_adjacency_graph(self.image, adj_ratio)
      if debug:
         print ("Line adjacency graph time: %.04f, num sections: %d" %
                (time.clock() - t, len(sections)))
         sections_image = Image(self.image)
         for i, section in enumerate(sections):
            section.i = i
            for run in section.runs:
               sections_image.subimage(run).fill(i+1)

      t = time.clock()
      sections = self._remove_noise(sections, noise_size)
      if debug:
         print ("Remove noise time: %.04f, num sections: %d" %
                (time.clock() - t, len(sections)))

      t = time.clock()
      self._calculate_features(sections)
      if debug:
         print "Calculate features time: %.04f" % (time.clock() - t)

      t = time.clock()
      filaments = self._find_potential_stafflines(sections, alpha, beta, angle_threshold)
      if debug:
         print ("Find potential stafflines time: %.04f, num filaments: %d" %
                (time.clock() - t, len(filaments)))
         filaments_image = Image(self.image)
         for i, section in enumerate(filaments):
            if section.is_filament:
               for run in section.runs:
                  filaments_image.subimage(run).fill(i+1)

      t = time.clock()
      strings = self._create_filament_strings(filaments, string_join_factor)
      if debug:
         print ("Create filament strings time: %.04f, num strings: %d" %
                (time.clock() - t, len(strings)))
         strings_image = Image(self.image)
         for i, string in enumerate(strings):
            for filament in string.filaments:
               for run in filament.runs:
                  strings_image.subimage(run).fill(i+1)

      del filaments

      t = time.clock()
      staffline_strings = self._find_stafflines(strings, num_lines)
      if debug:
         print ("Find stafflines time: %.04f num staffline fragments: %d" %
                (time.clock() - t, len(staffline_strings)))
         staffline_image = Image(self.image)
         staffline_model_image = self.image.to_rgb()
         for i, staffline in enumerate(staffline_strings):
            for section in staffline.filaments:
               for run in section.runs:
                  staffline_image.subimage(run).fill(i+1)

      if len(staffline_strings) == 0:
         return self.image

      t = time.clock()
      line_groupings = self._trace_stafflines(sections, staffline_strings, angle_threshold)
      if debug:
         print ("Trace stafflines time: %.04f" % (time.clock() - t))

      t = time.clock()
      self._remove_stafflines(sections)
      if debug:
         print ("Remove stafflines time: %.04f" % (time.clock() - t))

      t = time.clock()
      self.stafflines = self._make_line_models(line_groupings)
      if debug:
         print ("Make line models time: %.04f" % (time.clock() - t))
         for y, staffline in self.stafflines.stafflines:
            last_point = staffline[0]
            for point in staffline[1:]:
               staffline_model_image.draw_line(last_point, point, RGBPixel(255, 0, 0))
               last_point = point
         return (self.image, sections_image, filaments_image, strings_image,
                 staffline_image, staffline_model_image)
      else:
         return self.image
Ejemplo n.º 30
0
    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 remove_staves(self, crossing_symbols='all', num_lines=5, adj_ratio=2.5, noise_size=5, alpha=10.0, beta=0.1, angle_threshold=3, string_join_factor=0.25, debug=False):
      """Detects and removes staff lines from a music/tablature image.

Signature:

  ``remove_staves(crossing_symbols='all', num_lines=5, adj_ratio=2.5, noise_size=5, alpha=10.0, beta=0.1, angle_threshold=3.0, string_join_factor=0.25)``

with

  *crossing_symbols*:
    Ignored.

  *num_lines*:
    The number of stafflines in each staff.  (Autodetection of number of stafflines not yet
    implemented).

It is unlikely one would need to provide the arguments below:

  *adj_ratio*
    The maximum ratio between adjacent vertical runs in order to be considered part of the
    same section.  Higher values of this number will result in fewer, larger sections.

  *noise_size*
    The maximum size of a section that will be considered noise.

  *alpha*
    The minimum aspect ratio of a potential staffline section.

  *beta*
    The minimum \"straightness\" of a potential staffline section (as given by the gamma fittness
    function of a least-squares fit line).

  *angle_threshold*
    The maximum distance from the mean angle a section may be to be considered as a potential
    staffline (in degrees).

  *string_join_factor*
    The threshold for joining filaments together into filament strings.

  *debug*
    When True, returns a tuple of images to help debug each stage of the algorithm.
    Times for each stages are also displayed.  The tuples elements are:

    *result*
       The normal result, with stafflines removed.

    *sections*
       Shows the segmentation by LAG.  Use display_ccs to show the segments differently
       coloured.

    *potential_stafflines*
       Shows the sections that are potentially part of stafflines

    *filament_strings*
       Shows how the vertically aligned potential stafflines are grouped into
       filament_strings

    *staffline_chunks*
       The filament strings believed to be part of stafflines

    *stafflines*
       Draws the modelled lines on top of the image.
"""
      if num_lines <= 0:
         num_lines = 5

      t = time.clock()
      sections = self._make_line_adjacency_graph(self.image, adj_ratio)
      if debug:
         print ("Line adjacency graph time: %.04f, num sections: %d" %
                (time.clock() - t, len(sections)))
         sections_image = Image(self.image)
         for i, section in enumerate(sections):
            section.i = i
            for run in section.runs:
               sections_image.subimage(run).fill(i+1)

      t = time.clock()
      sections = self._remove_noise(sections, noise_size)
      if debug:
         print ("Remove noise time: %.04f, num sections: %d" %
                (time.clock() - t, len(sections)))

      t = time.clock()
      self._calculate_features(sections)
      if debug:
         print "Calculate features time: %.04f" % (time.clock() - t)

      t = time.clock()
      filaments = self._find_potential_stafflines(sections, alpha, beta, angle_threshold)
      if debug:
         print ("Find potential stafflines time: %.04f, num filaments: %d" %
                (time.clock() - t, len(filaments)))
         filaments_image = Image(self.image)
         for i, section in enumerate(filaments):
            if section.is_filament:
               for run in section.runs:
                  filaments_image.subimage(run).fill(i+1)

      t = time.clock()
      strings = self._create_filament_strings(filaments, string_join_factor)
      if debug:
         print ("Create filament strings time: %.04f, num strings: %d" %
                (time.clock() - t, len(strings)))
         strings_image = Image(self.image)
         for i, string in enumerate(strings):
            for filament in string.filaments:
               for run in filament.runs:
                  strings_image.subimage(run).fill(i+1)

      del filaments

      t = time.clock()
      staffline_strings = self._find_stafflines(strings, num_lines)
      if debug:
         print ("Find stafflines time: %.04f num staffline fragments: %d" %
                (time.clock() - t, len(staffline_strings)))
         staffline_image = Image(self.image)
         staffline_model_image = self.image.to_rgb()
         for i, staffline in enumerate(staffline_strings):
            for section in staffline.filaments:
               for run in section.runs:
                  staffline_image.subimage(run).fill(i+1)

      if len(staffline_strings) == 0:
         return self.image

      t = time.clock()
      line_groupings = self._trace_stafflines(sections, staffline_strings, angle_threshold)
      if debug:
         print ("Trace stafflines time: %.04f" % (time.clock() - t))

      t = time.clock()
      self._remove_stafflines(sections)
      if debug:
         print ("Remove stafflines time: %.04f" % (time.clock() - t))

      t = time.clock()
      self.stafflines = self._make_line_models(line_groupings)
      if debug:
         print ("Make line models time: %.04f" % (time.clock() - t))
         for y, staffline in self.stafflines.stafflines:
            last_point = staffline[0]
            for point in staffline[1:]:
               staffline_model_image.draw_line(last_point, point, RGBPixel(255, 0, 0))
               last_point = point
         return (self.image, sections_image, filaments_image, strings_image,
                 staffline_image, staffline_model_image)
      else:
         return self.image
Ejemplo n.º 32
0
    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
Ejemplo n.º 33
0
    def __call__(self,
                 p,
                 text,
                 color,
                 size=10,
                 font_family=0,
                 italic=False,
                 bold=False,
                 halign=0):
        from gamera.core import Dim, RGB, ONEBIT, Image
        from gamera.plugins import string_io
        try:
            import wx
        except ImportError:
            raise RuntimeError("Drawing text requires wxPython.")
        try:
            dc = wx.MemoryDC()
        except wx._core.PyNoAppError:
            app = wx.App()
            dc = wx.MemoryDC()
        if font_family < 0 or font_family > 2:
            raise ValueError("font_family must be in range 0-2.")
        font_family = [wx.ROMAN, wx.SWISS, wx.MODERN][font_family]
        italic = (italic and wx.ITALIC) or wx.NORMAL
        bold = (bold and wx.BOLD) or wx.NORMAL
        if type(text) == str:
            encoding = wx.FONTENCODING_SYSTEM
        elif type(text) == unicode:
            encoding = wx.FONTENCODING_UNICODE
        else:
            raise ValueError("text must be a string or unicode string.")
        font = wx.Font(size, font_family, italic, bold, encoding=encoding)
        font.SetPixelSize(wx.Size(size * 2, size * 2))
        dc.SetFont(font)
        w, h = dc.GetTextExtent(text)

        # Do the actual drawing
        bmp = wx.EmptyBitmap(w, h, -1)
        dc.SelectObject(bmp)
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(wx.WHITE_BRUSH)
        dc.DrawRectangle(0, 0, w, h)
        dc.SetBrush(wx.BLACK_BRUSH)
        dc.DrawText(text, 0, 0)
        img = bmp.ConvertToImage()
        img_str = img.GetData()

        text_image = string_io._from_raw_string((0, 0), Dim(w, h), RGB, 0,
                                                img_str)
        text_image = text_image.to_onebit()
        if halign == 1:
            p = (p[0] - w / 2, p[1])
        elif halign == 2:
            p = (p[0] - w, p[1])

        ul = (max(p[0], self.ul_x), max(p[1], self.ul_y))
        lr = (min(p[0] + w, self.lr_x), min(p[1] + h, self.lr_y))
        w = lr[0] - ul[0]
        h = lr[1] - ul[1]
        if w < 0 or h < 0:
            return

        text_image = text_image.subimage(
            (max(self.ul_x - p[0], 0), max(self.ul_y - p[1], 0)), Dim(w, h))
        if self.data.pixel_type == ONEBIT:
            subimage = self.subimage(ul, Dim(w, h))
            if color:
                subimage.or_image(text_image, in_place=True)
            else:
                text_image.invert()
                subimage.and_image(text_image, in_place=True)
        elif self.data.pixel_type == RGB:
            subimage = Image(
                (max(ul[0] - self.ul_x, p[0]), max(ul[1] - self.ul_y, p[1])),
                Dim(w, h), ONEBIT)
            subimage.or_image(text_image, in_place=True)
            self.highlight(subimage, color)
Ejemplo n.º 34
0
  def __call__(self, p, text, color, size=10, font_family=0,
               italic=False, bold=False, halign=0):
    from gamera.core import Dim, RGB, ONEBIT, Image
    from gamera.plugins import string_io
    try:
      import wx
      from gamera.gui import compat_wx
    except ImportError:
      raise RuntimeError("Drawing text requires wxPython.")
    try:
      dc = wx.MemoryDC()
    except wx._core.PyNoAppError:
      app = wx.App()
      dc = wx.MemoryDC()
    if font_family < 0 or font_family > 2:
      raise ValueError("font_family must be in range 0-2.")
    font_family = [wx.ROMAN, wx.SWISS, wx.MODERN][font_family]
    italic = (italic and wx.ITALIC) or wx.NORMAL
    bold = (bold and wx.BOLD) or wx.NORMAL
    if type(text) == str:
      encoding = wx.FONTENCODING_SYSTEM
    elif type(text) == unicode:
      encoding = wx.FONTENCODING_UNICODE
    else:
      raise ValueError("text must be a string or unicode string.")
    font = wx.Font(size, font_family, italic, bold,
                   encoding = encoding)
    font.SetPixelSize(wx.Size(size * 2, size * 2))
    dc.SetFont(font)
    w, h = dc.GetTextExtent(text)

    # Do the actual drawing
    bmp = compat_wx.create_empty_bitmap(w, h, -1)
    dc.SelectObject(bmp)
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.SetBrush(wx.WHITE_BRUSH)
    dc.DrawRectangle(0, 0, w, h)
    dc.SetBrush(wx.BLACK_BRUSH)
    dc.DrawText(text, 0, 0)
    img = bmp.ConvertToImage()
    img_str = img.GetData()
    
    text_image = string_io._from_raw_string(
      (0, 0), Dim(w, h), RGB, 0, img_str)
    text_image = text_image.to_onebit()
    if halign == 1:
      p = (p[0] - w / 2, p[1])
    elif halign == 2:
      p = (p[0] - w, p[1])

    ul = (max(p[0], self.ul_x), max(p[1], self.ul_y))
    lr = (min(p[0] + w, self.lr_x), min(p[1] + h, self.lr_y))
    w = lr[0] - ul[0]
    h = lr[1] - ul[1]
    if w < 0 or h < 0:
      return
    
    text_image = text_image.subimage(
      (max(self.ul_x - p[0], 0), max(self.ul_y - p[1], 0)),
      Dim(w, h))
    if self.data.pixel_type == ONEBIT:
      subimage = self.subimage(ul, Dim(w, h))
      if color:
        subimage.or_image(text_image, in_place=True)
      else:
        text_image.invert()
        subimage.and_image(text_image, in_place=True)
    elif self.data.pixel_type == RGB:
      subimage = Image((max(ul[0] - self.ul_x, p[0]),
                        max(ul[1] - self.ul_y, p[1])),
                       Dim(w, h), ONEBIT)
      subimage.or_image(text_image, in_place=True)
      self.highlight(subimage, color)