Esempio n. 1
0
def compute_line_seeds(binary, bottom, top, colseps, scale):
    """Base on gradient maps, computes candidates for baselines
    and xheights.  Then, it marks the regions between the two
    as a line seed."""
    t = args.threshold  # 0.2###############################################################more focus here for bigger fonts.!!!
    # print "SbU", t
    vrange = int(args.vscale * scale)
    bmarked = maximum_filter(bottom == maximum_filter(bottom, (vrange, 0)),
                             (2, 2))
    bmarked *= array((bottom > t * amax(bottom) * t) * (1 - colseps),
                     dtype=bool)
    tmarked = maximum_filter(top == maximum_filter(top, (vrange, 0)), (2, 2))
    tmarked *= array((top > t * amax(top) * t / 2) * (1 - colseps), dtype=bool)
    tmarked = maximum_filter(tmarked, (1, 20))
    testseeds = zeros(binary.shape, 'i')
    seeds = zeros(binary.shape, 'i')
    delta = max(3, int(scale / 2))
    for x in range(bmarked.shape[1]):
        transitions = sorted([(y, 1) for y in psegutils.find(bmarked[:, x])] +
                             [(y, 0)
                              for y in psegutils.find(tmarked[:, x])])[::-1]
        transitions += [(0, 0)]
        for l in range(len(transitions) - 1):
            y0, s0 = transitions[l]
            if s0 == 0:
                continue
            seeds[y0 - delta:y0, x] = 1
            y1, s1 = transitions[l + 1]
            if s1 == 0 and (y0 - y1) < 5 * scale:
                seeds[y1:y0, x] = 1
    seeds = maximum_filter(seeds, (1, int(1 + scale)))
    seeds *= (1 - colseps)
    ###
    ####imsave('/home/gupta/Documents/seeds.png', seeds)
    ####imsave('/home/gupta/Documents/top_bottom.png', [testseeds,0.3*tmarked+0.7*bmarked,binary])
    ###
    ####imsave('/home/gupta/Documents/lineseeds.png', [seeds,0.3*tmarked+0.7*bmarked,binary])
    # DSAVE("lineseeds",[seeds,0.3*tmarked+0.7*bmarked,binary])
    seeds, _ = morph.label(seeds)
    return seeds
 def compute_line_seeds(self, binary, bottom, top, colseps, scale):
     """Base on gradient maps, computes candidates for baselines
     and xheights.  Then, it marks the regions between the two
     as a line seed."""
     t = self.parameter['threshold']
     vrange = int(self.parameter['vscale'] * scale)
     bmarked = maximum_filter(bottom == maximum_filter(bottom, (vrange, 0)),
                              (2, 2))
     bmarked *= np.array((bottom > t * np.amax(bottom) * t) * (1 - colseps),
                         dtype=bool)
     tmarked = maximum_filter(top == maximum_filter(top, (vrange, 0)),
                              (2, 2))
     tmarked *= np.array((top > t * np.amax(top) * t / 2) * (1 - colseps),
                         dtype=bool)
     tmarked = maximum_filter(tmarked, (1, 20))
     testseeds = np.zeros(binary.shape, 'i')
     seeds = np.zeros(binary.shape, 'i')
     delta = max(3, int(scale / 2))
     for x in range(bmarked.shape[1]):
         transitions = sorted([(y, 1)
                               for y in psegutils.find(bmarked[:, x])] +
                              [(y, 0)
                               for y in psegutils.find(tmarked[:,
                                                               x])])[::-1]
         transitions += [(0, 0)]
         for l in range(len(transitions) - 1):
             y0, s0 = transitions[l]
             if s0 == 0:
                 continue
             seeds[y0 - delta:y0, x] = 1
             y1, s1 = transitions[l + 1]
             if s1 == 0 and (y0 - y1) < 5 * scale:
                 seeds[y1:y0, x] = 1
     seeds = maximum_filter(seeds, (1, int(1 + scale)))
     seeds *= (1 - colseps)
     seeds, _ = morph.label(seeds)
     return seeds