2

I want to recognize numbers in the following image

enter image description here

I am currently using Tess4J library in eclipse java project but it only recognizes the characters in a plane color background. For this image it could not even identify that there are characters(numbers) on this image. Help me find a way to accomplish this task.

Here is my current code:

import net.sourceforge.tess4j.*; 
import java.io.File; 

public class Main { 
  public static void main(String[] args) { 
    File imageFile = new File("image.png"); 
    Tesseract instance = Tesseract.getInstance(); 
    try { 
      String result = instance.doOCR(imageFile); 
      System.out.println(result); 
    } catch (TesseractException e) {
      System.err.println(e.getMessage()); 
    } 
  } 
}

and if there is way to count the squares separated by yellow lines.

Thank you

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Sathirar
  • 331
  • 4
  • 17
  • You should post some sample code showing how you are using the Tess4J library. – RustyTheBoyRobot Feb 12 '13 at 15:26
  • It is nothing much. Importing the .jar files and .dll files in to project and creating `Tesseract` instance; `import net.sourceforge.tess4j.*; import java.io.File; public class Main { public static void main(String[] args) { File imageFile = new File("image.png"); Tesseract instance = Tesseract.getInstance(); try { String result = instance.doOCR(imageFile); System.out.println(result); } catch (TesseractException e) { System.err.println(e.getMessage()); } } }` – Sathirar Feb 12 '13 at 15:28
  • 1
    It could just be that the library that you are using isn't very powerful (I don't know anything about image processing). – RustyTheBoyRobot Feb 12 '13 at 15:29
  • Yes it is. Mee too need help here. Thanks. – Sathirar Feb 12 '13 at 15:31
  • 4
    I think you have to increase the contrast in the images so that pure white color remains white and other colors are darkened before feeding it to the OCR. – Extreme Coders Feb 12 '13 at 15:35
  • How to accomplish it in java? – Sathirar Feb 12 '13 at 15:36
  • @ExtremeCoders : like this ?http://stackoverflow.com/questions/3433275/adjust-brightness-and-contrast-of-bufferedimage-in-java – Sathirar Feb 12 '13 at 15:40
  • @Sathirar You may follow that and try with different combinations of `RescaleOp` parameters. – Extreme Coders Feb 12 '13 at 16:13
  • @Sathirar is your font fixed and always like in the example ? If yes, there is no need to rely on tesseract for this task. Some simple descriptors can distinguish between the digits. – mmgp Feb 12 '13 at 17:40

1 Answers1

8

If your image is representative, then all you need as a first step is a binarization at a threshold close to the maximum value followed by discarding of small components.

f = Import["https://i.stack.imgur.com/6AXwH.jpg"]
step1 = SelectComponents[Binarize[ColorConvert[f, "Grayscale"], 0.9], 
  "Count", #1 > 100 &]

enter image description here

Now, if you know that the digits cannot be too tall or too thin (this is dependent on image dimensions), then you can filter the remaining components based on its bounding box.

SelectComponents[step1, "BoundingBox", 
 And[10 < #[[2, 1]] - #[[1, 1]] < 100, 50 < #[[2, 2]] - #[[1, 2]] < 100] &]

enter image description here

To separate each of the regions, you could consider using a colorspace where there is a channel dedicated to the yellow color. CMYK is a possibility here, and again all you need is a threshold at a high value, together with the basic morphological closing to complete the lines (since in your example the lines do not extend to the border of the image). Instead of using morphological closings here, you could detect the lines using Hough or RANSAC, for example.

rects = Closing[
  Closing[Binarize[ColorSeparate[f, "CMYK"][[3]], 0.9], 
   ConstantArray[1, {1, 15}]], ConstantArray[1, {15, 1}]] (* left image *)
Colorize[MorphologicalComponents[ColorNegate[rects]], 
 ColorFunction -> "Rainbow"]                              (* right image *)

enter image description here enter image description here

The tools used here are very simple, and almost any image processing library will provide them. There are also more robust approaches that could be taken, but for the given image it is not needed.

mmgp
  • 18,901
  • 3
  • 53
  • 80