# Character segmentation in poor quality grayscale

Given grayscale lines from a receipt like this:

,

in which parts of the letters sometimes appear almost as light as the background, my goal is to determine the $x$-positions of the centres of the bounding boxes of the characters, to uncover the monospaced text grid.

The problem is that, due to the bad quality of the scan, a binarization either disconnects pars of letters that belong together, or connect letters that should be considered separate component. Using MorphologicalBinarize[ColorNegate[#],thres] has given best results so far:

,

but this result is unsatisfactory.

My question is: What is a good way to separate the letters, in order to find their centers and recover the horizontal grid. The shape of the characters does not necessarily have to be saved in this step.

More lines like the example can be found at https://www.dropbox.com/s/ghlyxl6l1rop41s/lines.zip?dl=0.

• LocalAdaptiveBinarize gives you more control over the binarization then MorphologicalBinarize – paw Dec 1 '14 at 20:44

Assuming that it is always a monospaced font, sum the image in both axes.

img = "your image";
imgdat = ImageData[img];

horiz = Total[imgdat];
vert = Total[imgdat//Transpose];


The resulting lists can be easily searched for peaks and valleys estimating the centers/edges of bounding boxes.

• Thanks! This is a bit tricky, because at points where a letter is very thin (such as the upper part of 'r'), this curve shows a valley. – Thijs Dec 1 '14 at 20:30
• Do not consider each letter individually but use the information that the monospaced font has a fixed pitch for the entire line of characters. Specifically,'r' in this case is next to "ose" which have very centered distributions as does the numeric portion (excepting "1"). This leads me to consider straightforward autocorrelation or Fourier techniques to identify the pitch from the summed lists. – John Morganthau Dec 1 '14 at 22:43