50

I'm looking for a simple gamma correction formula for grayscale images with values between 0 and 255.

Let's say that the gamma of my screen is 2.2 (it's an LCD screen so I would probably need to estimate it with a more complicated procedure, but let's assume my screen is behaving nicely).

Which one of the following formulas would be the correct one?

  1. Corrected = 255 * (Image/255).^2.2

OR

  1. Corrected = 255 * (Image/255).^(1/2.2)

(Those are destined to be MATLAB codes but I hope they are understandable even to non-MATLAB people)

I've been looking around on the Internet but found both formulas going around. I suspect (2) is the right one, and my confusion is due to the tendency to call "gamma value" the inverse of the actual gamma value, but I would really appreciate some feedback by people who know what they are talking about...

2
  • 2
    you're right to ask. this is all very confusing.
    – v.oddou
    Commented Aug 27, 2014 at 13:07
  • @v.oddou your comment made my day, i also vouch this, this is really confusing !! Commented Oct 1, 2023 at 17:55

2 Answers 2

42

Both formulas are used, one to encode gamma, and one to decode gamma.

Gamma encoding is used to increase the quality of shadow values when an image is stored as integer intensity values, so to do gamma encoding you use the formula:

encoded = ((original / 255) ^ (1 / gamma)) * 255

Gamma decoding is used to restore the original values, so the formula for that is:

original = ((encoded / 255) ^ gamma) * 255

If the monitor does the gamma decoding, you would want to use the first formula to encode the image data.

3
  • 3
    good but CAREFUL. in your first formula original is a SYNTHESISED value from, say, a renderer. (raytracing, raster whatever). and is in linear (physical) space. If original is the pixel of a photo, or a texture, then you DO NOT APPLY ANY FORMULA, because they are already gamma encoded. if you do, you will just do things twice. ANd this is where things get complicated, if you have textures as input of a renderer ? you need to linearize by applying formula 2 first, then do math, then gamma-ify back to storage.
    – v.oddou
    Commented Aug 27, 2014 at 17:04
  • How to know which gammma value is used in image captured by logitech camera?
    – Abc
    Commented Aug 20, 2016 at 6:07
  • 1
    @Abc: Normally the gamma value is 2.2 for an image that is intended to be shown on a regular screen. Typically that is a JPEG image using the sRGB or Adobe RGB color spaces.
    – Guffa
    Commented Aug 23, 2016 at 11:00
36

Gamma correction controls the overall brightness of an image. Images which are not corrected can look either bleached out or too dark. Suppose a computer monitor has 2.2 power function as an intensity to voltage response curve. This just means that if you send a message to the monitor that a certain pixel should have intensity equal to x, it will actually display a pixel which has intensity equal to x2.2 Because the range of voltages sent to the monitor is between 0 and 1, this means that the intensity value displayed will be less than what you wanted it to be. Such a monitor is said to have a gamma of 2.2.

So in your case,

Corrected = 255 * (Image/255)^(1/2.2).
2
  • Sorry, I don't understand. What does Image means in your formula? Is it a value between 0-1 or between 0-255? And what about the result, should it be a number between 0-1 or between 0-255? I was keeping up with your explanation but then when it came to the formula you assumed some knowledge that I don't have. You mentioned x^2.2 in your explanation (that I understand will decrease the value if it is between 0-1), but then you used (1/2.2) in the formula, which is the exact opposite of what you set up for from your explanation. This is probably a lack of basic math on my part, but anyway. Commented Sep 9, 2021 at 21:57
  • Explaining my questions above: I tried a concrete example with Image being 128 (in case it should be a value between 0-255): 255 * (128 / 255) ^ (1/2.2), which results in 186.41. So 128 increased instead of decreasing, which is contrary to your explanation. Then I tried with 0.5 (in case it should be a value between 0-1, and the result is 14.99, which is even weirder. It would be great if you could expand a bit those questions in your answer! Commented Sep 9, 2021 at 22:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.