32
$\begingroup$

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.sstatic.net/cLncR.png"], 50];
Map[TextRecognize, digits, {2}]
$\endgroup$
1
  • 4
    $\begingroup$ 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. $\endgroup$ Commented Apr 19, 2012 at 11:50

1 Answer 1

25
$\begingroup$
(* Get your image *)
digits = ImagePartition[Import["https://i.sstatic.net/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

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – FJRA
    Commented Apr 19, 2012 at 14:02
  • $\begingroup$ @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 $\endgroup$ Commented Apr 19, 2012 at 14:06
  • 1
    $\begingroup$ 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. $\endgroup$
    – FJRA
    Commented Apr 19, 2012 at 14:15
  • 2
    $\begingroup$ @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. $\endgroup$ Commented Apr 19, 2012 at 14:32
  • $\begingroup$ but very nice to play with it! $\endgroup$
    – FJRA
    Commented Apr 19, 2012 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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