Beispiel #1
0
 def charseg(self, line):
     """Segment a text line into potential character parts."""
     assert mean(line) > 0.5 * amax(line)
     line0 = amax(line) - line
     ccuts = contourcuts(line0,
                         maxdist=self.maxdist,
                         minrange=self.minrange,
                         mincdist=self.mincdist,
                         sigma=self.sigma,
                         r=self.rr,
                         s=self.s)
     line = maximum(0, line0 - ccuts)
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,
                         imweight=self.imweight,
                         bweight=self.bweight,
                         diagweight=self.diagweight,
                         debug=self.debug,
                         r=self.r)
     tracks = array(tracks < 0.5 * amax(tracks), 'i')
     tracks, _ = morph.label(tracks)
     self.tracks = tracks
     stracks = morph.spread_labels(tracks)
     rsegs = stracks * (line0 > 0.5 * amax(line0))
     if self.debug:
         figure("temp")
         subplot(311)
         morph.showlabels(tracks)
         subplot(312)
         morph.showlabels(stracks)
         subplot(313)
         morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
Beispiel #2
0
 def charseg(self, line):
     """Segment a text line into potential character parts."""
     assert mean(line) > 0.5 * amax(line)
     line = amax(line) - line
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,
                         imweight=self.imweight,
                         bweight=self.bweight,
                         diagweight=self.diagweight,
                         debug=self.debug,
                         r=self.r,
                         threshold=self.threshold)
     tracks = array(tracks < 0.5 * amax(tracks), 'i')
     tracks, _ = morph.label(tracks)
     self.tracks = tracks  # for debuggin
     stracks = morph.spread_labels(tracks)
     rsegs = stracks * (line > 0.5 * amax(line))
     if 0:
         figure("temp")
         subplot(311)
         morph.showlabels(tracks)
         subplot(312)
         morph.showlabels(stracks)
         subplot(313)
         morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
Beispiel #3
0
def remove_noise(line,minsize=8):
    """Remove small pixels from an image."""
    if minsize==0: return line
    bin = (line>0.5*amax(line))
    labels,n = morph.label(bin)
    sums = measurements.sum(bin,labels,range(n+1))
    sums = sums[labels]
    good = minimum(bin,1-(sums>0)*(sums<minsize))
    return good
Beispiel #4
0
def non_noise_components(seg,threshold=0.1):
    """Estimate the number of non-noise connected components in a character
    image. This computes the size of all connected components, and it considers
    all components of size less then `threshold` times the size of the largest
    component to be noise."""
    seg = 1*(seg>0)
    labels,n = morph.label(seg)
    totals = measurements.sum(seg,labels,range(1,n+1))
    return sum(totals>amax(totals)*threshold)
Beispiel #5
0
def ccslineseg(image,debug=0):
    image = 1.0*(image>0.3*amax(image))
    sigma = 10.0
    smooth = filters.gaussian_filter(image,(sigma,1.0*sigma),mode='constant')
    center = (smooth==amax(smooth,axis=0)[newaxis,:])
    center = filters.maximum_filter(center,(3,3))
    center = morph.keep_marked(image>0.5,center)
    center = filters.maximum_filter(center,(2,2))
    center,_ = morph.label(center)
    center = morph.spread_labels(center)
    center *= image
    return center
Beispiel #6
0
def ccslineseg(image,debug=0):
    image = 1.0*(image>0.3*amax(image))
    sigma = 10.0
    smooth = filters.gaussian_filter(image,(sigma,1.0*sigma),mode='constant')
    center = (smooth==amax(smooth,axis=0)[newaxis,:])
    center = filters.maximum_filter(center,(3,3))
    center = morph.keep_marked(image>0.5,center)
    center = filters.maximum_filter(center,(2,2))
    center,_ = morph.label(center)
    center = morph.spread_labels(center)
    center *= image
    return center
Beispiel #7
0
def remove_noise(line, minsize=8):
    """Remove small pixels from an image."""
    if minsize == 0:
        return line
    bin = (line > 0.5 * amax(line))
    labels, n = morph.label(bin)
    print("remove_noise: n=%d labels=%s" % (n, desc(labels)))
    sums = measurements.sum(bin, labels, range(n + 1))
    sums = sums[labels]
    print("sums=%s" % desc(sums))
    good = minimum(bin, 1 - (sums > 0) * (sums < minsize))
    print("good=%s" % desc(good))
    # assert False
    return good
Beispiel #8
0
 def charseg(self,line):
     """Segment a text line into potential character parts."""
     assert mean(line)>0.5*amax(line)
     line = amax(line)-line
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,imweight=self.imweight,bweight=self.bweight,
                         diagweight=self.diagweight,debug=self.debug,r=self.r,
                         threshold=self.threshold)
     tracks = array(tracks<0.5*amax(tracks),'i')
     tracks,_ = morph.label(tracks)
     self.tracks = tracks # for debuggin
     stracks = morph.spread_labels(tracks)
     rsegs = stracks*(line>0.5*amax(line))
     if 0:
         figure("temp")
         subplot(311); morph.showlabels(tracks)
         subplot(312); morph.showlabels(stracks)
         subplot(313); morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
Beispiel #9
0
 def charseg(self,line):
     """Segment a text line into potential character parts."""
     assert mean(line)>0.5*amax(line)
     line0 = amax(line)-line
     ccuts = contourcuts(line0,maxdist=self.maxdist,minrange=self.minrange,
                         mincdist=self.mincdist,sigma=self.sigma,r=self.rr,s=self.s)
     line = maximum(0,line0-ccuts)
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,imweight=self.imweight,bweight=self.bweight,
                         diagweight=self.diagweight,debug=self.debug,r=self.r)
     tracks = array(tracks<0.5*amax(tracks),'i')
     tracks,_ = morph.label(tracks)
     self.tracks = tracks
     stracks = morph.spread_labels(tracks)
     rsegs = stracks*(line0>0.5*amax(line0))
     if self.debug:
         figure("temp")
         subplot(311); morph.showlabels(tracks)
         subplot(312); morph.showlabels(stracks)
         subplot(313); morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
Beispiel #10
0
def number_of_holes(seg):
    holes = morphology.binary_fill_holes(seg)-seg
    _,n = morph.label(holes)
    return n
Beispiel #11
0
def binary_objects(binary):
    labels, n = morph.label(binary)
    objects = morph.find_objects(labels)
    return objects
Beispiel #12
0
def binary_objects(binary):
    labels,n = morph.label(binary)
    objects = morph.find_objects(labels)
    return objects