# TextRecognize with crosses, circles and spaces results into oddities, why?

I am investigating this answer here about detecting crosses and circles with TextRecognize. I tried to solve this problem here by directly using the builtin command TextRecognize but it does not work as expected: this example should print xoo x xx x x in ASCII and not xoo x 'xx. Elements are so that empty should return space, x should return x or circle should return o.

Is TextRecognize bad choice for this task having ASCII gameplay characters such as x, o, # and possibly others? What can explain TextRecognize's malfunctioning? You can see that there is no ' but it still prints it and it misses some characters.

-
My answer there will work reasonable well if you restrict yourself to the first incarnation of that question (ie. no blank spaces) or you care to manage the blank spaces before feeding the argument to TextRecognize –  belisarius May 15 '13 at 18:07
@belisarius yes but TextRecognize does not work with only a character so a hackish solution -- my goal is to make the space somehow something special so Mathematica would read it as some specifial character, without needing to create odd code. Perhaps fill all spaces with something very distinct and then use TextRecognize? –  hhh May 15 '13 at 18:09

Here is a trivial text recognizer based on the input samples you've given, it might act as a template for a more customized solution for you.

It may be that you want a more generic solution, in which case you can increase the number of training templates, or abstract a generalized model of the characters that you are interested in.

(* Import example images/prototypes *)
x = Import["http://i.stack.imgur.com/gDzKy.png"];
o = Import["http://i.stack.imgur.com/LMPQq.png"];
empty = Import["http://i.stack.imgur.com/zDqzd.png"];

(* A naive similarity function *)
similarity[c1_Image, {charset_List, vals_List}] :=
With[{scores =
Plus @@ Abs[Flatten@ImageData@c1 - Flatten@ImageData@#] & /@
charset}, Extract[vals, Position[scores, Min@scores]]]

similarity[#, {{x, o, empty}, {"x", "o", " "}}] & /@ {o, x, o, empty, x} // Flatten


{"o", "x", "o", " ", "x"}

-
I tried to do it for the grid but img = Import["http://i.stack.imgur.com/NbTGY.jpg"]; grid = ImagePartition[ ImageCrop[ImageRotate[img, 0.7 \[Degree]], {1180, 720}, {-0.15, 0.2}], {118, 119}]; Table[similarity[i, {{x, o, empty}, {"x", "o", " "}}], {i, grid}] -- traversing not working correctly?! Wait it does not work because this method works only with specific cross and specific circle? –  hhh May 16 '13 at 1:11
There are a number of reasons this doesn't give the result you would like. Firstly have you had a look at the structure of grid, particularly what grid[[1]] might look like. Similarity takes a single image as it's first input, not a list of images. Secondly the second parameter is intended to be a set of prototype images which have exactly the same image dimensions as, and a strong visual resemblance to, the images you wish to recognise and a set of labels to associate with those prototypes. –  image_doctor May 16 '13 at 7:34
This approach is based on measuring the distance between a sample and a prototype or template, its success or failure depends on the closeness of those two things. As a generalised problem, text recognition is a sub class of machine learning ( aka pattern recognition). There are a broad range of more complex techniques that might be applied here, maybe what you are seeking depends on whether you want to learn in more depth how those things work or if you just need a solution to your immediate problem. –  image_doctor May 16 '13 at 9:12