Skip to content

OpenCV – Gamma Correction

February 23, 2011

Gamma correction (also known as power transformation) is used in image processing in several situations. The classic example where gamma correction is needed is in CRT monitors. In CRTs the amount or intensity of light from the phosphor in the screen is related to voltage as

Output = Voltage crt_gamma

Hence if we give a linear or normal image to the CRT, the image would be look much too dark. In order to mitigate this effect, we “gamma correct” the image such a way that the image output looks well. All we do is something opposite to what the CRT does.

gamma_correc_image = image(1/crt_gamma)

For most CRTs, the crt_gamma is somewhere between 1.0 and 3.0.

Now lets delve into the code. The code given below is just to explicit to understand (I believe).


int main(int argc, char **argv)

{	const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
	IplImage* img = 0;
	int h, w, step, channels, i, j, k;
	uchar *data;
        //read the input image
	img = cvLoadImage(imagename, CV_LOAD_IMAGE_UNCHANGED);
		printf("Could not load image file: %s\n", imagename);
	h = img->height;
	w = img->width;
	step = img->widthStep;
	channels = img->nChannels;
	data = (uchar *)img->imageData;
        //assign the gamma inverse value, here 2
	int gamma = 2;

        //new image with gamma correction
	IplImage* trans = cvCreateImage(cvGetSize(img), 8, channels);
	int trans_step;
	trans_step = trans->widthStep;
        //gamma correction
	cvPow(img, trans, gamma);
	cvConvertScaleAbs(trans, trans, 1, 0);
	cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE);
	cvShowImage("OpenCV", trans);

	return 0;

From → Open Source, OpenCV

  1. Hi, I am trying to implement the gamma transformation. My code is very similar to your code but for some reason is not working. I tried yours also but it is not working as well. Using your code, I tried with gamma=2 and with an image where the intensity increases horizontally from 0 to 255, that is, it is a gray scale image. I expected to obtained a similar image, but I obtain a black and white image. Maybe, you can help to understand which is the problem.

    I will appreciate your help

    • Amarnath permalink


      I believe the code I have given here works well. If you give a greyscale image with intensity varying uniformly from 0 to 255, and give gamma = 2, you will get a non uniformly varying intensity for the output image. I have not got the point that you have put up here. How can you get a black and while image? Maybe if you upload the input and output images somewhere on internet, I may be able to help you.



  2. Andrew permalink

    Hi, I am trying to apply gamma correction to a constant stream of images coming in from my feed. I understand how to apply the correction but I am not sure what value to use. You are using ‘2’ but these images can sometimes have good or bad contrast to start from. Is there a way of ‘reading’ a ‘poor’ image and then applying when the adjustable gamma ‘figure’ ?

    Many Thanks

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: