Working with Gamma

From VFXPedia

Revision as of 19:30, 23 July 2010 by Gringo (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Gamma Bears or Tale of Stolen Irises

This story is written for artists (further reading is restricted for physicians :) ) to easily clarify some cases when we really should care about image gamma.

Here you'll find short simplified explanation of how does gamma work in practice. And no superfluous theory.


Unlike traditional artists, CG-artist should know that gamma value is the exponent of the power function.

So when you define gamma, value of each pixel's color is raised to the power of gamma.

For example, if gamma=2 then 0^2=0, 0.5^2=0.25, 1^2=1, 2^2=4

0 and 1 are never changed by gamma-correction, midtones get dimmer with gamma increasing and values above 1 get brighter.

Image:GammasGraph.gif


Why gamma? Where does it come from?

Once upon a time, when LCD monitors didn't exist and mice were futuristic devices with a ball inside, people of the world suddenly discovered that brightness CRT-display produce isn't equal to electrons quantity sent to it's luminophor.

So when they tried to see a picture through it, this picture looked darker and it was darkened not linearly. Then sRGB standard was established for digital pictures. It compensated picture darkening using gamma. So, monitors darken picture with gamma about 2.2 and sRGB pictures are lightened with gamma 0.45(45) that is 1/2.2 to look linear on CRT displays.

Image:GammaChain.gif

User-friendly applications like Fusion use inverted gamma (gamma-correction) instead of gamma, so, when you want to make midtones lighter, you can intuitively increase the Gamma parameter and get result you expect.

Consequentially, if you set Gamma=2 in Fusion, you get not power of two, but square root.

In general, InvertedGamma=1/Gamma, So when you set the Gamma parameter in Brightness/Contrast to 3, you actually, get the qubic root of your color and when you set the Gamma=0.333(3) that is 1/3, you actually raise your color to the power of 3.

Composition with 2 gammas: BC and CT in comparison

If you are interested by values below 0 (superblacks, negative colors, blackholes) in most cases gamma isn't defined below zero, but honest and POWERful Fusion applies reflected parabola to that range

Image:GammaInFusion.gif

So -1^2=-1, -1^3=-1, -0.5^2=-0.25, -0.5^3=-0.125

It's not perfectly-correct in sense of traditional mathematics, but this math we can use for correct image processing.


Gamma 2.2 in Fusion is near to quadratic parabola that we remember from school, fallen on it's side. Graph


-So where is the promised practice? Is it another lesson of mathematics from middle school?!

-Well, now we can continue with practice, 'cause you've got the power!


You might hear that gamma of monitors is almost equal to inverted gamma of our visual perception, so they compensate each other an all is OK.

Here is some mess.

To be honest, human's retina has really non-linear response. We see more details in shadows and less details in highlights. It is great for us because we see the most we can on the ground, between trees and in bushes when we hunt or gather mushrooms and apples and in the same time, the sky that is actually much brighter, doesn't blind us and don't harm our eyes.

So, in our perception, the sky is a little brighter than bushes and trees while it is much brighter actually.

Many of contemporary humans and most of ones, who read this article so far, notice that you can easily see different shades on buildings, trees and ground in the street as well as different shades of clouds, bright led-displays, lamps and so on... But when we see all that things reflected in a car surface, in glass, or in wet asphalt, the picture is completely different! Clouds become more contrast, bright lamps and leds are almost the same while all the ground, buildings, trees and even people are almost black!

The reason for that is: reflected picture is dimmed linearly (multiplied with reflectivity of the surface) while our perception is still non-linear.

Look at the graph: Original, dimmed lines with sky, black velvet, ground marks projected onto log graph

The graph of our retina's response is much closer to logarithmic than to graph of the power function though we can use gamma to simplify work flow and speed-up calculations.

Anyway, there is not much relation between the monitor gamma and our non-linear sight.

But it's handy to use gamma instead of the logarithm because its behavior is suddenly close to logarithmic and it's easier to implement using our usual tools.


You can guess that linear math applied to linear values and then raised to gamma differs from linear math applied to gamma-corrected values immediately.

In other words,

(0.5*0.4)^0.45 is not equal to 0.5^0.45*0.4

as well as

0.5^0.45+0.4^0.45 is not equal to (0.5+0.4)^0.45=0.793

