def compute_colseps_conv(binary,scale=1.0):
"""Find column separators by convoluation and
thresholding."""
h,w = binary.shape
# find vertical whitespace by thresholding
smoothed = gaussian_filter(1.0*binary,(scale,scale*0.5))
smoothed = uniform_filter(smoothed,(5.0*scale,1))
thresh = (smoothed<amax(smoothed)*0.1)
DSAVE("1thresh",thresh)
# find column edges by filtering
grad = gaussian_filter(1.0*binary,(scale,scale*0.5),order=(0,1))
grad = uniform_filter(grad,(10.0*scale,1))
# grad = abs(grad) # use this for finding both edges
grad = (grad>0.5*amax(grad))
DSAVE("2grad",grad)
# combine edges and whitespace
seps = minimum(thresh,maximum_filter(grad,(int(scale),int(5*scale))))
seps = maximum_filter(seps,(int(2*scale),1))
DSAVE("3seps",seps)
# select only the biggest column separators
seps = morph.select_regions(seps,sl.dim0,min=args['csminheight']*scale,nbest=args['maxcolseps'])
DSAVE("4seps",seps)
return seps
python类uniform_filter()的实例源码
def compute_gradmaps(binary,scale):
# use gradient filtering to find baselines
boxmap = psegutils.compute_boxmap(binary,scale)
cleaned = boxmap*binary
DSAVE("cleaned",cleaned)
if args['usegause']:
# this uses Gaussians
grad = gaussian_filter(1.0*cleaned,(args['vscale']*0.3*scale,
args['hscale']*6*scale),order=(1,0))
else:
# this uses non-Gaussian oriented filters
grad = gaussian_filter(1.0*cleaned,(max(4,args['vscale']*0.3*scale),
args['hscale']*scale),order=(1,0))
grad = uniform_filter(grad,(args['vscale'],args['hscale']*6*scale))
bottom = ocrolib.norm_max((grad<0)*(-grad))
top = ocrolib.norm_max((grad>0)*grad)
return bottom,top,boxmap
def draw_blur_levels():
import matplotlib.pyplot as plt
from skimage import io
image = io.imread('out/66.png') # 36 for $, 79 for O
fig, axes = plt.subplots(nrows=2, ncols=3,
subplot_kw={'adjustable': 'box-forced'})
ax = axes.ravel()
for blur_level in range(6):
blurred = uniform_filter(image, 3.0*blur_level, mode='reflect', cval=0)
ax[blur_level].imshow(blurred, cmap='gray', interpolation='nearest')
ax[blur_level].set_title(str(blur_level), fontsize=20)
plt.show()
def measure(self,line):
h,w = line.shape
smoothed = filters.gaussian_filter(line,(h*0.5,h*self.smoothness),mode='constant')
smoothed += 0.001*filters.uniform_filter(smoothed,(h*0.5,w),mode='constant')
self.shape = (h,w)
a = argmax(smoothed,axis=0)
a = filters.gaussian_filter(a,h*self.extra)
self.center = array(a,'i')
deltas = abs(arange(h)[:,newaxis]-self.center[newaxis,:])
self.mad = mean(deltas[line!=0])
self.r = int(1+self.range*self.mad)
if self.debug:
figure("center")
imshow(line,cmap=cm.gray)
plot(self.center)
ginput(1,1000)
def compute_gradmaps(binary, scale, usegauss, vscale, hscale, debug=False):
# use gradient filtering to find baselines
boxmap = psegutils.compute_boxmap(binary,scale)
cleaned = boxmap*binary
if debug:
debug_show(cleaned, "cleaned")
if usegauss:
# this uses Gaussians
grad = gaussian_filter(1.0*cleaned,(vscale*0.3*scale,
hscale*6*scale),
order=(1,0))
else:
# this uses non-Gaussian oriented filters
grad = gaussian_filter(1.0*cleaned, (max(4, vscale*0.3*scale),
hscale*scale ), order=(1,0))
grad = uniform_filter(grad, (vscale, hscale*6*scale))
if debug:
debug_show(grad, "compute_gradmaps grad")
bottom = ocrolib.norm_max((grad<0)*(-grad))
top = ocrolib.norm_max((grad>0)*grad)
if debug:
debug_show(bottom, "compute_gradmaps bottom")
debug_show(top, "compute_gradmaps top")
return bottom, top, boxmap
def measure(self,line):
h,w = line.shape
smoothed = filters.gaussian_filter(line,(h*0.5,h*self.smoothness),mode='constant')
smoothed += 0.001*filters.uniform_filter(smoothed,(h*0.5,w),mode='constant')
self.shape = (h,w)
a = argmax(smoothed,axis=0)
a = filters.gaussian_filter(a,h*self.extra)
self.center = array(a,'i')
deltas = abs(arange(h)[:,newaxis]-self.center[newaxis,:])
self.mad = mean(deltas[line!=0])
self.r = int(1+self.range*self.mad)
if self.debug:
figure("center")
imshow(line,cmap=cm.gray)
plot(self.center)
ginput(1,1000)
def compute_colseps_mconv(binary,scale=1.0):
"""Find column separators using a combination of morphological
operations and convolution."""
h,w = binary.shape
smoothed = gaussian_filter(1.0*binary,(scale,scale*0.5))
smoothed = uniform_filter(smoothed,(5.0*scale,1))
thresh = (smoothed<amax(smoothed)*0.1)
DSAVE("1thresh",thresh)
blocks = morph.rb_closing(binary,(int(4*scale),int(4*scale)))
DSAVE("2blocks",blocks)
seps = minimum(blocks,thresh)
seps = morph.select_regions(seps,sl.dim0,min=args['csminheight']*scale,nbest=args['maxcolseps'])
DSAVE("3seps",seps)
blocks = morph.r_dilation(blocks,(5,5))
DSAVE("4blocks",blocks)
seps = maximum(seps,1-blocks)
DSAVE("5combo",seps)
return seps
def rb_dilation(image,size,origin=0):
"""Binary dilation using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=0)
return array(output>0,'i')
def rb_erosion(image,size,origin=0):
"""Binary erosion using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=1)
return array(output==1,'i')
def generate_blurred_images(image, blur_factor, levels):
images = []
for blur_level in range(levels):
#images.append(gaussian_filter(image, blur_factor*blur_level, mode='reflect', cval=0))
blurred = uniform_filter(image, blur_factor*blur_level, mode='reflect')
images.append(blurred / (blur_factor+1))
# TODO 'constant', or 'reflect'
return images
def rb_dilation(image,size,origin=0):
"""Binary dilation using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=0)
return array(output>0,'i')
def rb_erosion(image,size,origin=0):
"""Binary erosion using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=1)
return array(output==1,'i')
def compute_colseps_conv(binary, csminheight, maxcolseps, scale=1.0, debug=False):
"""Find column separators by convoluation and
thresholding."""
h,w = binary.shape
# find vertical whitespace by thresholding
smoothed = gaussian_filter(1.0 * binary, (scale, scale*0.5))
smoothed = uniform_filter(smoothed, (5.0*scale,1))
thresh = (smoothed<np.amax(smoothed)*0.1)
if debug:
debug_show(thresh, "compute_colseps_conv thresh")
# find column edges by filtering
grad = gaussian_filter(1.0*binary, (scale, scale*0.5), order=(0,1))
grad = uniform_filter(grad, (10.0*scale,1))
# grad = abs(grad) # use this for finding both edges
grad = (grad>0.5*np.amax(grad))
if debug:
debug_show(grad, "compute_colseps_conv grad")
# combine edges and whitespace
seps = np.minimum(thresh,maximum_filter(grad, (int(scale), int(5*scale))))
seps = maximum_filter(seps,(int(2*scale),1))
if debug:
debug_show(seps, "compute_colseps_conv seps")
# select only the biggest column separators
seps = morph.select_regions(seps,sl.dim0,
min=csminheight*scale,
nbest=maxcolseps)
if debug:
debug_show(seps, "compute_colseps_conv 4seps")
return seps
def rb_dilation(image,size,origin=0):
"""Binary dilation using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=0)
return array(output>0,'i')
def rb_erosion(image,size,origin=0):
"""Binary erosion using linear filters."""
output = zeros(image.shape,'f')
filters.uniform_filter(image,size,output=output,origin=origin,mode='constant',cval=1)
return array(output==1,'i')