Binarize Image in Android

后端 未结 7 1014
耶瑟儿~
耶瑟儿~ 2020-12-17 03:11

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

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 04:09

    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.

提交回复
热议问题