def compute_line_seeds(binary, bottom, top, colseps, threshold, vscale, scale, debug=False):
"""Base on gradient maps, computes candidates for baselines
and xheights. Then, it marks the regions between the two
as a line seed."""
t = threshold
vrange = int(vscale*scale)
bmarked = maximum_filter(bottom==maximum_filter(bottom, (vrange, 0)),(2,2))
bmarked = bmarked*(bottom>t*np.amax(bottom)*t)*(1-colseps)
tmarked = maximum_filter(top==maximum_filter(top,(vrange,0)),(2,2))
tmarked = tmarked*(top>t*np.amax(top)*t/2)*(1-colseps)
tmarked = maximum_filter(tmarked,(1,20))
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 np.where(bmarked[:,x])[0]]+[(y,0) for y in np.where(tmarked[:,x][0])])[::-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 = seeds*(1-colseps)
if debug:
debug_show([seeds,0.3*tmarked+0.7*bmarked,binary], "lineseeds")
seeds,_ = morph.label(seeds)
return seeds
评论列表
文章目录