Search code examples
androidbitmapbinarygrayscale

Android: Convert Grayscale to Binary Image


i hava done with get grayscale value, but i don't know how to use function to convert the grayscale to be binary image. Please help me, here my function code:

public Bitmap toBinary(Bitmap bmpOriginal) {
    int width, height, threshold;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    threshold = 127;
    final Bitmap bmpBinary = null;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get one pixel color
            int pixel = bmpOriginal.getPixel(x, y);

            //get grayscale value
            int gray = (int)(pixel & 0xFF);

            //get binary value
            if(gray < threshold){
                bmpBinary.setPixel(x, y, 0);
            } else{
                bmpBinary.setPixel(x, y, 255);
            }

        }
    }
    return bmpBinary;
}

here my full code:

public class MainActivity extends Activity {

    ImageView img;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //convert imageview to bitmap
        img =(ImageView) findViewById(R.id.imageView1);
        BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
        final Bitmap imgbitmap = drawable.getBitmap();


        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //convert bitmap to grayscale 
                Bitmap imgnew;
                imgnew = toGrayscale(imgbitmap);    
                //convert to binary
imgnew = toBinary(imgnew);

                //convert bitmap to imageview 
                ImageView imgbit;
                imgbit = (ImageView) findViewById(R.id.imageView2);
                imgbit.setImageBitmap(imgnew);
            }
        });

    }

    public Bitmap toGrayscale(Bitmap bmpOriginal){        
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }


public Bitmap toBinary(Bitmap bmpOriginal) {
    int width, height, threshold;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    threshold = 127;
    final Bitmap bmpBinary = null;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get one pixel color
            int pixel = bmpOriginal.getPixel(x, y);

            //get grayscale value
            int gray = (int)(pixel & 0xFF);

            //get binary value
            if(gray < threshold){
                bmpBinary.setPixel(x, y, 0);
            } else{
                bmpBinary.setPixel(x, y, 255);
            }

        }
    }
    return bmpBinary;
}

}

Solution

  • First, you get a NullReferenceException because bmpBinary is NULL.
    Second, to get one Color chanel you can use int red = Color.red(pixel);
    Third, to set a pixel white use bmpBinary.setPixel(x, y, 0xFFFFFFFF);

    I modified your code a bit:

    public Bitmap toBinary(Bitmap bmpOriginal) {
        int width, height, threshold;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();
        threshold = 127;
        Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal);
    
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                int pixel = bmpOriginal.getPixel(x, y);
                int red = Color.red(pixel);
    
                //get binary value
                if(red < threshold){
                    bmpBinary.setPixel(x, y, 0xFF000000);
                } else{
                    bmpBinary.setPixel(x, y, 0xFFFFFFFF);
                }
    
            }
        }
        return bmpBinary;
    }
    

    An even better way is not to use just the value of one color chanel but a weighted average of red green and blue for example:

    int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);