In [78]:
from pylab import *
import ocrolib
from scipy.ndimage import filters,interpolation
from scipy import stats
In [130]:
target_h = 32 # target height for line
perc = 80 # percentile at which we score
dest = 0.5 # destination fraction for percentile
In [131]:
figsize(8,8)
image = 1-ocrolib.read_image_gray("010036.bin.png")
image = interpolation.affine_transform(image,array([[1,0.03],[0,1]]),offset=(-30,0),output_shape=(100,700))
imshow(image,cmap=cm.gray)
h,w = image.shape
In [132]:
smoothed = filters.gaussian_filter(image,(10,30))
imshow(smoothed,cmap=cm.gray)
Out[132]:
In [133]:
ys = arange(len(smoothed))
xs = arange(len(smoothed[0]))
center = sum((smoothed*ys[:,newaxis]),axis=0)/sum(smoothed,axis=0)
plot(center)
Out[133]:
In [134]:
p = polyfit(xs,center,1)
In [135]:
plot(center)
plot(polyval(p,xs))
print p
m,b = p
In [136]:
figsize(18,10)
imshow(image,cmap=cm.gray)
plot(polyval(p,xs))
Out[136]:
In [137]:
ys = (image>0.5)*(arange(h)[:,newaxis]-polyval(p,xs)[newaxis,:])
d = stats.scoreatpercentile(abs(ys[image>0.5]),90)
print d
In [138]:
scale = d/((target_h/2)*dest)
print scale
In [139]:
print b,target_height/2,(target_height/2)/scale
offset = b-scale*target_h/2
print b*scale-offset
In [142]:
output = interpolation.affine_transform(image,array([[scale,scale*m],[0,scale]]),offset=(offset,0),output_shape=(target_h,int(w/scale)))
imshow(output)
Out[142]:
In [ ]: