Adaptive Threshold Opencv Android Studio Tutorial

I want to create threshold pengayak based Android for my character recognition app without OpenCV /Tesseract, etc, here is my case :

  • I have an image with several characters with background and foreground color of a different
  • Then when executed outputs the resulting image appears in the form of a white background and black foreground image

I’ve made ​​some functions in my program, the first color images are filtered by the algorithm grayscale (it’s easy and it works perfectly), the second step, a grayscale image is filtered with a tapis threshold algorithm, but the results did not come out as expected, as shown below

enter image description here

Supposedly I was expecting the output is as shown below

enter image description here

And here’s my source code Grayscale Function I’ve made :

          public Bitmap DoGrayscale(Bitmap BitmapColor){          int w, h;        h= BitmapColor.getHeight();        w= BitmapColor.getWidth();             Bitmap BitmapGrayscale = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);        int R, G, B;       int pixel;         for(int x = 0; x < w; ++x) {        for(int y = 0; y < h; ++y) {          pixel = BitmapColor.getPixel(x, y);          R =;         G =;         B =;          R = G = B = (int)((R + G + B)/3);          BitmapGrayscale.setPixel(x, y, Color.rgb(R, G, B));        }       }       return BitmapGrayscale;     }

And here’s my Threshold Function source code :

          public Bitmap DoThreshold(Bitmap BitmapGrayscale) {         int w, h, threshold;         h= BitmapGrayscale.getHeight();        w= BitmapGrayscale.getWidth();         threshold = 128;         Bitmap BitmapBiner = Bitmap.createBitmap(BitmapGrayscale);         for(int x = 0; x < w; ++x) {        for(int y = 0; y < h; ++y) {                 int pixel = BitmapGrayscale.getPixel(x, y);                int gray =;                 if(gray < threshold){                    BitmapBiner.setPixel(x, y, 0xFF000000);                } else{                    BitmapBiner.setPixel(x, y, 0xFFFFFFFF);                }             }        }        return BitmapBiner;    }

I’ve been looking for various related tutorials, but none of them segak, would you help resolve this ki kesulitan, please ??? Thanks a lot