16

Please take a look at the screenshot below and see if you can tell me why this won't work. The examples in on the reference page for TextRecognize look pretty impressive, I don't think recognizing single letters like this should be a problem. I've tried resizing the letters as well as having the image sharpened.

For convenience in case you want to try this yourself I have included the image that I use at the bottom of this post. You can also find plenty more like this by searching for "Wordfeud" in Google Image Search.

Mathematica screenshot

Wordfeud board

C. E.
  • 10,297
  • 10
  • 53
  • 77
  • Can you make the *question* more explicit in your description? (BTW I tried it once, and I agree, it's not very good. I didn't bother much to get it working. Maybe it's interesting to note it uses [Teserract](http://code.google.com/p/tesseract-ocr/)) – Szabolcs Jan 18 '12 at 22:59
  • 1
    I think Calle wanted to check whether the text recognition function was as limited as he suspected it was. If not, what did he do that was wrong? If so, what is required for a text to be scanned with a high hit-rate? – DavidC Jan 19 '12 at 00:06
  • you might want to start with a high resolution image, `TextRecognize` does not recognize text below certain threshold – Prashant Bhate Jan 19 '12 at 00:20

3 Answers3

14

Very cool question!

TextRecognize uses heuristics to recognize whole words from the English language. This is the gotcha that makes recognizing single letters very hard

Consider the following line of thought:

s = Import["https://i.stack.imgur.com/JHYuh.png"];
p = ImagePartition[s, 32]

Now pick letters to form the English word 'EXIT':

x = {p[[1, 13]], p[[6, 6]], p[[3, 13]], p[[1, 12]]}

Now clean up these images a bit, like so:

d = ImageAssemble[ Map[ImageTake[#, {3, 27}, {2, 20}] &, x ]];

Then this returns the string "EXIT":

TextRecognize[d]

Mathematica graphics

Arnoud Buzing
  • 15,383
  • 3
  • 20
  • 50
  • Nice point about mma looking to recognize actual words (by default,in the English language) rather than individual letters. That's why I looked for an English crossword puzzle; I also wanted a B&W image. It seems that mma is more likely to recognize a string of widely spaced letters as a word when the background does not distract it. – DavidC Jan 18 '12 at 23:59
  • Thank you both for your answers. I should note to anyone visiting this page in the future that the reference page does have a working example where letters are randomly sorted. So this not working is a combination of the top two answers. ImageCorrelate, suggested in a third answer, produces solid results. – C. E. Jan 24 '12 at 21:38
  • 1
    A feature suggestion: it would be useful to have an option to disable spelling correction and another option to constrain the set of characters to be recognized (which I believe Tesseract, the back end of `TextRecognize`, can already do). This would make [reading numerical data](http://mathematica.stackexchange.com/q/18683/12) much easier. – Szabolcs Jan 29 '13 at 17:45
12

This is an approach completely different from using TextRecognize, so I am posting this as a separate answer. It uses the same image recognition technique from the How do I find Waldo with Mathematica.

First get the puzzle:

wordfeud = Import["https://i.stack.imgur.com/JHYuh.png"]

Mathematica graphics

And then get the pieces of the puzzle:

Grid[pieces = ImagePartition[s, 32]]

Mathematica graphics

Let's be interested in the letter E:

LetterE = pieces[[4, 3]]

Mathematica graphics

Get the correlation image:

correlation = 
 ImageCorrelate[wordfeud, Binarize[LetterE], 
 NormalizedSquaredEuclideanDistance]

Mathematica graphics

And highlight the matches:

positions = Dilation[ColorNegate[Binarize[correlation, .1]], DiskMatrix[20]];
found = ImageMultiply[wordfeud, ImageAdd[ColorConvert[positions, "GrayLevel"], .5]]

Mathematica graphics

As before, this requires a bit of tuning on binarizing the correlation image, but other than that this should help to identify bits and pieces of this puzzle.

Community
  • 1
  • 1
Arnoud Buzing
  • 15,383
  • 3
  • 20
  • 50
6

I thought the quality of your image might be interfering. Binarizing your image did not help : recognition was zilch. I also tried a very sharp black and white image of a crossword puzzle solution. (see below) Again, nothing was recognized whether in regular or binarized format.

crossword solution

So I removed the black background leaving only the letters and their thin black frames. Again, recognition was about 0%.

When I removed the frames from around some of the letters AND binarized the image the only parts that were recognizable were those regions in which there was nothing but letters. (see below)

crossword 2

Notice in the output below, ANTS, TIRES, and TEXAS are correctly identified (as well as VECTORS), but just about nothing else.

Notice also that, even though the strings were widely spaced, mma interpreted them as words, rather than separate letters. Note "TEXAS" instead of "T E X A S".

TextRecognize[Binarize@img]

(* output *)
ANTS FFWWW FEEWF
E R o If IU I?
E A FI5F WWWFF 5
5552? L E F F
T s E NTT BT|
H0RWW@0WVlWF;EE F
5 W E   ; OCS
FOFT W W R AL%AE
A TT I T ? _
i iE@W'NF WG%S W
A A EW F I i
SWWTW W ALTFCWD N
H A V 5 A F F
PLATT EWWLIGHT
W N E T
HE TIRES C
TEXAS VECTORS

I didn't have the patience to completely clean up the image. It would have been much faster to retype the text by hand.

Conclusion: Don't use text recognition in mma unless you have absolutely clear text against an even-colored, bright, preferrably white, background.

The results also varied depending on the file format used. Avoid .pdf altogether.



Edit

acl captured and tried to recognize the last 5 lines (above Edit). His results (in a comment below): mostly gibberish.

I decided to do the same. But since Prashant warned that text size makes a difference, I zoomed in first so that the text appear (to my eyes) to be about 20 pica. Below is the picture of the text I scanned and TextRecognized.


text2


Here's the result of an unbinarized TextRecognize (at that large size):

Gliii. Q lk-ii`t`*¥ if EY £\[CloseCurlyDoubleQuote]1\[Euro]'EE \
Di'¥C~E\"P ITF SKI' T»f}!E'!',IL:?E\[CloseCurlyDoubleQuote] I 2 VEEE5\
\[CloseCurlyQuote] LEP \"- \"VE
1. ur e=\\..r.1.»».»\\\\ rw r 1»»\\|a'*r | r .fm -»'-an \
\[OpenCurlyQuote] -.-rr -_.»~|-.'i~-.w~,.-- nv n.w~»-\
\[OpenCurlyDoubleQuote]~"

Now, here's the result for the TextRecognize of the binarized image. The original image was a .png from Jing.

I didn't have the patience to completely clean up the image. It would \
have been much faster to retype the
text by hand.
Conclusion: Don't use text recognition in mma unless you have \
absolutely clear text against an even-
colored, bright, preferrably white, background.
The results also varied depending on the file format used. Avoid .pdf \
altogether. 
DavidC
  • 3,056
  • 1
  • 20
  • 30
  • So, I took a screenshot (png) of the last 5 lines of your post and tried to get mma to recognize it. It said: ""|men\[OpenCurlyQuote]\\ have the pamence \\e cump\\e\\e\\y dean up \ the wage. nweme have been much |ae\\er\\e refype the \\e><\\ ny mane. cenememn: Dem use \\e><\\ reeegnmen m mme umess yeu have \ ahsu\\u\\e\\y e\\ear\\e><\\ agamm an even- cmured, nngm, prelerramy wrme, naexgreune. The resmls mee vanee eepenemg en the me lurmat ueee. Avme .pdf \ auegemer."" – acl Jan 19 '12 at 00:18
  • (+1 for the effort by the way) – acl Jan 19 '12 at 00:19
  • @acl Try `Binarize`ing the image before trying to recognize the text and see if that helps. – DavidC Jan 19 '12 at 12:20
  • It does not seem to make any difference if I cut at the same size I tried yesterday. Perhaps it has to do with the size of the text. – acl Jan 19 '12 at 12:58
  • @acl What file format are you using? When I use .png, even at a large fontsize, I need to `Binarize` the result even though the text appears to be black. – DavidC Jan 19 '12 at 15:34
  • png. It probably has to do with the magnification. I'll play with it when I am done with some code I am writing (I am typing this while waiting for code to finish running at the moment). – acl Jan 19 '12 at 15:36