32

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["https://i.stack.imgur.com/cLncR.png"], 50];
Map[TextRecognize, digits, {2}]
Niki Estner
  • 36,101
  • 3
  • 92
  • 152
  • 4
    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

1 Answers1

25
(* Get your image *)
digits = ImagePartition[Import["https://i.stack.imgur.com/cLncR.png"], 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" *)
TextRecognize@ImageAssemble@d2
(*
-> "64776958729385431752328231"
*)

Edit

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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • 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
  • 1
    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
  • 2
    @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