Fast image binarization on Android I'm working on an application that turns a raw camera image into a binary (pure black/white) image and I need this to happen as fast as possible for swift further processing. This is what my code currently looks like:

public static boolean[][] createBinaryImage( Bitmap bm )
{
    int[] pixels = new int[bm.getWidth()*bm.getHeight()];
    bm.getPixels( pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight() );
    int w = bm.getWidth();

    // Calculate overall lightness of image
    long gLightness = 0;
    int lLightness;
    int c;
    for ( int x = 0; x &lt; bm.getWidth(); x++ )
    {
        for ( int y = 0; y &lt; bm.getHeight(); y++ )
        {
            c = pixels[x+y*w];
            lLightness = ((c&amp;0x00FF0000 )&gt;&gt;16) + ((c &amp; 0x0000FF00 )&gt;&gt;8) + (c&amp;0x000000FF);
            pixels[x+y*w] = lLightness;
            gLightness += lLightness;
        }
    }
    gLightness /= bm.getWidth() * bm.getHeight();
    gLightness = gLightness * 5 / 6;

    // Extract features
    boolean[][] binaryImage = new boolean[bm.getWidth()][bm.getHeight()];

    for ( int x = 0; x &lt; bm.getWidth(); x++ )
        for ( int y = 0; y &lt; bm.getHeight(); y++ )
            binaryImage[x][y] = pixels[x+y*w] &lt;= gLightness;

    return binaryImage;
}


As you can see, it uses a global threshold to tell apart the black and white pixels. I recently switched from bm.getPixel to bm.getPixels and this yielded a speed improvement of ~33%. Now my question is if there is anything else I can do to speed things up?

It currently takes &lt;0.5 seconds to process 558x256 image and this is reasonable for my application, but I wonder if there's room for any other simple optimizations.
You can get rid of the nested loop, replacing this

for ( int x = 0; x &lt; bm.getWidth(); x++ )
 {
     for ( int y = 0; y &lt; bm.getHeight(); y++ )
     {
         c = pixels[x+y*w];
         lLightness = ((c&amp;0x00FF0000 )&gt;&gt;16) + ((c &amp; 0x0000FF00 )&gt;&gt;8) + (c&amp;0x000000FF);
         pixels[x+y*w] = lLightness;
         gLightness += lLightness;
     }
 }


With this:

int size = bm.getWidth()*bm.getHeight();
for (int i = 0; i &lt; size; i++) {
  c = pixels[i];
 // etc.
}


It will at least save one x+y*w operation per pixel.

It may be advantageous to calculate size into a separate variable as above, instead of doing it inside the loop, as in for (int i = 0; i &lt; bm.getWidth()*bm.getHeight(); i++). The condition is checked at each round so you would end up calling the getters at each round, and who knows what's in them? The JIT compiler may be able to optimize this kind of stuff away - and probably will, if the getters are simple "return class member" statements - but at least it's not guaranteed.

More substantial improvements can be got by sacrificing perfection. Maybe you needn't take each pixel into account for calculating the threshold? Perhaps count only every 10th on each direction? Then you would probably use the nested loop again. Like this:

int width = bm.getWidth();
int height = bm.getHeight();

for ( int x = 0; x &lt; width; x+=10; )
{
  for ( int y = 0; y &lt; height; y+=10; )
  {
   // etc.
  }
}

gLightness /= (width / 10) * (height / 10);