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]:
<matplotlib.image.AxesImage at 0x151cee10>

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]:
[<matplotlib.lines.Line2D at 0x151deed0>]

In [134]:
p = polyfit(xs,center,1)

In [135]:
plot(center)
plot(polyval(p,xs))
print p
m,b = p


[ -2.73499420e-02   4.91159095e+01]

In [136]:
figsize(18,10)
imshow(image,cmap=cm.gray)
plot(polyval(p,xs))


Out[136]:
[<matplotlib.lines.Line2D at 0x152a5190>]

In [137]:
ys = (image>0.5)*(arange(h)[:,newaxis]-polyval(p,xs)[newaxis,:])
d = stats.scoreatpercentile(abs(ys[image>0.5]),90)
print d


9.32206143538

In [138]:
scale = d/((target_h/2)*dest)
print scale


1.16525767942

In [139]:
print b,target_height/2,(target_height/2)/scale
offset = b-scale*target_h/2
print b*scale-offset


49.1159094839 16 13.7308685302
26.7609040948

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]:
<matplotlib.image.AxesImage at 0x15dbb550>

In [ ]: