Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binarize Image in Android

I am developing android app that uses tesseract OCR to scan a text from image,
I heard that binarizing image before performing OCR on it will give better result,
So I start looking for a code that do the operation,

I found few but its actually in java and needs awt library ... so they don't work on android.
So can you help me to find one.
thank you

like image 331
Firas Al Mannaa Avatar asked May 04 '13 14:05

Firas Al Mannaa


1 Answers

I have to do a similar task as part of a project for an asignment. I found in my workspace this piece of code, I think this is what you need:

Bitmap img = BitmapFactory.decodeResource(this.getResources(), drawable.testimage);
Paint paint = new Paint();

ColorMatrix cm = new ColorMatrix();
float a = 77f;
float b = 151f;
float c = 28f;
float t = 120 * -256f;
cm.set(new float[] { a, b, c, 0, t, a, b, c, 0, t, a, b, c, 0, t, 0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(img, 0, 0, paint);

Here I used ColorMatrix to generate a black and white image from a color one. Also I found this piece of code that I used to convert a color image to a gray scale image:

Bitmap result = Bitmap.createBitmap(destWidth, destHeight,Bitmap.Config.RGB_565);
RectF destRect = new RectF(0, 0, destWidth, destHeight);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(filter);
canvas.drawBitmap(bitmap, sourceRect, destRect, paint); 

Hope this help you.

like image 88
Diego Avatar answered Oct 12 '22 20:10

Diego