Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While answering this question, I tried TextRecognize to read single digits. But it doesn't recognize a single digit, even though the digits are clearly readable.

For example, this enter image description here is not recognized.

This is my code:

digits = ImagePartition[Import[""], 50];
Map[TextRecognize, digits, {2}]
share|improve this question
If we could do this: Map[TextRecognize[#,Range[0,9]]&, digits, {2}] it would be more likely to work. But we can't. I have asked Wolfram developers for this, maybe you should too. – Gustavo Delfino Apr 19 '12 at 11:50
up vote 25 down vote accepted
(* Get your image *)
digits = ImagePartition[Import[""], 50];

(* Remove Borders and Binarize *)
d1 = Binarize[ImageCrop[#, 37], .5] & /@ Flatten@digits;

(* Keep only images with content (blanks affect the OCR) *)
d2 = Select[d1, Min@ImageData@# == 0 &];

(* Assemble as a line and "read" *)
-> "64776958729385431752328231"


The following will reassemble your original image (but cleaner)

tr    = TextRecognize@ImageAssemble@d2;
posd2 = Flatten@Position[d1, x_ /; Min@ImageData@x == 0, {1}];
tp2   = Thread@List[posd2, Characters@tr];
(d1[[#[[1]]]] = Graphics[Text[Style[#[[2]], Large]], ImageSize -> 37]) & /@ tp2;
ia    = ImageAssemble@Partition[ImageCrop[#, 39] & /@ d1, 9]; 
ia1   = Map[ImageCrop[#, ImageDimensions@ia/3 + 3] &, 
            ImagePartition[ia, ImageDimensions@ia/{3, 3}], {2}];

GraphicsRow[{ImageAssemble@ia1, ImageAssemble@digits}]

enter image description here

share|improve this answer
If I try TextRecognize@d2[[1]], it doesn't work. Neither works for me TextRecognize@ImageAssemble[d2[[1 ;; 2]]], but TextRecognize@ImageAssemble[d2[[1 ;; 3]]] does. Do you know any minimun requirements for TextRecognize? – FJRA Apr 19 '12 at 14:02
@FJRA Nope, it's a trial and error thing. I found that you need several chars in a row, with little blank space in between for the thing to work in a reliable way – Dr. belisarius Apr 19 '12 at 14:06
Yes, doing some test, I would have expected something like this to work: TextRecognize[Rasterize[5]]. And it seems that image size matters a lot: TextRecognize[Rasterize[50868, ImageSize -> 300]] vs TextRecognize[Rasterize[50868]]. Something else to take care when using TextRecognize. – FJRA Apr 19 '12 at 14:15
@FJRA TextRecognize[] is not a full blown OCR implemententation, but a nice testbed for exploring some ideas or solving simple image conversion problems. I'd not use it in a "real" problem environment. – Dr. belisarius Apr 19 '12 at 14:32
but very nice to play with it! – FJRA Apr 19 '12 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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