3
votes

I have made a lot of research and have not found a suitable solution for this. I am programming an Android OCR app. I have already successfully loaded Tesseract and Leptonica and I am successfully taking and processing images as well as converting them to text using OCR. However, the recognition accuracy is not so good.

After much tweaking we found out that we were not filtering, cleaning and/or deskewing the image enough to aid Tesseract in the OCR processing. Therefore, I looked on the internet for any library or code that would work for me and use it on Android to no avail.

Does anyone know of a library or can provide me some code to aid me on accomplishing this? All I want is to take a bitmap, convert it to black and white, deskew and/or execute some filtering tasks and give it to Tesseract for it to convert it to text using OCR.

2
Try looking here first.Robert Harvey

2 Answers

3
votes

ImageMagick can do that. Commandline:

convert                              \
    input.{png,pdf,tif,jpeg,gif,...} \
   -colorspace grayscale             \
   -threshold 50%                    \
   -deskew                           \
    output.{png,pdf,tif,jpeg,gif,...}

-colorspace grayscale : helps to also process colored input.
-threshold 50% : play with the percentage value -- but basically it converts to black + white only.
-deskews : deskews

However, I'm not sure how easy or difficult it is to build ImageMagick for the Android platform. It's available for Linux, Mac OS X, Windows, Solaris, HP-UX,... so: it multi-platform by design anyway.

1
votes

I used the below approach to convert my image to B&W and that helped me to increase my accuracy very much.

    private Bitmap GetBinaryBitmap(Bitmap bitmap_src) {
    Bitmap bitmap_new = bitmap_src.copy(bitmap_src.getConfig(), true);

    for (int x = 0; x < bitmap_new.getWidth(); x++) {
        for (int y = 0; y < bitmap_new.getHeight(); y++) {
            int color = bitmap_new.getPixel(x, y);
            color = GetNewColor(color);
            bitmap_new.setPixel(x, y, color);
        }
    }

    return bitmap_new;
}


private double GetColorDistance(int c1, int c2) {
    int db = Color.blue(c1) - Color.blue(c2);
    int dg = Color.green(c1) - Color.green(c2);
    int dr = Color.red(c1) - Color.red(c2);

    double d = Math.sqrt(Math.pow(db, 2) + Math.pow(dg, 2) + Math.pow(dr, 2));
    return d;
}

private int GetNewColor(int c) {
    double dwhite = GetColorDistance(c, Color.WHITE);
    double dblack = GetColorDistance(c, Color.BLACK);

    if (dwhite <= dblack) {
        return Color.WHITE;

    } else {
        return Color.BLACK;
    }
}

Hope it helps.