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).

```#include<stdio.h>
#include<highgui.h>
#include<cv.h>
#include<math.h>
#include<stdlib.h>

int main(int argc, char **argv)

{	const char* imagename = argc > 1 ? argv : "lena.jpg";
IplImage* img = 0;
int h, w, step, channels, i, j, k;
uchar *data;
if(!img)
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);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&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.

• Hey

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.

Cheers

Amarnath

2. • 