So, when you apply gamma after applying blurs and merges, areas of confusion are brighter than if you blur and merge two gamma-corrected images.

Not everyone knows and understands the formulas, but everybody sees the difference between real-world phenomena and CG reproduction

graph with two gamma-curves and graph with two lines and gamma


So, we need digital pictures to be gamma-corrected to look linear on our monitors but when we apply real-world (linear) math to them the result differs from real-world result.


It can be particularly seen on defocus and motion blur phenomena real defocus-sRGB-defocus-Linear def-linear to sRGB

When you apply defocus to already gamma-corrected image you get a kind of box-blur, but when you apply defocus to a true-linear picture and then gamma correct it, you get result similar to real. As real-world is linear and all the real phenomena algorithms are linear but we see them, let's say gamma-corrected.

That effect can be also observed when you merge a masked actor over bright background. Soft edges of the foreground layer in motion blurred or defocused areas are shrinked in the areas where background is bright. If you merge two gamma-corrected images, you need to make some tricks to achieve that. But if your images are linear (relative to measured real luminance) and you apply gamma-correction after merge operation, the Merge reproduce that itself.


You can correctly apply linear algorythms in different ways:

If you import TGA sequences, you can place the Gamut tool right after loaders and select sRGB input, Remove Gamma

  • Loaders should be set to 32 bit float mode.
  • Gamut must be placed before the Saver as well. Set sRGB output, Apply Gamma in it.
  • Intermediate results can be previewed using an additional Gamut node. For your convenience you can save a .setting file from it to the Fusion:\LUTs folder and then select it from Display LUT menu Screenshot with simple composition menu and Gamut_sRGB item.

Image:Merge_Gamma_vs_Linear.jpg

Similar result can be achieved if you replace Gamut with Brightness/Contrast. In that case the BC after loaders must have Gamma=1/2.2 and the BC before the saver must have Gamma=2.2. It will be less accurate but more simple. Screenshot with simple composition For previewing intermediate results you can use Fusion View LUT. Select Edit, set Gamma=2.2 Screenshot with LUT setup dialog


If you work with DPX, just use format settings by default. Screenshot - DPX Format tab Check if color depth of the loader is 32 bit float. If not, select it in the Import tab.


This method has some disadvantages. For example, the ColorCorrector gives unexpected results because it's math is applied to linear image and results are viewed using gamma-correction.

When you increase the Contrast, you expect that gray point you define visually is leaved intact and black and white points are moved equally, but actually you see that highlights and midtones are almost the same while shadows become black quickly. In other words, ((Color-0.5)*Contrast)^2.2 is not the same as ((Color^2.2-0.5)*Contrast) - проверить формулу

When you adjust the Brightness, you can see that shadows are influenced much more too because Color^2.2+Brightness is not equall to (Color+Brightness)^2.2

Similar things happen to Gain and Levels. And by the way, the Histogram doesn't represent correctly lightness of the image you see.

Another way is to decrease gamma in the Brightness/Contrast tool right before the important operations like Merge, Defocus and DirectionalBlur and restore it with the Brightness/Contrast after.

Screenshot with a setup for directional blur

Here are two macros that correctly apply the native Fusion DirectionalBlur and Defocus tools:

DefocusGamma_v02-1

DirectionalBlurGamma_v02

As you can see, there is no need for the artificial and ugly bloom effect. In the macro it's set to 0. Fusion Defocus tool works correctly by itself in the Lens mode being applied to a true-linear image.

The main idea of this article is: all the linear CG algorithms should be applied to image with decreased gamma and viewed with the gamma restored back.

If gamma is increased after effects are applied, the picture gets natural look.


Some Fusion therms explanation you may need during your experiments with gamma

Gain is multiplication operation NewColor=Color*Gain

InvertedGain=1/Gain

Brightness adds a constant value to your color NewColor=Color+Brightness

InvertedBrightness=-Brightness

Image:Gain-Brightness-Gamma.gif


P.S. Don't forget to set correct neutral white and black points before compositing to not suffer from wrong operations order (further explatation).

If you think two plus two multiplied by two equals eight, just believe everything above is true. If you think it equals six, you know what I mean.

Thanks to Yola Rubio for the inspiration.

Special thanks to Dmitry Slepichev and Roman Fedotov for help in math and gnuplot usage.



Gregory Chalenko
www.compositing.tv