## 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*)

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

It looks a bit darker and intensive.

What about gamma of: **Red = 1.8, Green = 1.8, Blue = 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

Categories: Tutorials
correction, gamma, image, processing, tutorials

Application hang because of large loop of pixels..how can i deal with it?..

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.

how do you call your doGamma method?

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

check this out http://stackoverflow.com/questions/6254840/circular-fisheye-distortion-rotated-270-deg-clocwise-or-90-deg-anticlockwise-befo/6447401#6447401 Its the fisheye effect for android using java