Given grayscale lines from a receipt like this:

Line from a receipt,

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:

MorphologicalBinarize at different threshold levels,

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.

  • $\begingroup$ LocalAdaptiveBinarize gives you more control over the binarization then MorphologicalBinarize $\endgroup$
    – 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.

enter image description here

  • $\begingroup$ 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. $\endgroup$
    – Thijs
    Dec 1 '14 at 20:30
  • $\begingroup$ 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. $\endgroup$ Dec 1 '14 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.