Home > Tutorials > Image Processing – Gamma Correction On The Fly

Image Processing – Gamma Correction On The Fly


Gamma correction or gamma encoding is one among the common image processing method. I guess, I don’t need to discuss about its meanings and how to calculate it, but how to implement it in Android.

You might want to refer to Wikipedia page on Gamma Correction.

Sample for the original image (still the beauty one)

Original Image

Original Image

What would it be like if I setup the gamma: Red = 0.6, Green = 0.6, Blue = 0.6 ?

Gamma Image: (R, G, B) = (0.6, 0.6, 0.6)

Gamma Image: (R, G, B) = (0.6, 0.6, 0.6)

It looks a bit darker and intensive.

What about gamma of: Red = 1.8, Green = 1.8, Blue = 1.8?

Gamma Image (R, G, B) = (1.8, 1.8, 1.8)

Gamma Image (R, G, B) = (1.8, 1.8, 1.8)

Contrast to the previous settings, it results into a brighter one.

There are various computations on Gamma correction, however, I just pick up and implement the simplest one as mentioned on Wiki page.

	public static Bitmap doGamma(Bitmap src, double red, double green, double blue) {
		// create output image
		Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
		// get image size
		int width = src.getWidth();
		int height = src.getHeight();
		// color information
		int A, R, G, B;
		int pixel;
		// constant value curve
		final int    MAX_SIZE = 256;
		final double MAX_VALUE_DBL = 255.0;
		final int    MAX_VALUE_INT = 255;
		final double REVERSE = 1.0;

		// gamma arrays
		int[] gammaR = new int[MAX_SIZE];
		int[] gammaG = new int[MAX_SIZE];
		int[] gammaB = new int[MAX_SIZE];

		// setting values for every gamma channels
		for(int i = 0; i < MAX_SIZE; ++i) {
			gammaR[i] = (int)Math.min(MAX_VALUE_INT,
					(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / red)) + 0.5));
			gammaG[i] = (int)Math.min(MAX_VALUE_INT,
					(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / green)) + 0.5));
			gammaB[i] = (int)Math.min(MAX_VALUE_INT,
					(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / blue)) + 0.5));
		}

		// apply gamma table
		for(int x = 0; x < width; ++x) {
			for(int y = 0; y < height; ++y) {
				// get pixel color
				pixel = src.getPixel(x, y);
				A = Color.alpha(pixel);
				// look up gamma
				R = gammaR[Color.red(pixel)];
				G = gammaG[Color.green(pixel)];
				B = gammaB[Color.blue(pixel)];
				// set new 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

Advertisements
  1. Jay Tanna
    February 3, 2017 at 7:42 pm

    What should be the work flow for image calibration?
    is it 1.)Gamma correction 2.)scale image 3.)subtract NIR channel from image?
    Looking forward to hearing from you.

  2. Ashay B
    January 31, 2014 at 4:46 pm

    how do you call your doGamma method?

  3. pia
    March 5, 2013 at 2:20 pm

    Hey can u just say how to bring cross process,holga effects in an android application???

  4. Junior
  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: