title: How to make adaptive threshold for character recognition in Android without OpenCV
body: <p>I want to create threshold filter based Android for my character recognition app without OpenCV /Tesseract, etc, here is my case : </p>
  
  <ul>
  <li>I have an image with several characters with background and foreground color of a different</li>
  <li>Then when executed outputs the resulting image appears in the form of a white background and black foreground image</li>
  </ul>
  
  <p>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 filter threshold algorithm, but the results did not come out as expected, as shown below</p>
  
  <hr>
  
  <h2><img src="https://i.stack.imgur.com/6eJ2N.jpg" alt="enter image description here"></h2>
  
  <p>Supposedly I was expecting the output is as shown below</p>
  
  <hr>
  
  <p><img src="https://i.stack.imgur.com/EWuCS.jpg" alt="enter image description here"></p>
  
  <p>And here's my source code Grayscale Function I've made : </p>
  
  <pre><code>      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 &lt; w; ++x) {
         for(int y = 0; y &lt; h; ++y) {
  
          pixel = BitmapColor.getPixel(x, y);
  
          R = Color.red(pixel);
          G = Color.green(pixel);
          B = Color.blue(pixel);
  
          R = G = B = (int)((R + G + B)/3);
  
          BitmapGrayscale.setPixel(x, y, Color.rgb(R, G, B));
         }
        }
        return BitmapGrayscale;
  
     }
  </code></pre>
  
  <p>And here's my Threshold Function source code : </p>
  
  <pre><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 &lt; w; ++x) {
         for(int y = 0; y &lt; h; ++y) {
  
                 int pixel = BitmapGrayscale.getPixel(x, y);
                 int gray = Color.red(pixel);
  
                 if(gray &lt; threshold){
                     BitmapBiner.setPixel(x, y, 0xFF000000);
                 } else{
                     BitmapBiner.setPixel(x, y, 0xFFFFFFFF);
                 }
  
             }
         }
         return BitmapBiner;
     }
  </code></pre>
  
  <p>I've been looking for various related tutorials, but none of them fit, would you help resolve this problem, please ??? Thanks a lot</p>