Home > Tutorials > Image Processing – Grayscale Image On The Fly

Image Processing – Grayscale Image On The Fly


As you know, grayscale image is an image that each pixel holds a single sample, only intensity information, also known as black-and-white image.

You might want to refer about Greyscale Image on Wikipedia and how it is implemented in GIMP.

The intensity is calculated by common formula: 30% of Red + 59% of Green + 11 % of Blue

This is my original beauty:

Original Image

Original Image

After processing the grayscale level for image, the beauty has only back-and-white information:

Greyscaled Image

Greyscaled Image

You should have got the idea of grayscale now, shouldn’t you?

Here the simple implementation:

	public static Bitmap doGreyscale(Bitmap src) {
		// constant factors
		final double GS_RED = 0.299;
		final double GS_GREEN = 0.587;
		final double GS_BLUE = 0.114;

		// create output bitmap
		Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
		// pixel information
		int A, R, G, B;
		int pixel;

		// get image size
		int width = src.getWidth();
		int height = src.getHeight();

		// scan through every single pixel
		for(int x = 0; x < width; ++x) {
			for(int y = 0; y < height; ++y) {
				// get one pixel color
				pixel = src.getPixel(x, y);
				// retrieve color of all channels
				A = Color.alpha(pixel);
				R = Color.red(pixel);
				G = Color.green(pixel);
				B = Color.blue(pixel);
				// take conversion up to one single value
				R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
				// set new pixel color to output bitmap
				bmOut.setPixel(x, y, Color.argb(A, R, G, B));
			}
		}

		// return final image
		return bmOut;
	}

Hope you like it!

 

Cheers,
Pete Houston

  1. March 2, 2016 at 12:04 am

    Hello I want to ask, why those code can’t work in android platform? no result after I paste the code in my project (I combined with gallery) but the result is nothing. pls help.

  2. nano
    September 5, 2014 at 10:56 pm

    how to convert an image to grayscale in Java Android???pliss help~

  3. sadi shafait
    December 19, 2013 at 6:10 pm

    Ashu Singh……
    i did not know…where to save image and then any thing need to write in the activity class…
    so plz can u explain me…

  4. anjali
    December 1, 2013 at 5:25 pm

    I am a beginner in android app development…
    The code is showing error at the first line “public static Bitmap doGreyscale(Bitmap src) {”
    Please help me out….

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.os.Bundle;

    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;

    public class next extends Activity {

    ImageView iv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.call_camera);

    iv=(ImageView) findViewById(R.id.imageView);

    Button btn = (Button) findViewById(R.id.takephoto);
    btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i, 0);
    }

    });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
    Bitmap theImage = (Bitmap) data.getExtras().get(“data”);
    iv.setImageBitmap(theImage);

    }

    Button btn1 = (Button) findViewById(R.id.gray_scale);
    btn1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

    public Bitmap dograyscale(Bitmap src) {

    // constant factors
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    // pixel information
    int A, R, G, B;
    int pixel;

    // get image size
    int width = src.getWidth();
    int height = src.getHeight();

    // scan through every single pixel
    for(int x = 0; x < width; ++x) {
    for(int y = 0; y < height; ++y) {
    // get one pixel color
    pixel = src.getPixel(x, y);
    // retrieve color of all channels
    A = Color.alpha(pixel);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);
    // take conversion up to one single value
    R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
    // set new pixel color to output bitmap
    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
    }
    }

    // return final image
    return bmOut;
    }

    }
    });

    }
    }

  5. anjali
    December 1, 2013 at 5:20 pm

    HI,
    showing error in the first line-“public static Bitmap doGreyscale(Bitmap src) {….”
    showing error of multiple markers at this line..can not figure out the mistake…

  6. Luca
    March 8, 2013 at 8:49 pm

    It’s more efficient if you use a ColorMatrix, which works on the WHOLE picture at once, rather than nesting two cycles to do the same work ONE PIXEL at a time…

    see:
    http://stackoverflow.com/questions/8381514/android-converting-color-image-to-grayscale

  7. Samer
    February 13, 2013 at 12:24 am

    How can i contact you ?!

  8. Sanford
    January 9, 2013 at 2:47 pm

    Thank you very much. This is certainly gonna help me in my project. You are now officially my HERO!!!

  9. Ashu Singh
    October 28, 2011 at 12:34 pm

    Hey,
    This post was really helpful for me. I converted my Image in Bitmap and passed it to this method and i got the output (Grayscale image) in one sort only.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: