29
$\begingroup$

I have an image contain only numbers, and TextRecognize fail to recognize some numbers:

img=enter image description here;

TextRecognize[img]
(*826718*)

The documentation says that "The quality of recognized text may improve by enlarging the image", but no luck on this example

TextRecognize[ImageResize[img, Scaled[2]]]
(*826718*)

also tried different language, also no help

TextRecognize[ImageResize[img, Scaled[2]], Language -> "French"]
(*826718*)

I also tried Walfram|Alpha, it also gave the same results as Mathematica:

enter image description here

Are there some ways to solve the problem?

$\endgroup$
1
  • 2
    $\begingroup$ After answering, I noticed this is a possible duplicate of this post which has some useful comments attached. $\endgroup$ Commented Jun 15, 2013 at 2:00

3 Answers 3

24
$\begingroup$

Update

As of Version 12.1, TextRecognize@Import["https://i.sstatic.net/cPRrY.png"] works without the need for additional manipulations of the image or use of undocumented features.

previous answer

TextRecognize seems to be a work in progress, consider the following

Rasterize[Graphics[Text[Style["3", 100]]]] // TextRecognize
Rasterize[Graphics[Text[Style["a", 100]]]] // TextRecognize
Rasterize[Graphics[Text[Style["123", 100]]]] // TextRecognize
Rasterize[Graphics[Text[Style["1234", 100]]]] // TextRecognize
Rasterize[Graphics[Text[Style["hello", 100]]]] // TextRecognize
Rasterize[Graphics[Text[Style["hello 3", 100]]]] // TextRecognize

yields the following output

{nothing here}
{nothing here}
{nothing here}
1234
hello
hello 3

For reasons that are entirely unclear, single characters are not recognized as text, nor are numbers small "arrays" of numbers. Oddly enough, small numbers are recognized if preceeded with an actual word, making the following a terrible solution that nonetheless gives you the answer:

n = Import["https://i.sstatic.net/cPRrY.png"];
pretext = Rasterize["hello ", RasterSize -> 175, ImageSize -> 40];
Row[{pretext, ImageResize[n, 1000]}] // Rasterize;
t = TextRecognize@ImageResize[%, Scaled[5]];
StringSplit@t

gives the output

{hello,3482671897}

Let's hope someone comes up with a better answer...

$\endgroup$
2
  • 1
    $\begingroup$ I like the last part. hello $\endgroup$
    – s0rce
    Commented Jun 15, 2013 at 2:07
  • 6
    $\begingroup$ +1 "work in progress" used to mean "it's currently being worked on"; I suspect it means the opposite in this case... :) $\endgroup$
    – cormullion
    Commented Jun 15, 2013 at 6:22
27
$\begingroup$

TextRecognize[] accepts an undocumented Option "SegmentationMode". The allowed values are:

?Image`ExternalOCRDump`$TextRecognizeSegmentationModes

{
 {{3, "Fully automatic page segmentation, but no OSD. (Default)"}},
 {{4, "Assume a single column of text of variable sizes"}},
 {{6, "Assume a single uniform block of text"}},
 {{7, "Treat the image as a single text line"}},
 {{8, "Treat the image as a single word"}},
 {{10, "Treat the image as a single character"}}
}

Of course in this case we want to use mode 7:

im = Import@"https://i.sstatic.net/cPRrY.png"
TextRecognize[im, "SegmentationMode" -> 7]

Mathematica graphics

(* "3482671897" *)

And that's it.



Now, if you don't want to use undocumented options, you've to know that TextRecognize[] works much better if you first adjust the spacing between characters:

im = Import@"https://i.sstatic.net/cPRrY.png"  

Mathematica graphics

a = ConstantArray[1 , Last@ImageDimensions@im];
newImage = Image@Transpose[Transpose[ImageData@im] //. 
                           {x__, Longest[a..], y__} :> {x, a, a, y}]  

Mathematica graphics

TextRecognize@newImage

(* "3482671897" *)

Note that we are replacing the variable length vertical white strips with a minimum of 3 pixels wide with a standard separator of two pixels, as you can see here:

Image@Transpose[Transpose[ ImageData@im] //. 
                {x__, b : Longest[a..], y__} :> {x, 0 b, y}]  

Mathematica graphics

$\endgroup$
1
  • 5
    $\begingroup$ It would be nice to specify in which versions did this undocumented options work. AFAICT not in v11.1. $\endgroup$
    – rhermans
    Commented Jun 23, 2017 at 15:51
3
$\begingroup$

Mathematica version 11.1

This still fails sometimes but it's better than nothing

AlphaNumTextRecognize[img_Image] := StringJoin[
  Map[
   TextRecognize[#, RecognitionPrior -> "Character"] &,
   TextRecognize[ImageResize[img, 4 ImageDimensions[img]], 
    "Character", "Image"]
   ]
  ]
$\endgroup$

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.