zengxiange / tesseract-android-tools

Automatically exported from code.google.com/p/tesseract-android-tools
0 stars 0 forks source link

Binarize.otsuAdaptiveThreshold gives bad results #8

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here is my code sample. If I remove the Binarize operation, it works well, 
shows a grayscale image with black text and a lighter background that looks 
sharp and in focus. b is an ARGB_8888 bitmap taken from the camera in my tests 
(with inSampleSize of 2 to fit in Memory).

Pix pix = ReadFile.readBitmap(b);
pix = Convert.convertTo8(pix);
pix = Binarize.otsuAdaptiveThreshold(pix);
Bitmap bmp = WriteFile.writeBitmap(pix);
ImageView imageView = (ImageView)findViewById(R.id.image);
imageView.setImageBitmap(bmp);

When I run it, i get an output in B&W like expected, but all the characters of 
the text that was in the photo are converted to a white blocky form with a 
black background that looks like a really badly scaled low-resolution copy of 
the image. I tried to change the parameters (Global, small tiles, big tiles, no 
fraction etc...) but no luck (gets better and worse results depending on the 
parameters, but nothing close to an acceptable result). Tried to use the 
Sauvola Method after adding it in the JNI, same result. I tried to UnsharpMask 
and NormBackground before trying the binarization, do not help that much either.

Original issue reported on code.google.com by eric.bou...@gmail.com on 15 Jun 2011 at 12:46

GoogleCodeExporter commented 9 years ago
What version of the product are you using? On what operating system?

tessreact-for-android-1.00 (Lastest version from the svn as of 2011-06-14)
used ubuntu32 11.04 in a VirtualBox to build so files
source versions as in readme file
java application compiled using eclipse on Windows 7 64

What steps will reproduce the problem?
See previous Post

What is the expected output? What do you see instead?
I expect a B&W image with the text (foreground) in black and the background in 
white.
I get text in white with a black background, with kind of blocky pixels that is 
non-usable and barely readable.

Original comment by eric.bou...@gmail.com on 15 Jun 2011 at 1:02

GoogleCodeExporter commented 9 years ago
I managed to fix the problem. It was when converting the 1bpp PIX to the 32 bit 
Android Bitmap.

The problem lies at line 194 of Writefile.cpp.

This line : 
dstx[0] = dstx[1] = dstx[2] = (srcx[0] & (dw % 8)) ? 0xFF : 0x00;
needs to be replaced by: 
dstx[0] = dstx[1] = dstx[2] = (1 << (7 - (dw & 7)) & srcx[0]) ? 0x00 : 0xFF;

The basic problem was that the bitwise AND was comparing srcx[0], an 8 bit 
integer (0-255), with the result of the modulo which is only 3 bits (0-7). 
Needs to compare with decreasing powers of 2 (like 2^(7-0)), so 128, 64, 32 
etc...

Also I inverted the results, to get a black foreground with a white background 
(? 0x00 : 0xFF).

Original comment by eric.bou...@gmail.com on 17 Jun 2011 at 8:36

GoogleCodeExporter commented 9 years ago
convertTo8 has similiar problem, do you know how to fix it? The following codes 
can not get acceptable result.

Pix p = Convert.convertTo8(ReadFile.readBitmap(bitmap));
baseApi.setImage(p); 

Original comment by xwinstu...@gmail.com on 13 Jan 2012 at 3:36

GoogleCodeExporter commented 9 years ago
bitmap is from Camera preview frame data processed by:

  public Bitmap renderCroppedGreyscaleBitmap() {
    int width = getWidth();
    int height = getHeight();
    int[] pixels = new int[width * height];
    byte[] yuv = yuvData;
    int inputOffset = top * dataWidth + left;

    for (int y = 0; y < height; y++) {
      int outputOffset = y * width;
      for (int x = 0; x < width; x++) {
        int grey = yuv[inputOffset + x] & 0xff;
        pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
      }
      inputOffset += dataWidth;
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }

If I use a bitmap from file, convertTo8 and setImage work fine.

Original comment by xwinstu...@gmail.com on 13 Jan 2012 at 4:01

GoogleCodeExporter commented 9 years ago
I am searching OCR for Credit Cards and bank Cheques.I found OCR tool for 
Cheques i.e, Clear Check 21 MICR but for the Credit cards  I didn't get any OCR 
tool.Is there any OCR library to support to develop Application for reading 
cheques and cards.If anyone know about this please give me some 
suggestions.............  

Original comment by manem.su...@gmail.com on 20 Feb 2012 at 12:08

GoogleCodeExporter commented 9 years ago
hi eric

i have developed OCR application using android tesseract tools. there is a 
binarization using otsu and now im trying to change the binarization method 
using sauvola.

where should i make change?

here is the source of android tesseract that i am using as libarary for the 
application
https://github.com/rmtheis/tess-two

best regards

Original comment by mayco.va...@gmail.com on 21 Feb 2012 at 11:53

GoogleCodeExporter commented 9 years ago
Thanks Eric. Merged your fix in.

Original comment by alanv@google.com on 11 Sep 2012 at 8:24