Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Some days ago I saw in this site the algorithm below to convert color images to binary images and I used it in my application (with some small changes). However, I need to know what is the name of the implemented algorithm and, if possible, some references about it.

Thanks in advance!

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 < bm.getWidth(); x++ )
    {
        for ( int y = 0; y < bm.getHeight(); y++ )
        {
            c = pixels[x+y*w];
            lLightness = ((c&0x00FF0000 )>>16) + ((c & 0x0000FF00 )>>8) + (c&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 < bm.getWidth(); x++ )
        for ( int y = 0; y < bm.getHeight(); y++ )
            binaryImage[x][y] = pixels[x+y*w] <= gLightness;

    return binaryImage;
}
share|improve this question

migrated from codereview.stackexchange.com May 18 '13 at 2:59

This question came from our site for peer programmer code reviews.

2  
This question appears to be off-topic because it is about guessing the name of a particular block of code (that might not have a name at all). –  MichaelT Oct 10 '13 at 2:09

1 Answer 1

Appears as pretty naive attempt to convert RGB to luminosity with some heuristic use of average brightness as a threshold.

You can find correct RGB->LUMA formulas along the lines in this discussion: http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.